Skip to content

Commit 18637e8

Browse files
AronNovakclaude
andcommitted
Replace curl with gh CLI and remove unused docker_mirror_url parameter
- Replace manual curl GitHub API calls with gh CLI commands in DeploymentTrait - gh pr view for fetching PR data - gh repo view for checking repo visibility - gh issue comment for posting comments - Remove unused docker_mirror_url parameter from BootstrapTrait - Remove unused $github_token variable (gh CLI uses GITHUB_TOKEN env var automatically) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4f863fc commit 18637e8

File tree

2 files changed

+30
-43
lines changed

2 files changed

+30
-43
lines changed

robo-components/BootstrapTrait.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,22 @@ trait BootstrapTrait {
2020
* The Pantheon machine token.
2121
* @param string $github_token
2222
* The GitHub personal access token for a user with access to this project.
23-
* @param string $docker_mirror_url
24-
* The Docker mirror URL. Optional, but expect CI failures if not set,
25-
* this is due to rate limiting on Docker Hub.
2623
* @param string $http_basic_auth_user
2724
* The HTTP basic auth user. Optional. If set, all the Pantheon environments
2825
* will be protected with HTTP basic auth.
2926
* @param string $http_basic_auth_password
3027
* The HTTP basic auth password. Optional.
3128
*/
32-
public function bootstrapProject(string $project_name, string $github_repository_url, string $terminus_token, string $github_token, string $docker_mirror_url = '', string $http_basic_auth_user = '', string $http_basic_auth_password = '') {
29+
public function bootstrapProject(string $project_name, string $github_repository_url, string $terminus_token, string $github_token, string $http_basic_auth_user = '', string $http_basic_auth_password = '') {
3330
// Extract project name from $github_repository_url.
3431
// The syntax is like: git@github.com:Organization/projectname.git .
3532
preg_match('/github.com[:\/](.*)\/(.*)\.git/', $github_repository_url, $matches);
3633
$github_organization = $matches[1];
3734
$project_machine_name = $matches[2];
3835

39-
$this->verifyRequirements($project_name, $github_organization, $project_machine_name, $terminus_token, $github_token, $docker_mirror_url, $http_basic_auth_user, $http_basic_auth_password);
36+
$this->verifyRequirements($project_name, $github_organization, $project_machine_name, $terminus_token, $github_token, $http_basic_auth_user, $http_basic_auth_password);
4037

41-
$this->prepareGithubRepository($project_name, $github_organization, $project_machine_name, $github_repository_url, $docker_mirror_url);
38+
$this->prepareGithubRepository($project_name, $github_organization, $project_machine_name, $github_repository_url);
4239

4340
$this->createPantheonProject($terminus_token, $project_name, $project_machine_name);
4441

@@ -59,8 +56,8 @@ public function bootstrapProject(string $project_name, string $github_repository
5956
$this->say("You might want to run the following commands to properly place the project:");
6057
$this->say("mv .bootstrap ../$project_machine_name");
6158
$this->say("mv .pantheon ../$project_machine_name/.pantheon");
62-
$this->say("To configure autodeployment to pantheon run:");
63-
$this->say("ddev robo deploy:config-autodeploy $terminus_token, $github_token");
59+
$this->say("To configure autodeployment to Pantheon run:");
60+
$this->say("ddev robo deploy:config-autodeploy $terminus_token $github_token");
6461
}
6562

6663
/**
@@ -74,10 +71,8 @@ public function bootstrapProject(string $project_name, string $github_repository
7471
* The project machine name in GH slug.
7572
* @param string $github_repository_url
7673
* The clone URL of the GitHub repository.
77-
* @param string $docker_mirror_url
78-
* The Docker mirror URL. Optional, but expect CI failures if not set.
7974
*/
80-
protected function prepareGithubRepository(string $project_name, string $organization, string $project_machine_name, string $github_repository_url, string $docker_mirror_url = '') {
75+
protected function prepareGithubRepository(string $project_name, string $organization, string $project_machine_name, string $github_repository_url) {
8176
$temp_remote = 'bootstrap_' . time();
8277
$this->taskExec("git remote add $temp_remote $github_repository_url")
8378
->run();
@@ -370,14 +365,12 @@ public function lockPantheonEnvironments(string $project_machine_name, string $h
370365
* The Pantheon machine token.
371366
* @param string $github_token
372367
* The GitHub token.
373-
* @param string $docker_mirror_url
374-
* The Docker mirror URL.
375368
* @param string $http_basic_auth_user
376369
* The HTTP basic auth user.
377370
* @param string $http_basic_auth_password
378371
* The HTTP basic auth password.
379372
*/
380-
protected function verifyRequirements(string $project_name, string $organization, string $project_machine_name, string $terminus_token, string $github_token, string $docker_mirror_url, $http_basic_auth_user, $http_basic_auth_password) {
373+
protected function verifyRequirements(string $project_name, string $organization, string $project_machine_name, string $terminus_token, string $github_token, $http_basic_auth_user, $http_basic_auth_password) {
381374
if (is_dir('.bootstrap')) {
382375
throw new \Exception('The .bootstrap directory already exists. Please remove / move it and try again.');
383376
}
@@ -402,12 +395,6 @@ protected function verifyRequirements(string $project_name, string $organization
402395
if (empty(trim($github_token))) {
403396
throw new \Exception('The GitHub token is empty.');
404397
}
405-
if (empty(trim($docker_mirror_url))) {
406-
throw new \Exception('The Docker mirror URL is empty.');
407-
}
408-
if (!empty($docker_mirror_url) && !filter_var($docker_mirror_url, FILTER_VALIDATE_URL)) {
409-
throw new \Exception('The Docker mirror URL is not a valid URL.');
410-
}
411398
}
412399

413400
}

robo-components/DeploymentTrait.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,19 @@ public function deployConfigAutodeploy(string $token, string $github_token, stri
708708
$pantheon_info = $this->getPantheonNameAndEnv();
709709
$project_name = $pantheon_info['name'];
710710

711-
// Generate SSH key for deployment
711+
// Generate SSH key for deployment.
712712
$result = $this->taskExec('ssh-keygen -t rsa -f deploy-key -P ""')->run();
713713
if ($result->getExitCode() !== 0) {
714714
throw new \Exception('The key generation failed.');
715715
}
716716

717-
// Encrypt the SSH key for use in GitHub Actions
717+
// Encrypt the SSH key for use in GitHub Actions.
718718
$result = $this->taskExec('openssl rand -hex 32')->printOutput(FALSE)->run();
719719
if ($result->getExitCode() !== 0) {
720720
throw new \Exception('Failed to generate encryption key.');
721721
}
722722
$encryption_key = trim($result->getMessage());
723-
723+
724724
$result = $this->taskExec('openssl rand -hex 16')->printOutput(FALSE)->run();
725725
if ($result->getExitCode() !== 0) {
726726
throw new \Exception('Failed to generate encryption IV.');
@@ -737,7 +737,7 @@ public function deployConfigAutodeploy(string $token, string $github_token, stri
737737
->run();
738738
$pantheon_git_url = trim($result->getMessage());
739739

740-
// Update GitHub Actions workflows if they exist
740+
// Update GitHub Actions workflows if they exist.
741741
if (file_exists('.github/workflows/lint.template.yml')) {
742742
$this->_exec("cp .github/workflows/lint.template.yml .github/workflows/lint.yml");
743743
$this->taskReplaceInFile('.github/workflows/lint.yml')
@@ -786,7 +786,6 @@ public function deployNotify(string $pantheon_environment = 'qa', string $issue_
786786
$data = ['body' => $issue_comment];
787787
$issue_comment = json_encode($data);
788788
}
789-
$github_token = getenv('GITHUB_TOKEN');
790789
$git_commit_message = getenv('GITHUB_COMMIT_MESSAGE');
791790
if (strstr($git_commit_message, 'Merge pull request') === FALSE && strstr($git_commit_message, ' (#') === FALSE) {
792791
$this->say($git_commit_message);
@@ -809,21 +808,20 @@ public function deployNotify(string $pantheon_environment = 'qa', string $issue_
809808
$this->say("Could not determine the PR number from the commit message: $git_commit_message");
810809
return;
811810
}
812-
// Retrieve the issue number from the PR description via GitHub API.
811+
// Retrieve the issue number from the PR description via GitHub CLI.
813812
$pr_number = $pr_matches[1][0];
814-
$pr = $this->taskExec("curl -H \"Authorization: token $github_token\" https://api.github.com/repos/" . self::$githubProject . "/pulls/$pr_number")
813+
$pr_body = $this->taskExec("gh pr view $pr_number --json body --jq .body")
815814
->printOutput(FALSE)
816815
->run()
817816
->getMessage();
818-
$pr = json_decode($pr);
819-
if (!isset($pr->body)) {
817+
if (empty(trim($pr_body))) {
820818
$this->say("Could not determine the issue number from the PR: $git_commit_message");
821819
return;
822820
}
823821
// The issue number should be the "#1234"-like reference in the PR body.
824-
preg_match_all('!#([0-9]+)\s+!', $pr->body, $issue_matches);
822+
preg_match_all('!#([0-9]+)\s+!', $pr_body, $issue_matches);
825823
if (!isset($issue_matches[1][0])) {
826-
$this->say("Could not determine the issue number from the PR description: $pr->body");
824+
$this->say("Could not determine the issue number from the PR description: $pr_body");
827825
return;
828826
}
829827
foreach ($issue_matches[1] as $issue_match) {
@@ -837,17 +835,13 @@ public function deployNotify(string $pantheon_environment = 'qa', string $issue_
837835
$pantheon_info = $this->getPantheonNameAndEnv();
838836
$pantheon_terminus_environment = $pantheon_info['name'] . '.' . $pantheon_environment;
839837

840-
// Let's figure out if the repository is public or not via GitHub API.
841-
$repo = $this->taskExec("curl -H \"Authorization: token $github_token\" https://api.github.com/repos/" . self::$githubProject)
838+
// Check if the repository is private via GitHub CLI.
839+
$is_private = $this->taskExec("gh repo view --json isPrivate --jq .isPrivate")
842840
->printOutput(FALSE)
843841
->run()
844842
->getMessage();
845-
$repo = json_decode($repo);
846-
if (!isset($repo->private)) {
847-
$this->yell("Could not determine if the repository is private or not.");
848-
return;
849-
}
850-
if ($repo->private) {
843+
$is_private = trim($is_private) === 'true';
844+
if ($is_private) {
851845
$quick_link = $this->deployGetEnvironmentUrl($pantheon_terminus_environment);
852846
}
853847
else {
@@ -857,19 +851,25 @@ public function deployNotify(string $pantheon_environment = 'qa', string $issue_
857851

858852
if (empty($issue_comment)) {
859853
if (empty($pr_number)) {
860-
$issue_comment = "{\"body\": \"The latest merged PR just got deployed successfully to Pantheon [`$pantheon_environment`]($quick_link) environment\"}";
854+
$comment_body = "The latest merged PR just got deployed successfully to Pantheon [`$pantheon_environment`]($quick_link) environment";
861855
}
862856
else {
863-
$issue_comment = "{\"body\": \"The latest merged PR #$pr_number just got deployed successfully to Pantheon [`$pantheon_environment`]($quick_link) environment\"}";
857+
$comment_body = "The latest merged PR #$pr_number just got deployed successfully to Pantheon [`$pantheon_environment`]($quick_link) environment";
864858
}
865859
}
860+
else {
861+
// Extract body from JSON if issue_comment was provided.
862+
$comment_data = json_decode($issue_comment, TRUE);
863+
$comment_body = $comment_data['body'] ?? $issue_comment;
864+
}
865+
866866
foreach ($issue_numbers as $issue_number) {
867-
$result = $this->taskExec("curl -X POST -H 'Authorization: token $github_token' -d '$issue_comment' https://api.github.com/repos/" . self::$githubProject . "/issues/$issue_number/comments")
867+
$result = $this->taskExec("gh issue comment $issue_number --body " . escapeshellarg($comment_body))
868868
->printOutput(FALSE)
869869
->run();
870870
$exit_code = $result->getExitCode();
871871
if ($exit_code) {
872-
throw new \Exception("Could not notify GitHub of the deployment, GitHub API error: " . $result->getMessage());
872+
throw new \Exception("Could not notify GitHub of the deployment, GitHub CLI error: " . $result->getMessage());
873873
}
874874
}
875875
}

0 commit comments

Comments
 (0)