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

eiriksm / cosy-composer / 16469015360

23 Jul 2025 11:10AM UTC coverage: 86.578% (-0.4%) from 86.939%
16469015360

push

github

web-flow
More accurate parsing of named prs  (#427)

82 of 102 new or added lines in 9 files covered. (80.39%)

1 existing line in 1 file now uncovered.

2019 of 2332 relevant lines covered (86.58%)

44.84 hits per line

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

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

3
namespace eiriksm\CosyComposer\Providers;
4

5
use eiriksm\CosyComposer\ProviderInterface;
6
use Github\AuthMethod;
7
use Github\Client;
8
use Github\ResultPager;
9
use Violinist\Slug\Slug;
10

11
class Github implements ProviderInterface
12
{
13

14
    private $cache;
15

16
    private $client;
17

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

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

39
    public function enableAutomerge(array $pr_data, Slug $slug, $merge_method = self::MERGE_METHOD_MERGE) : bool
40
    {
41
        if (!isset($pr_data["node_id"])) {
1✔
42
            return false;
×
43
        }
44
        // https://docs.github.com/en/graphql/reference/enums#pullrequestmergemethod
45
        $api_merge_method = 'MERGE';
1✔
46
        if ($merge_method === self::MERGE_METHOD_REBASE) {
1✔
47
            $api_merge_method = 'REBASE';
×
48
        }
49
        if ($merge_method === self::MERGE_METHOD_SQUASH) {
1✔
50
            $api_merge_method = 'SQUASH';
×
51
        }
52
        $data = $this->client->api('graphql')->execute('mutation MyMutation ($input: EnablePullRequestAutoMergeInput!) {
1✔
53
  enablePullRequestAutoMerge(input: $input) {
54
    pullRequest {
55
      id
56
    }
57
  }
58
}', [
1✔
59
        'input' => [
1✔
60
            'pullRequestId' => $pr_data['node_id'],
1✔
61
            'mergeMethod' => $api_merge_method,
1✔
62
        ],
1✔
63
        ]);
1✔
64
        if (!empty($data["errors"])) {
1✔
65
            return false;
×
66
        }
67
        return true;
1✔
68
    }
69

70
    public function authenticate($user, $token)
71
    {
72
        $this->client->authenticate($user, null, AuthMethod::ACCESS_TOKEN);
1✔
73
    }
74

75
    public function authenticatePrivate($user, $token)
76
    {
77
        $this->client->authenticate($user, null, AuthMethod::ACCESS_TOKEN);
1✔
78
    }
79

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

90
    public function getDefaultBranch(Slug $slug)
91
    {
92
        $user = $slug->getUserName();
1✔
93
        $repo = $slug->getUserRepo();
1✔
94
        if (!isset($this->cache['repo'])) {
1✔
95
            $this->cache['repo'] = $this->client->api('repo')->show($user, $repo);
1✔
96
        }
97
        return $this->cache['repo']['default_branch'];
1✔
98
    }
99

100
    protected function getBranches($user, $repo)
101
    {
102
        if (!isset($this->cache['branches'])) {
2✔
103
            $pager = new ResultPager($this->client);
2✔
104
            $api = $this->client->api('repo');
2✔
105
            $method = 'branches';
2✔
106
            $this->cache['branches'] = $pager->fetchAll($api, $method, [$user, $repo]);
2✔
107
        }
108
        return $this->cache['branches'];
2✔
109
    }
110

111
    public function getBranchesFlattened(Slug $slug)
112
    {
113
        $user = $slug->getUserName();
1✔
114
        $repo = $slug->getUserRepo();
1✔
115
        $branches = $this->getBranches($user, $repo);
1✔
116

117
        $branches_flattened = [];
1✔
118
        foreach ($branches as $branch) {
1✔
119
            $branches_flattened[] = $branch['name'];
1✔
120
        }
121
        return $branches_flattened;
1✔
122
    }
