$title, ) ); return is_array( $result ) && $result['title'] === $title; } /** * Function to create branch using the GitHub REST API. * * @param string $branch The branch to be created. * @param string $sha The sha1 reference for the branch. * @return bool True on success, False otherwise. */ function create_github_branch( $branch, $sha ) { global $repo_owner, $repo_name; $ref = "refs/heads/{$branch}"; $result = do_github_api_post_request( "/repos/{$repo_owner}/{$repo_name}/git/refs", array( 'ref' => $ref, 'sha' => $sha, ) ); return is_array( $result ) && $result['ref'] === $ref; } /** * Function to create branch using the GitHub REST API from an existing branch. * * @param string $source The branch from which to create. * @param string $target The branch to be created. * @return bool True on success, False otherwise. */ function create_github_branch_from_branch( $source, $target ) { $ref = get_ref_from_branch( $source ); if ( ! $ref ) { return false; } return create_github_branch( $target, $ref ); } /** * Function to do a GitHub API POST Request. * * @param array $request_url * @param array $body The body of the request to be json encoded. * @return mixed The json-decoded response if a response is received, 'false' (or whatever file_get_contents returns) otherwise. */ function do_github_api_post_request( $request_path, $body ) { global $github_token, $github_api_url, $github_api_response_code; $context = stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => array( 'Accept: application/vnd.github.v3+json', 'Content-Type: application/json', 'User-Agent: GitHub Actions for creation of milestones', 'Authorization: bearer ' . $github_token, ), 'content' => json_encode( $body ), ), ) ); $full_request_url = rtrim( $github_api_url, '/' ) . '/' . ltrim( $request_path, '/' ); $result = @file_get_contents( $full_request_url, false, $context ); // Verify that the post request was sucessful. $status_line = $http_response_header[0]; preg_match( "/^HTTPS?\/\d\.\d\s+(\d{3})\s+/i", $status_line, $matches ); $github_api_response_code = $matches[1]; if ( '2' !== substr( $github_api_response_code, 0, 1 ) ) { return false; } return is_string( $result ) ? json_decode( $result, true ) : $result; } /** * Function to query the GitHub GraphQL API. * * @param string $body The GraphQL-formatted request body, without "query" or "mutation" wrapper. * @param bool $is_mutation True if the request is a mutation, false if it's a query. * @return mixed The json-decoded response if a response is received, 'false' (or whatever file_get_contents returns) otherwise. */ function do_graphql_api_request( $body, $is_mutation = false ) { global $github_token, $graphql_api_url; $keyword = $is_mutation ? 'mutation' : 'query'; $data = array( 'query' => "$keyword { $body }" ); $context = stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => array( 'Accept: application/json', 'Content-Type: application/json', 'User-Agent: GitHub action to set the milestone for a pull request', 'Authorization: bearer ' . $github_token, ), 'content' => json_encode( $data ), ), ) ); $result = file_get_contents( $graphql_api_url, false, $context ); return is_string( $result ) ? json_decode( $result, true ) : $result; } // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions