Completed
Push — master ( 523be1...f0b46d )
by Greg
01:23
created

HubphAPI::allPRs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 prClose($org, $project, $number)
39
    {
40
        foreach ((array)$number as $n) {
41
            $gitHubAPI = $this->gitHubAPI();
42
            $gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']);
43
        }
44
    }
45
46
    public function prCheck($projectWithOrg, $vids)
47
    {
48
        // Find all of the PRs that contain any vid
49
        $existingPRs = $this->existingPRs($projectWithOrg, $vids);
50
51
        // Check to see if there are PRs matching all of the vids/vvals.
52
        // If so, exit with a message and do nothing.
53
        $titles = $existingPRs->titles();
54
        if ($vids->allExist($titles)) {
55
            return [2, "Pull requests already exist; nothing more to do."];
56
        }
57
58
        // Check to see if there are PRs matching SOME of the vids (with
59
        // or without the matching vvals).  If so, close all that match.
60
        if ($existingPRs->isEmpty()) {
61
            return [0, "No open pull requests that need to be closed."];
62
        }
63
64
        return [0, $existingPRs->prNumbers()];
65
    }
66
67
    public function addTokenAuthentication($url)
68
    {
69
        $token = $this->gitHubToken();
70
        if (!$token) {
71
            return $url;
72
        }
73
        $projectAndOrg = $this->projectAndOrgFromUrl($url);
74
        return "https://{$token}:[email protected]/{$projectAndOrg}.git";
75
    }
76
77 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...
78
    {
79
        $remote = preg_replace('#^git@[^:]*:#', '', $remote);
80
        $remote = preg_replace('#^[^:]*://[^/]*/#', '', $remote);
81
        $remote = preg_replace('#\.git$#', '', $remote);
82
83
        return $remote;
84
    }
85
86
    protected function existingPRs($projectWithOrg, $vids)
87
    {
88
        $preamble = $vids->getPreamble();
89
        $q = "repo:$projectWithOrg in:title is:pr state:open $preamble";
90
        $result = new PullRequests();
91
        $gitHubAPI = $this->gitHubAPI();
92
        $searchResults = $gitHubAPI->api('search')->issues($q);
93
        $result->addSearchResults($searchResults, $vids->pattern());
94
95
        return $result;
96
    }
97
98
    public function allPRs($projectWithOrg)
99
    {
100
        $q = "repo:$projectWithOrg in:title is:pr state:open";
101
        $result = new PullRequests();
102
        $searchResults = $this->gitHubAPI()->api('search')->issues($q);
103
        $result->addSearchResults($searchResults);
104
105
        return $result;
106
    }
107
108
    /**
109
     * Authenticate and then return the gitHub API object.
110
     */
111
    public function gitHubAPI()
112
    {
113
        if (!$this->gitHubAPI) {
114
            $token = $this->gitHubToken();
115
116
            $this->gitHubAPI = new \Github\Client();
117
            $this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN);
118
        }
119
        return $this->gitHubAPI;
120
    }
121
122
    /**
123
     * Look up the GitHub token set either via environment variable or in the
124
     * auth-token cache directory.
125
     */
126
    public function gitHubToken()
127
    {
128
        if (!$this->token) {
129
            $this->token = $this->getGitHubToken();
130
        }
131
        return $this->token;
132
    }
133
134
    protected function getGitHubToken()
135
    {
136
        $as = $this->as;
137
        if ($as == 'default') {
138
            $as = $this->getConfig()->get("github.default-user");
139
        }
140
        $github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path");
141
        if (file_exists($github_token_cache)) {
142
            $token = trim(file_get_contents($github_token_cache));
143
            putenv("GITHUB_TOKEN=$token");
144
        } else {
145
            $token = getenv('GITHUB_TOKEN');
146
        }
147
148
        return $token;
149
    }
150
151
    protected function getConfig()
152
    {
153
        return $this->config;
154
    }
155
}
156