1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace PurpleBooth\GitGitHubLint\Command; |
5
|
|
|
|
6
|
|
|
use Github\Client; |
7
|
|
|
use PurpleBooth\GitGitHubLint\GitHubLintImplementation; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
class EvaluatePullRequest extends Command |
17
|
|
|
{ |
18
|
|
|
const ARGUMENT_GITHUB_USERNAME = 'github-username'; |
19
|
|
|
const ARGUMENT_GITHUB_REPOSITORY = 'github-repository'; |
20
|
|
|
const ARGUMENT_PULL_REQUEST_ID = 'pull-request-id'; |
21
|
|
|
const OPTION_TOKEN_OR_USERNAME = 'token-or-username'; |
22
|
|
|
const OPTION_PASSWORD = 'password'; |
23
|
|
|
const COMMAND_NAME = 'git-github-lint:pr'; |
24
|
|
|
|
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this->setName(self::COMMAND_NAME); |
28
|
|
|
$this->setDescription("Check the style of commit messages in a pull request"); |
29
|
|
|
|
30
|
|
|
$help = ''; |
|
|
|
|
31
|
|
|
$help .= "Evaluates a the commits in a pull request and checks that their messages match the style advised by "; |
32
|
|
|
$help .= "Git. It will then update the \"status\" in github (that little dot next to the commits).\n"; |
33
|
|
|
$help .= "\n"; |
34
|
|
|
$help .= "\n"; |
35
|
|
|
$help .= "Here are some good articles on commit message style:\n"; |
36
|
|
|
$help .= "\n"; |
37
|
|
|
$help .= "* http://chris.beams.io/posts/git-commit/\n"; |
38
|
|
|
$help .= "* https://git-scm.com/book/ch5-2.html#Commit-Guidelines\n"; |
39
|
|
|
$help .= "* https://github.com/blog/926-shiny-new-commit-styles\n"; |
40
|
|
|
|
41
|
|
|
$this->setHelp($help); |
42
|
|
|
$this->setDefinition( |
43
|
|
|
new InputDefinition( |
44
|
|
|
[ |
45
|
|
|
new InputArgument( |
46
|
|
|
self::ARGUMENT_GITHUB_USERNAME, |
47
|
|
|
InputArgument::REQUIRED, |
48
|
|
|
'GitHub Username' |
49
|
|
|
), |
50
|
|
|
new InputArgument( |
51
|
|
|
self::ARGUMENT_GITHUB_REPOSITORY, |
52
|
|
|
InputArgument::REQUIRED, |
53
|
|
|
'GitHub Repository' |
54
|
|
|
), |
55
|
|
|
new InputArgument( |
56
|
|
|
self::ARGUMENT_PULL_REQUEST_ID, |
57
|
|
|
InputArgument::REQUIRED, |
58
|
|
|
'The ID of the pull request' |
59
|
|
|
), |
60
|
|
|
new InputOption( |
61
|
|
|
self::OPTION_TOKEN_OR_USERNAME, |
62
|
|
|
['t', 'u'], |
63
|
|
|
InputOption::VALUE_REQUIRED, |
64
|
|
|
'The token or username to authenticate to the API with. Assumed to be token without password' |
65
|
|
|
), |
66
|
|
|
new InputOption( |
67
|
|
|
self::OPTION_PASSWORD, |
68
|
|
|
'p', |
69
|
|
|
InputOption::VALUE_OPTIONAL, |
70
|
|
|
'The password to authenticate to the API with' |
71
|
|
|
), |
72
|
|
|
] |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
78
|
|
|
{ |
79
|
|
|
$io = new SymfonyStyle($input, $output); |
|
|
|
|
80
|
|
|
$config = new Client(); |
81
|
|
|
$linter = new GitHubLintImplementation($config); |
82
|
|
|
|
83
|
|
|
$password = $input->getOption(self::OPTION_PASSWORD); |
84
|
|
|
$authenticationType = Client::AUTH_HTTP_TOKEN; |
85
|
|
|
|
86
|
|
|
if ($password) { |
87
|
|
|
$authenticationType = Client::AUTH_HTTP_PASSWORD; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$config->authenticate( |
91
|
|
|
$input->getOption(self::OPTION_TOKEN_OR_USERNAME), |
92
|
|
|
$password, |
93
|
|
|
$authenticationType |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$gitHubUsername = $input->getArgument(self::ARGUMENT_GITHUB_USERNAME); |
97
|
|
|
$gitHubRepository = $input->getArgument(self::ARGUMENT_GITHUB_REPOSITORY); |
98
|
|
|
$pullRequestId = $input->getArgument(self::ARGUMENT_PULL_REQUEST_ID); |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$io->title(self::COMMAND_NAME); |
102
|
|
|
$io->comment("Analysing PR $gitHubUsername/$gitHubRepository#$pullRequestId"); |
103
|
|
|
$linter->analyse( |
104
|
|
|
$gitHubUsername, |
105
|
|
|
$gitHubRepository, |
106
|
|
|
(int)$pullRequestId |
107
|
|
|
); |
108
|
|
|
$io->success("Analysed PR $gitHubUsername/$gitHubRepository#$pullRequestId"); |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.