Completed
Push — master ( 1e042c...2fd18e )
by Greg
01:27
created

HubphAPI::prCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
3
namespace Hubph;
4
5
use Consolidation\Config\ConfigInterface;
6
7
class HubphAPI
8
{
9
    protected $config;
10
    protected $token;
11
    protected $gitHubAPI;
12
    protected $as = 'default';
13
14
    /**
15
     * HubphAPI constructor
16
     */
17
    public function __construct(ConfigInterface $config)
18
    {
19
        $this->config = $config;
20
    }
21
22
    public function setAs($as)
23
    {
24
        if ($as != $this->as) {
25
            $this->as = $as;
26
            $this->token = false;
27
            $this->gitHubAPI = false;
28
        }
29
    }
30
31
    public function whoami()
32
    {
33
        $gitHubAPI = $this->gitHubAPI();
34
        $authenticated = $gitHubAPI->api('current_user')->show();
35
        return $authenticated;
36
    }
37
38
    public function prCreate($org, $project, $title, $body, $base, $head)
39
    {
40
        $params = [
41
            'title' => $title,
42
            'body' => $body,
43
            'base' => $base,
44
            'head' => $head,
45
        ];
46
        $this->gitHubAPI()->api('pull_request')->create($org, $project, $params);
47
        return $this;
48
    }
49
50
    public function prClose($org, $project, $number)
51
    {
52
        foreach ((array)$number as $n) {
53
            $gitHubAPI = $this->gitHubAPI();
54
            $gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']);
55
        }
56
    }
57
58
    public function prCheck($projectWithOrg, $vids)
59
    {
60
        // Find all of the PRs that contain any vid
61
        $existingPRs = $this->existingPRs($projectWithOrg, $vids);
62
63
        // Check to see if there are PRs matching all of the vids/vvals.
64
        // If so, exit with a message and do nothing.
65
        $titles = $existingPRs->titles();
66
        if ($vids->allExist($titles)) {
67
            return [2, "Pull requests already exist; nothing more to do."];
68
        }
69
70
        // Check to see if there are PRs matching SOME of the vids (with
71
        // or without the matching vvals).  If so, close all that match.
72
        if ($existingPRs->isEmpty()) {
73
            return [0, "No open pull requests that need to be closed."];
74
        }
75
76
        return [0, $existingPRs->prNumbers()];
77
    }
78
79
    public function addTokenAuthentication($url)
80
    {
81
        $token = $this->gitHubToken();
82
        if (!$token) {
83
            return $url;
84
        }
85
        $projectAndOrg = $this->projectAndOrgFromUrl($url);
86
        return "https://{$token}:[email protected]/{$projectAndOrg}.git";
87
    }
88
89 View Code Duplication
    protected function projectAndOrgFromUrl($remote)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $remote = preg_replace('#^git@[^:]*:#', '', $remote);
92
        $remote = preg_replace('#^[^:]*://[^/]*/#', '', $remote);
93
        $remote = preg_replace('#\.git$#', '', $remote);
94
95
        return $remote;
96
    }
97
98
    protected function existingPRs($projectWithOrg, $vids)
99
    {
100
        $preamble = $vids->getPreamble();
101
        $q = "repo:$projectWithOrg in:title is:pr state:open $preamble";
102
        $result = new PullRequests();
103
        $gitHubAPI = $this->gitHubAPI();
104
        $searchResults = $gitHubAPI->api('search')->issues($q);
105
        $result->addSearchResults($searchResults, $vids->pattern());
106
107
        return $result;
108
    }
109
110
    public function allPRs($projectWithOrg)
111
    {
112
        $q = "repo:$projectWithOrg in:title is:pr state:open";
113
        $result = new PullRequests();
114
        $searchResults = $this->gitHubAPI()->api('search')->issues($q);
115
        $result->addSearchResults($searchResults);
116
117
        return $result;
118
    }
119
120
    /**
121
     * Authenticate and then return the gitHub API object.
122
     */
123
    public function gitHubAPI()
124
    {
125
        if (!$this->gitHubAPI) {
126
            $token = $this->gitHubToken();
127
128
            $this->gitHubAPI = new \Github\Client();
129
            $this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN);
130
        }
131
        return $this->gitHubAPI;
132
    }
133
134
    /**
135
     * Look up the GitHub token set either via environment variable or in the
136
     * auth-token cache directory.
137
     */
138
    public function gitHubToken()
139
    {
140
        if (!$this->token) {
141
            $this->token = $this->getGitHubToken();
142
        }
143
        return $this->token;
144
    }
145
146
    protected function getGitHubToken()
147
    {
148
        $as = $this->as;
149
        if ($as == 'default') {
150
            $as = $this->getConfig()->get("github.default-user");
151
        }
152
        $github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path");
153
        if (file_exists($github_token_cache)) {
154
            $token = trim(file_get_contents($github_token_cache));
155
            putenv("GITHUB_TOKEN=$token");
156
        } else {
157
            $token = getenv('GITHUB_TOKEN');
158
        }
159
160
        return $token;
161
    }
162
163
    protected function getConfig()
164
    {
165
        return $this->config;
166
    }
167
}
168