From 1d662284dbc40ec19c3e44160ec51a116257bcc4 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Wed, 16 Jun 2021 09:51:40 -0400 Subject: [PATCH 1/2] ISTWCMS-4802: Remove project loading parameters after project is loaded --- uw_wcms_tools.gitlab.inc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index c982ca5..802c065 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -741,6 +741,8 @@ function uw_wcms_tools_gitlab_tag_release(string $namespace, string $path): void if (!$project) { throw new Exception('Invalid project.'); } + // Ensure these are not used. Use properties from $project. + unset($namespace, $path); echo uw_wcms_tools_shell_color('Project: ' . $project->path_with_namespace . "\n", 'blue'); -- GitLab From d8141522eda2b2bbb4ee0946d0e3fe5dff487992 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Tue, 15 Jun 2021 15:57:50 -0400 Subject: [PATCH 2/2] ISTWCMS-4802: Do merges with repository clones instead of merge requests Merge requests will not merge for the third and subsequent tags because the release branch is behind. GitLab requires a rebase which does not work with our branching model. Instead, do the merge in a repository clone. The clones will be kept to avoid the need to clone each time. The user running the script must have read-write access to UW_WCMS_TOOLS_REPOSITORY_DIRECTORY and ssh keys configured to allow the push. --- uw_wcms_tools.gitlab.inc | 61 +++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index 802c065..1dca077 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -15,6 +15,8 @@ require_once 'uw_wcms_tools.lib.inc'; // The URL of the Gitlab server. define('UW_WCMS_TOOLS_GITLAB_SERVER', 'https://git.uwaterloo.ca/'); +// The directory in which to keep clones of repositories. +define('UW_WCMS_TOOLS_REPOSITORY_DIRECTORY', '/home/wcms-wkr/Repositories'); /** * Return the GitLab private token. @@ -755,7 +757,7 @@ function uw_wcms_tools_gitlab_tag_release(string $namespace, string $path): void // Update or create release branch. if (in_array($current_branches['rel'], $branch_names)) { - echo 'Merge ' . $current_branches['dev'] . ' into ' . $current_branches['rel'] . '... '; + echo 'Merge ' . $current_branches['dev'] . ' into ' . $current_branches['rel'] . "...\n"; $release_branch = uw_wcms_tools_query_gitlab_api('projects/' . $project->id . '/repository/branches/' . urlencode($current_branches['rel'])); if ($release_branch['http_status'] !== 200) { @@ -780,30 +782,43 @@ function uw_wcms_tools_gitlab_tag_release(string $namespace, string $path): void return; } else { - // Create merge request. - $params = [ - 'source_branch' => $current_branches['dev'], - 'target_branch' => $current_branches['rel'], - 'title' => 'Tag ' . $current_branches['tag_next'], - ]; - $merge_request = uw_wcms_tools_query_gitlab_api('projects/' . $project->id . '/merge_requests', $params, 'POST'); - if ($merge_request['http_status'] !== 201) { - echo uw_wcms_tools_shell_color("Error.\n", 'red'); - var_dump($merge_request); - throw new Exception('Unable to create merge request.'); + // Merge changes to development branch into release branch. + // + // Path of this project's local repository clone. + $repo_path = UW_WCMS_TOOLS_REPOSITORY_DIRECTORY . '/' . $project->path; + + // Test if this project has already been cloned. If not, clone it. + if (!is_dir($repo_path)) { + if (!chdir(UW_WCMS_TOOLS_REPOSITORY_DIRECTORY)) { + throw new Exception('Unable to chdir into ' . UW_WCMS_TOOLS_REPOSITORY_DIRECTORY . '.'); + } + exec('git clone --branch=' . escapeshellarg($current_branches['dev']) . ' ' . escapeshellarg($project->ssh_url_to_repo), $output, $result_code); + if ($result_code) { + throw new Exception('Unable to clone; status: ' . $result_code . '.'); + } } - // Accept (do) merge request. - // Accept will fail if it happens too soon after create. - sleep(1); - $params = [ - 'sha' => $dev_branch['body']->commit->id, + // Merge the development branch into release branch and push. + if (!chdir($repo_path)) { + throw new Exception('Unable to chdir into ' . $repo_path . '.'); + } + // Reset branches and merge. + $merge_commands = [ + 'git fetch', + 'git checkout ' . escapeshellarg($current_branches['dev']), + 'git reset --hard origin/' . escapeshellarg($current_branches['dev']), + 'git checkout ' . escapeshellarg($current_branches['rel']), + 'git reset --hard origin/' . escapeshellarg($current_branches['rel']), + 'git merge --no-ff ' . escapeshellarg($current_branches['dev']), ]; - $merge_request = uw_wcms_tools_query_gitlab_api('projects/' . $project->id . '/merge_requests/' . $merge_request['body']->iid . '/merge', $params, 'PUT'); - if ($merge_request['http_status'] !== 200) { - echo uw_wcms_tools_shell_color("Error.\n", 'red'); - var_dump($merge_request); - throw new Exception('Unable to accept merge request.'); + exec(implode(' && ', $merge_commands), $output, $result_code); + if ($result_code) { + throw new Exception('Unable to merge; status: ' . $result_code . '.'); + } + // Push. + exec('git push origin ' . escapeshellarg($current_branches['rel']), $output, $result_code); + if ($result_code) { + throw new Exception('Unable to push; status: ' . $result_code . '.'); } // Update release branch. @@ -813,7 +828,7 @@ function uw_wcms_tools_gitlab_tag_release(string $namespace, string $path): void var_dump($release_branch); throw new Exception('Unable to load release branch after merge.'); } - echo uw_wcms_tools_shell_color("Done.\n", 'green'); + echo uw_wcms_tools_shell_color("Merge done.\n", 'green'); } } else { -- GitLab