Completed
Push — master ( 2aeea6...a38966 )
by Billie
04:01
created

EvaluatePullRequest::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 8.8571
cc 1
eloc 33
nc 1
nop 0
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               = 'token';
22
    const COMMAND_NAME               = 'git-github-lint:pr';
23
24
    protected function configure()
25
    {
26
        $this->setName(self::COMMAND_NAME);
27
        $this->setDescription("Check the style of commit messages in a pull request");
28
29
        $help = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
30
        $help .= "Evaluates a the commits in a pull request and checks that their messages match the style advised by ";
31
        $help .= "Git. It will then update the \"status\" in github (that little dot next to the commits).\n";
32
        $help .= "\n";
33
        $help .= "\n";
34
        $help .= "Here are some good articles on commit message style:\n";
35
        $help .= "\n";
36
        $help .= "* http://chris.beams.io/posts/git-commit/\n";
37
        $help .= "* https://git-scm.com/book/ch5-2.html#Commit-Guidelines\n";
38
        $help .= "* https://github.com/blog/926-shiny-new-commit-styles\n";
39
40
        $this->setHelp($help);
41
        $this->setDefinition(
42
            new InputDefinition(
43
                [
44
                    new InputArgument(
45
                        self::ARGUMENT_GITHUB_USERNAME,
46
                        InputArgument::REQUIRED,
47
                        'GitHub Username'
48
                    ),
49
                    new InputArgument(
50
                        self::ARGUMENT_GITHUB_REPOSITORY,
51
                        InputArgument::REQUIRED,
52
                        'GitHub Repository'
53
                    ),
54
                    new InputArgument(
55
                        self::ARGUMENT_PULL_REQUEST_ID,
56
                        InputArgument::REQUIRED,
57
                        'The ID of the pull request'
58
                    ),
59
                    new InputOption(
60
                        self::OPTION_TOKEN,
61
                        't',
62
                        InputOption::VALUE_REQUIRED,
63
                        'The token to authenticate to the API with.'
64
                    ),
65
                ]
66
            )
67
        );
68
    }
69
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $io       = new SymfonyStyle($input, $output);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
73
        $config   = new Client();
74
        $lintTool = new GitHubLintImplementation($config);
75
76
        $authenticationType = Client::AUTH_HTTP_TOKEN;
77
78
        $config->authenticate(
79
            $input->getOption(self::OPTION_TOKEN),
80
            null,
81
            $authenticationType
82
        );
83
84
        $gitHubUsername   = $input->getArgument(self::ARGUMENT_GITHUB_USERNAME);
85
        $gitHubRepository = $input->getArgument(self::ARGUMENT_GITHUB_REPOSITORY);
86
        $pullRequestId    = $input->getArgument(self::ARGUMENT_PULL_REQUEST_ID);
87
        $printableName    = "$gitHubUsername/$gitHubRepository#$pullRequestId";
88
89
90
        $io->title(self::COMMAND_NAME);
91
        $io->comment("Analysing PR $printableName");
92
        $lintTool->analyse(
93
            $gitHubUsername,
94
            $gitHubRepository,
95
            (int)$pullRequestId
96
        );
97
        $io->success("Finished!");
98
    }
99
}
100