Skip to content

Commit

Permalink
Merge pull request #169 from Automattic/add-status-api-1
Browse files Browse the repository at this point in the history
Add Status API support
  • Loading branch information
gudmdharalds authored May 14, 2021
2 parents 5a24499 + b60f6b4 commit dad9933
Show file tree
Hide file tree
Showing 9 changed files with 587 additions and 25 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Alternatively, if you do not wish to run TeamCity in a Docker-instance, you can
`vip-go-ci.php` exits with different UNIX exit codes depending on what problems were found and if any system issues were encountered:

* Code `0`: Normal, no errors were found in the code scanned and no fatal system errors were encountered. There could have been warnings found in the code, though.
* Code `249`: Scanning exceeded maximum time allowed.
* Code `250`: Scanning was completed, but some errors were found in the code.
* Code `251`: Exiting due to a system problem.
* Code `252`: A fatal problem with GitHub was encountered leading to an exit.
Expand Down Expand Up @@ -302,6 +303,13 @@ For example:

> {"skip-draft-prs":true}
### Maximum execution time

`vip-go-ci` supports setting maximum execution time. Once the maximum time is exceeded, it will exit with an error code (see [above](#exit-codes)).

Use the `--max-exec-time` option to set maximum execution time (in seconds):

> ./vip-go-ci.php ... --max-exec-time=600
### Informational URL

Expand Down Expand Up @@ -628,3 +636,27 @@ support-level-field-name= ; Support level field name in meta API

This file is not included, and needs to be configured manually. When that is complete, the tests can be re-run.

## Setting GitHub Build Status

`vip-go-ci` ships with an independent utility, `github-commit-status.php` to set [GitHub build status indication](https://docs.github.com/en/rest/reference/repos#statuses) for a particular commit. Use the utility to indicate that `vip-go-ci` is currently scanning or what the results of the scanning was (success, failure). The utility will communicate directly with the GitHub API to set the status.

Example usage:

```
php ~/vip-go-ci-tools/vip-go-ci/github-commit-status.php --repo-owner=`repo-owner` --repo-name=`repo-name` --github-token=`token` --github-commit=`commit-ID` --build-context=`vip-go-ci` --build-description="Analysis is in progress" --build-state="pending"
php ~/vip-go-ci-tools/vip-go-ci/vip-go-ci.php ...
if [ "$?" == "0" ] ; then
export BUILD_STATE="success"
export BUILD_DESCRIPTION="No problems were identified"
else
export BUILD_STATE="failure"
export BUILD_DESCRIPTION="Problems were identified"
fi
php ~/vip-go-ci-tools/vip-go-ci/github-commit-status.php --repo-owner=`repo-owner` --repo-name=`repo-name` --github-token=`token` --github-commit=`commit-ID` --build-context=`vip-go-ci` --build-description="$BUILD_DESCRIPTION" --build-state="$BUILD_STATE"
```

Note that the utility supports setting options via [environmental variables](#configuring-via-environmental-variables), just like `vip-go-ci` does.

9 changes: 6 additions & 3 deletions auto-approval.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function vipgoci_auto_approval_non_approval(
'as it contains ' .
'files which are not ' .
'automatically approvable' .
' -- PR URL: https://github.com/' .
' -- PR URL: ' .
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
rawurlencode( $options['repo-owner'] ) .
'/' .
rawurlencode( $options['repo-name'] ) .
Expand Down Expand Up @@ -317,7 +318,8 @@ function vipgoci_autoapproval_do_approve(
'as it alters or creates ' .
'only files that can be ' .
'automatically approved' .
' -- PR URL: https://github.com/' .
' -- PR URL: ' .
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
rawurlencode( $options['repo-owner'] ) .
'/' .
rawurlencode( $options['repo-name'] ) .
Expand Down Expand Up @@ -600,7 +602,8 @@ function vipgoci_auto_approval_scan_commit(
'No action taken with Pull-Request #' .
(int) $pr_item->number . ' ' .
'since no files were found' .
' -- PR URL: https://github.com/' .
' -- PR URL: ' .
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
rawurlencode( $options['repo-owner'] ) .
'/' .
rawurlencode( $options['repo-name'] ) .
Expand Down
5 changes: 5 additions & 0 deletions defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
define( 'VIPGOCI_SYNTAX_ERROR_STR', 'PHP Syntax Errors Found' );
define( 'VIPGOCI_GITHUB_ERROR_STR', 'GitHub API communication error. ' .
'Please contact a human.' );
/*
* Base URLs for GitHub
*/
define( 'VIPGOCI_GITHUB_WEB_BASE_URL', 'https://github.com' );

/* Define if not defined. Unit-tests can define this for testing. */
if ( ! defined( 'VIPGOCI_GITHUB_BASE_URL' ) ) {
Expand Down Expand Up @@ -78,6 +82,7 @@
*/

define( 'VIPGOCI_EXIT_NORMAL', 0 );
define( 'VIPGOCI_EXIT_EXEC_TIME', 249 );
define( 'VIPGOCI_EXIT_CODE_ISSUES', 250 );
define( 'VIPGOCI_EXIT_SYSTEM_PROBLEM', 251 );
define( 'VIPGOCI_EXIT_GITHUB_PROBLEM', 252 );
Expand Down
55 changes: 53 additions & 2 deletions github-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ function vipgoci_github_pr_generic_comment_submit_results(
"\n\r\n\r" .

// And finally a URL to the issue is
'https://github.com/' .
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
$repo_owner . '/' .
$repo_name . '/' .
'blob/' .
Expand Down Expand Up @@ -1786,7 +1786,8 @@ function vipgoci_github_pr_generic_comment_submit_results(
"\n\r\n\r" .

"Scan performed on the code at commit " . $commit_id .
" ([view code](https://github.com/" .
" ([view code](" .
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
rawurlencode( $repo_owner ) . "/" .
rawurlencode( $repo_name ) . "/" .
"tree/" .
Expand Down Expand Up @@ -4097,3 +4098,53 @@ function vipgoci_github_repo_collaborators_get(

return $repo_users_all;
}

/*
* Create commit status using the
* Status API.
*/
function vipgoci_github_status_create(
string $repo_owner,
string $repo_name,
string $github_token,
string $commit_id,
string $state,
string $target_url,
string $description,
string $context
) {
$github_url =
VIPGOCI_GITHUB_BASE_URL . '/' .
'repos/' .
rawurlencode( $repo_owner ) . '/' .
rawurlencode( $repo_name ) . '/' .
'statuses/' .
rawurlencode( $commit_id );

$github_postfields = array(
'state' => $state,
'description' => $description,
'context' => $context,
);

if ( ! empty( $target_url ) ) {
$github_postfields[ 'target_url' ] =
$target_url;
}

vipgoci_log(
'Setting GitHub commit status for a particular commit-ID',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'status' => $github_postfields,
)
);

vipgoci_github_post_url(
$github_url,
$github_postfields,
$github_token
);
}
151 changes: 151 additions & 0 deletions github-commit-status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/usr/bin/env php
<?php

// phpcs:disable PSR1.Files.SideEffects

define( 'VIPGOCI_INCLUDED', true );

require_once( __DIR__ . '/requires.php' );


/*
* Configure PHP error reporting.
*/
vipgoci_set_php_error_reporting();

/*
* Recognized options, get options
*/
$options_recognized = array(
'env-options:',
'repo-owner:',
'repo-name:',
'github-token:',
'github-commit:',
'build-state:',
'build-description:',
'build-context:',
);

$options = getopt(
null,
$options_recognized
);

/*
* Options to remove of any sensitive
* detail when cleaned later.
*/
vipgoci_options_sensitive_clean(
null,
array(
'github-token'
)
);


/*
* Parse options
*/
vipgoci_option_array_handle(
$options,
'env-options',
array(),
null,
',',
false
);

vipgoci_options_read_env(
$options,
$options_recognized
);

/*
* Verify we have all the options
* we need.
*/
if (
( ! isset(
$options['repo-owner'],
$options['repo-name'],
$options['github-token'],
$options['github-commit'],
$options['build-state'],
$options['build-description'],
$options['build-context']
) )
||
(
isset( $options['help'] )
)
) {
print 'Usage: ' . $argv[0] . PHP_EOL .
PHP_EOL .
"\t" . '--repo-owner=STRING Specify repository owner, can be an organization' . PHP_EOL .
"\t" . '--repo-name=STRING Specify name of the repository' . PHP_EOL .
"\t" . '--github-token=STRING The access-token to use to communicate with GitHub' . PHP_EOL .
"\t" . '--github-commit=STRING Specify the exact commit to set state for' . PHP_EOL .
PHP_EOL .
"\t" . '--build-state=STRING Specify build state, one of: "pending", "failure", ' . PHP_EOL .
"\t" . ' and "success" are valid.' . PHP_EOL .
"\t" . '--build-description=STRING Specify description for end-user, displayed along with ' . PHP_EOL .
"\t" . ' state.' . PHP_EOL .
"\t" . '--build-context=STRING Specify context, a consistent identifier used across ' . PHP_EOL .
"\t" . ' all the build states' . PHP_EOL .
PHP_EOL .
"\t" . '--help Prints this message.' . PHP_EOL .
PHP_EOL .
'All options are mandatory.' . PHP_EOL;

exit( VIPGOCI_EXIT_USAGE_ERROR );
}

/*
* Verify that --build-state is of valid
* value.
*/
switch( $options['build-state'] ) {
case 'pending':
case 'failure':
case 'success':
break;

default:
vipgoci_sysexit(
'Invalid parameter for --build-state, only "pending", "failure", and "success" are valid',
array(
'build-state' => $options['build-state'],
)
);
}

/*
* Log that we are setting build
* status and set it.
*/
vipgoci_log(
'Setting build status for commit ...',
array(
'options' => vipgoci_options_sensitive_clean(
$options
)
)
);

vipgoci_github_status_create(
$options['repo-owner'],
$options['repo-name'],
$options['github-token'],
$options['github-commit'],
$options['build-state'],
'',
$options['build-description'],
$options['build-context']
);

vipgoci_log(
'Finished, exiting',
);


Loading

0 comments on commit dad9933

Please sign in to comment.