123

124
    public function getPrsNamed(Slug $slug) : NamedPrs
125
    {
126
        $user = $slug->getUserName();
1✔
127
        $repo = $slug->getUserRepo();
1✔
128
        $pager = new ResultPager($this->client);
1✔
129
        $api = $this->client->api('pr');
1✔
130
        $method = 'all';
1✔
131
        $prs = $pager->fetchAll($api, $method, [$user, $repo]);
1✔
132
        $prs_named = new NamedPrs();
1✔
133
        foreach ($prs as $pr) {
1✔
134
            $prs_named->addFromPrData($pr);
1✔
135
            // Attempt to retrieve the actual commit.
136
            try {
137
                /** @var \Github\Api\Repository\Commits $commits */
138
                $commits = $this->client->api('repo')->commits();
1✔
NEW
139
                $commit = $commits->show($user, $repo, $pr['head']['sha']);
×
NEW
140
                if (!empty($commit["commit"]["message"])) {
×
NEW
141
                    $prs_named->addFromCommit($commit["commit"]["message"], $pr);
×
142
                }
143
            } catch (\Exception $e) {
1✔
144
                // If the commit is not found, we just skip it.
145
            }
146
        }
147
        return $prs_named;
1✔
148
    }
149

150
    public function getDefaultBase(Slug $slug, $default_branch)
151
    {
152
        $user = $slug->getUserName();
1✔
153
        $repo = $slug->getUserRepo();
1✔
154
        $branches = $this->getBranches($user, $repo);
1✔
155
        $default_base = null;
1✔
156
        foreach ($branches as $branch) {
1✔
157
            if ($branch['name'] == $default_branch) {
1✔
158
                $default_base = $branch['commit']['sha'];
1✔
159
            }
160
        }
161
        return $default_base;
1✔
162
    }
163

164
    public function createFork($user, $repo, $fork_user)
165
    {
166
        return $this->client->api('repo')->forks()->create($user, $repo, [
1✔
167
          'organization' => $fork_user,
1✔
168
        ]);
1✔
169
    }
170

171
    public function createPullRequest(Slug $slug, $params)
172
    {
173
        $user_name = $slug->getUserName();
1✔
174
        $user_repo = $slug->getUserRepo();
1✔
175
        /** @var \Github\Api\PullRequest $prs */
176
        $prs = $this->client->api('pull_request');
1✔
177
        $data = $prs->create($user_name, $user_repo, $params);
1✔
178
        if (!empty($params['assignees'])) {
1✔
179
            // Now try to update it with assignees.
180
            try {
181
                /** @var \Github\Api\Issue $issues */
182
                $issues = $this->client->api('issues');
×
183
                $issues->update($user_name, $user_repo, $data['number'], [
×
184
                    'assignees' => $params['assignees'],
×
185
                ]);
×
186
            } catch (\Exception $e) {
×
187
                // Too bad.
188
                //  @todo: Should be possible to inject a logger and log this.
189
            }
190
        }
191
        return $data;
1✔
192
    }
193

194
    public function updatePullRequest(Slug $slug, $id, $params)
195
    {
196
        $user_name = $slug->getUserName();
1✔
197
        $user_repo = $slug->getUserRepo();
1✔
198
        return $this->client->api('pull_request')->update($user_name, $user_repo, $id, $params);
1✔
199
    }
200

201
    public function closePullRequestWithComment(Slug $slug, $pr_id, $comment)
202
    {
203
        $this->client->issue()->comments()->create($slug->getUserName(), $slug->getUserRepo(), $pr_id, [
×
204
            'body' => $comment,
×
205
        ]);
×
206
        $this->client->api('pull_request')->update($slug->getUserName(), $slug->getUserRepo(), $pr_id, [
×
207
            'state' => 'closed',
×
208
        ]);
×
209
    }
210
}
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

© 2025 Coveralls, Inc