EvaluatePullRequest::configure()   B
last analyzed

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
/**
17
 * A command that allows you to try out the library
18
 *
19
 * @package PurpleBooth\GitGitHubLint\Command
20
 */
21
class EvaluatePullRequest extends Command
22
{
23
    const ARGUMENT_GITHUB_USERNAME   = 'github-username';
24
    const ARGUMENT_GITHUB_REPOSITORY = 'github-repository';
25
    const ARGUMENT_PULL_REQUEST_ID   = 'pull-request-id';
26
    const OPTION_TOKEN               = 'token';
27
    const COMMAND_NAME               = 'git-github-lint:pr';
28
29
    /**
30
     * Configures the current command.
31
     */
32
    protected function configure()
33
    {
34
        $this->setName(self::COMMAND_NAME);
35
        $this->setDescription("Check the style of commit messages in a pull request");
36
37
        $help  = '';
38
        $help .= "Evaluates a the commits in a pull request and checks that their messages match the style advised by ";
39
        $help .= "Git. It will then update the \"status\" in github (that little dot next to the commits).\n";
40
        $help .= "\n";
41
        $help .= "\n";
42
        $help .= "Here are some good articles on commit message style:\n";
43
        $help .= "\n";
44
        $help .= "* http://chris.beams.io/posts/git-commit/\n";
45
        $help .= "* https://git-scm.com/book/ch5-2.html#Commit-Guidelines\n";
46
        $help .= "* https://github.com/blog/926-shiny-new-commit-styles\n";
47
48
        $this->setHelp($help);
49
        $this->setDefinition(
50
            new InputDefinition(
51
                [
52
                    new InputArgument(
53
                        self::ARGUMENT_GITHUB_USERNAME,
54
                        InputArgument::REQUIRED,
55
                        'GitHub Username'
56
                    ),
57
                    new InputArgument(
58
                        self::ARGUMENT_GITHUB_REPOSITORY,
59
                        InputArgument::REQUIRED,
60
                        'GitHub Repository'
61
                    ),
62
                    new InputArgument(
63
                        self::ARGUMENT_PULL_REQUEST_ID,
64
                        InputArgument::REQUIRED,
65
                        'The ID of the pull request'
66
                    ),
67
                    new InputOption(
68
                        self::OPTION_TOKEN,
69
                        't',
70
                        InputOption::VALUE_REQUIRED,
71
                        'The token to authenticate to the API with.'
72
                    ),
73
                ]
74
            )
75
        );
76
    }
77
78
    /**
79
     * Executes the current command.
80
     *
81
     * This method is not abstract because you can use this class
82
     * as a concrete class. In this case, instead of defining the
83
     * execute() method, you set the code to execute by passing
84
     * a Closure to the setCode() method.
85
     *
86
     * @param InputInterface  $input  An InputInterface instance
87
     * @param OutputInterface $output An OutputInterface instance
88
     *
89
     * @return null|int null or 0 if everything went fine, or an error code
90
     *
91
     * @see setCode()
92
     */
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        $styleHelper = new SymfonyStyle($input, $output);
96
        $config      = new Client();
97
        $lintTool    = new GitHubLintImplementation($config);
98
99
        $authenticationType = Client::AUTH_HTTP_TOKEN;
100
101
        $config->authenticate(
102
            $input->getOption(self::OPTION_TOKEN),
103
            null,
104
            $authenticationType
105
        );
106
107
        $gitHubUsername   = $input->getArgument(self::ARGUMENT_GITHUB_USERNAME);
108
        $gitHubRepository = $input->getArgument(self::ARGUMENT_GITHUB_REPOSITORY);
109
        $pullRequestId    = $input->getArgument(self::ARGUMENT_PULL_REQUEST_ID);
110
        $printableName    = "$gitHubUsername/$gitHubRepository#$pullRequestId";
111
112
113
        $styleHelper->title(self::COMMAND_NAME);
114
        $styleHelper->comment("Analysing PR $printableName");
115
        $lintTool->analyse(
116
            $gitHubUsername,
117
            $gitHubRepository,
118
            (int)$pullRequestId
119
        );
120
        $styleHelper->success("Finished!");
121
122
        return 0;
123
    }
124
}
125