• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eiriksm / cosy-composer / 6538835069

16 Oct 2023 08:18PM UTC coverage: 84.19% (-0.8%) from 84.949%
6538835069

Pull #306

github

eiriksm
Allow phpunit 9
Pull Request #306: Allow phpunit 9

1523 of 1809 relevant lines covered (84.19%)

71.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.12
/src/Providers/Github.php
1
<?php
2

3
namespace eiriksm\CosyComposer\Providers;
4

5
use eiriksm\CosyComposer\ProviderInterface;
6
use Github\Api\Issue;
7
use Github\Api\PullRequest;
8
use Github\Client;
9
use Github\ResultPager;
10
use Violinist\Slug\Slug;
11

12
class Github implements ProviderInterface
13
{
14

15
    private $cache;
16

17
    private $client;
18

19
    public function __construct(Client $client)
20
    {
21
        $this->client = $client;
56✔
22
    }
23

24
    public function addLabels(array $pr_data, Slug $slug, array $labels) : bool
25
    {
26
        if (!isset($pr_data["number"])) {
×
27
            return false;
×
28
        }
29
        try {
30
            $data = $this->client->issue()->labels()->add($slug->getUserName(), $slug->getUserRepo(), $pr_data['number'], $labels);
×
31
            if (empty($data[0]["id"])) {
×
32
                return false;
×
33
            }
34
            return true;
×
35
        } catch (\Throwable $e) {
×
36
            return false;
×
37
        }
38
    }
39

40
    public function enableAutomerge(array $pr_data, Slug $slug) : bool
41
    {
42
        if (!isset($pr_data["node_id"])) {
×
43
            return false;
×
44
        }
45
        $data = $this->client->graphql()->execute('mutation MyMutation ($input: EnablePullRequestAutoMergeInput!) {
×
46
  enablePullRequestAutoMerge(input: $input) {
47
    pullRequest {
48
      id
49
    }
50
  }
51
}', [
×
52
        'input' => [
×
53
            'pullRequestId' => $pr_data['node_id']
×
54
        ]
×
55
        ]);
×
56
        if (!empty($data["errors"])) {
×
57
            return false;
×
58
        }
59
        return true;
×
60
    }
61

62
    public function authenticate($user, $token)
63
    {
64
        $this->client->authenticate($user, null, Client::AUTH_ACCESS_TOKEN);
2✔
65
    }
66

67
    public function authenticatePrivate($user, $token)
68
    {
69
        $this->client->authenticate($user, null, Client::AUTH_ACCESS_TOKEN);
2✔
70
    }
71

72
    public function repoIsPrivate(Slug $slug)
73
    {
74
        $user = $slug->getUserName();
4✔
75
        $repo = $slug->getUserRepo();
4✔
76
        if (!isset($this->cache['repo'])) {
4✔
77
            $this->cache['repo'] = $this->client->api('repo')->show($user, $repo);
4✔
78
        }
79
        return (bool) $this->cache['repo']['private'];
4✔
80
    }
81

82
    public function getDefaultBranch(Slug $slug)
83
    {
84
        $user = $slug->getUserName();
2✔
85
        $repo = $slug->getUserRepo();
2✔
86
        if (!isset($this->cache['repo'])) {
2✔
87
            $this->cache['repo'] = $this->client->api('repo')->show($user, $repo);
2✔
88
        }
89
        return $this->cache['repo']['default_branch'];
2✔
90
    }
91

92
    protected function getBranches($user, $repo)
93
    {
94
        if (!isset($this->cache['branches'])) {
4✔
95
            $pager = new ResultPager($this->client);
4✔
96
            $api = $this->client->api('repo');
4✔
97
            $method = 'branches';
4✔
98
            $this->cache['branches'] = $pager->fetchAll($api, $method, [$user, $repo]);
4✔
99
        }
100
        return $this->cache['branches'];
4✔
101
    }
102

103
    public function getBranchesFlattened(Slug $slug)
104
    {
105
        $user = $slug->getUserName();
2✔
106
        $repo = $slug->getUserRepo();
2✔
107
        $branches = $this->getBranches($user, $repo);
2✔
108

109
        $branches_flattened = [];
2✔
110
        foreach ($branches as $branch) {
2✔
111
            $branches_flattened[] = $branch['name'];
2✔
112
        }
113
        return $branches_flattened;
2✔
114
    }
115

116
    public function getPrsNamed(Slug $slug) : array
117
    {
118
        $user = $slug->getUserName();
2✔
119
        $repo = $slug->getUserRepo();
2✔
120
        $pager = new ResultPager($this->client);
2✔
121
        $api = $this->client->api('pr');
2✔
122
        $method = 'all';
2✔
123
        $prs = $pager->fetchAll($api, $method, [$user, $repo]);
2✔
124
        $prs_named = [];
2✔
125
        foreach ($prs as $pr) {
2✔
126
            $prs_named[$pr['head']['ref']] = $pr;
2✔
127
        }
128
        return $prs_named;
2✔
129
    }
130

131
    public function getDefaultBase(Slug $slug, $default_branch)
132
    {
133
        $user = $slug->getUserName();
2✔
134
        $repo = $slug->getUserRepo();
2✔
135
        $branches = $this->getBranches($user, $repo);
2✔
136
        $default_base = null;
2✔
137
        foreach ($branches as $branch) {
2✔
138
            if ($branch['name'] == $default_branch) {
2✔
139
                $default_base = $branch['commit']['sha'];
2✔
140
            }
141
        }
142
        return $default_base;
2✔
143
    }
144

145
    public function createFork($user, $repo, $fork_user)
146
    {
147
        return $this->client->api('repo')->forks()->create($user, $repo, [
2✔
148
          'organization' => $fork_user,
2✔
149
        ]);
2✔
150
    }
151

152
    public function createPullRequest(Slug $slug, $params)
153
    {
154
        $user_name = $slug->getUserName();
2✔
155
        $user_repo = $slug->getUserRepo();
2✔
156
        /** @var PullRequest $prs */
157
        $prs = $this->client->api('pull_request');
2✔
158
        $data = $prs->create($user_name, $user_repo, $params);
2✔
159
        if (!empty($params['assignees'])) {
2✔
160
            // Now try to update it with assignees.
161
            try {
162
                /** @var Issue $issues */
163
                $issues = $this->client->api('issues');
×
164
                $issues->update($user_name, $user_repo, $data['number'], [
×
165
                    'assignees' => $params['assignees'],
×
166
                ]);
×
167
            } catch (\Exception $e) {
×
168
                // Too bad.
169
                //  @todo: Should be possible to inject a logger and log this.
170
            }
171
        }
172
        return $data;
2✔
173
    }
174

175
    public function updatePullRequest(Slug $slug, $id, $params)
176
    {
177
        $user_name = $slug->getUserName();
2✔
178
        $user_repo = $slug->getUserRepo();
2✔
179
        return $this->client->api('pull_request')->update($user_name, $user_repo, $id, $params);
2✔
180
    }
181

182
    public function closePullRequestWithComment(Slug $slug, $pr_id, $comment)
183
    {
184
        $this->client->issue()->comments()->create($slug->getUserName(), $slug->getUserRepo(), $pr_id, [
×
185
            'body' => $comment,
×
186
        ]);
×
187
        $this->client->api('pull_request')->update($slug->getUserName(), $slug->getUserRepo(), $pr_id, [
×
188
            'state' => 'closed',
×
189
        ]);
×
190
    }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc