2021-04-15 15:16:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Script to automatically assign a milestone to a pull request when it's merged.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/GithubActions
|
|
|
|
*/
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions
|
|
|
|
|
2021-08-03 09:33:33 +00:00
|
|
|
require_once __DIR__ . '/post-request-shared.php';
|
2021-04-15 15:16:00 +00:00
|
|
|
|
2021-08-03 08:07:24 +00:00
|
|
|
/*
|
2021-04-15 15:16:00 +00:00
|
|
|
* Select the milestone to be added:
|
|
|
|
*
|
|
|
|
* 1. Get the first 10 milestones sorted by creation date descending.
|
|
|
|
* (we'll never have more than 2 or 3 active milestones but let's get 10 to be sure).
|
|
|
|
* 2. Discard those not open or whose title is not a proper version number ("X.Y.Z").
|
|
|
|
* 3. Sort descending using version_compare.
|
|
|
|
* 4. Get the oldest one that does not have a corresponding "release/X.Y" branch.
|
|
|
|
*/
|
|
|
|
|
|
|
|
echo "Getting the list of milestones...\n";
|
|
|
|
|
|
|
|
$query = "
|
|
|
|
repository(owner:\"$repo_owner\", name:\"$repo_name\") {
|
2021-04-19 12:46:45 +00:00
|
|
|
milestones(first: 10, states: [OPEN], orderBy: {field: CREATED_AT, direction: DESC}) {
|
2021-04-15 15:16:00 +00:00
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
$json = do_graphql_api_request( $query );
|
|
|
|
$milestones = $json['data']['repository']['milestones']['nodes'];
|
|
|
|
$milestones = array_map(
|
|
|
|
function( $x ) {
|
2021-04-19 12:46:45 +00:00
|
|
|
return 1 === preg_match( '/^\d+\.\d+\.\d+$/D', $x['title'] ) ? $x : null;
|
2021-04-15 15:16:00 +00:00
|
|
|
},
|
|
|
|
$milestones
|
|
|
|
);
|
|
|
|
$milestones = array_filter( $milestones );
|
|
|
|
usort(
|
|
|
|
$milestones,
|
|
|
|
function( $a, $b ) {
|
|
|
|
return version_compare( $b['title'], $a['title'] );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
echo 'Latest open milestone: ' . $milestones[0]['title'] . "\n";
|
|
|
|
|
|
|
|
$chosen_milestone = null;
|
|
|
|
foreach ( $milestones as $milestone ) {
|
|
|
|
$milestone_title_parts = explode( '.', $milestone['title'] );
|
|
|
|
$milestone_release_branch = 'release/' . $milestone_title_parts[0] . '.' . $milestone_title_parts[1];
|
|
|
|
|
|
|
|
$query = "
|
|
|
|
repository(owner:\"$repo_owner\", name:\"$repo_name\") {
|
|
|
|
ref(qualifiedName: \"refs/heads/$milestone_release_branch\") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
$result = do_graphql_api_request( $query );
|
|
|
|
|
|
|
|
if ( is_null( $result['data']['repository']['ref'] ) ) {
|
|
|
|
$chosen_milestone = $milestone;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the milestones have a release branch, just take the newest one.
|
|
|
|
if ( is_null( $chosen_milestone ) ) {
|
2021-07-29 14:47:27 +00:00
|
|
|
echo "WARNING: No milestone without release branch found, the newest one will be assigned.\n";
|
2021-04-15 15:16:00 +00:00
|
|
|
$chosen_milestone = $milestones[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
echo 'Milestone that will be assigned: ' . $chosen_milestone['title'] . "\n";
|
|
|
|
|
|
|
|
if ( getenv( 'DRY_RUN' ) ) {
|
|
|
|
echo "Dry run, skipping the actual milestone assignment\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign the milestone to the pull request.
|
|
|
|
*/
|
|
|
|
|
2021-07-28 13:20:16 +00:00
|
|
|
echo 'Assigning the milestone to the pull request... ';
|
2021-04-15 15:16:00 +00:00
|
|
|
|
|
|
|
$milestone_id = $chosen_milestone['id'];
|
|
|
|
$mutation = "
|
|
|
|
updatePullRequest(input: {pullRequestId: \"$pr_id\", milestoneId: \"$milestone_id\"}) {
|
|
|
|
clientMutationId
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
2021-07-28 13:20:16 +00:00
|
|
|
$result = do_graphql_api_request( $mutation, true );
|
|
|
|
if ( is_array( $result ) ) {
|
|
|
|
if ( empty( $result['errors'] ) ) {
|
|
|
|
echo "Ok!\n";
|
|
|
|
} else {
|
|
|
|
echo "\n*** Errors found while assigning the milestone:\n";
|
|
|
|
echo var_dump( $result['errors'] );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "\n*** Error found while assigning the milestone: file_get_contents returned the following:\n";
|
|
|
|
echo var_dump( $result );
|
|
|
|
}
|
2021-04-15 15:16:00 +00:00
|
|
|
|
|
|
|
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions
|