/
CommentAddPullRequestCommand.php
62 lines (54 loc) · 1.92 KB
/
CommentAddPullRequestCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Terminus Plugin that contain a collection of commands useful during
* the build step on a [Pantheon](https://www.pantheon.io) site that uses
* a GitHub PR workflow.
*
* See README.md for usage information.
*/
namespace Pantheon\TerminusBuildTools\Commands;
use Pantheon\Terminus\Exceptions\TerminusException;
/**
* Pull Request Comment Command
*/
class CommentAddPullRequestCommand extends BuildToolsBase
{
/**
* Add a comment to a specified PR on the repository.
*
* @authorize
*
* @command build:comment:add:pr
*/
public function commentAddPullRequest(
$options = [
'pr_id' => '',
'message' => '',
'site_url' => ''
])
{
// Get current repository and commit
$remoteUrlFromGit = exec('git config --get remote.origin.url');
$prId = $options['pr_id'];
if (empty($prId)) {
throw new TerminusException( '--pr_id=<id> is required.' );
}
// Create a Git repository service provider appropriate to the URL
$this->inferGitProviderFromUrl($remoteUrlFromGit);
// Ensure that credentials for the Git provider are available
$this->providerManager()->validateCredentials();
// Compile message
if (!empty($options['site_url'])) {
$message = "[![Visit Site](https://raw.githubusercontent.com/pantheon-systems/terminus-build-tools-plugin/master/assets/images/visit-site-36.png)](".$options['site_url'].")\n\n".$options['message'];
} else {
$message = $options['message'];
}
if (!empty($message)) {
// Submit message
$targetProject = $this->projectFromRemoteUrl($remoteUrlFromGit);
$this->git_provider->commentOnPullRequest($targetProject, $prId, $message);
} else {
throw new TerminusException( '--message and/or --site_url are required.' );
}
}
}