Releases::createRelease()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 6
1
<?php
2
namespace FlexyProject\GitHub\Receiver\Repositories;
3
4
use FlexyProject\GitHub\AbstractApi;
5
use FlexyProject\GitHub\Receiver\Repositories;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * The Releases API class provides access to repository's releases.
10
 *
11
 * @link    https://developer.github.com/v3/repos/releases/
12
 * @package FlexyProject\GitHub\Receiver\Repositories
13
 */
14
class Releases extends AbstractRepositories
15
{
16
17
    /**
18
     * List releases for a repository
19
     *
20
     * @link https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
21
     * @return array
22
     */
23
    public function listReleases(): array
24
    {
25
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases',
26
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo()));
27
    }
28
29
    /**
30
     * Get a single release
31
     *
32
     * @link https://developer.github.com/v3/repos/releases/#get-a-single-release
33
     *
34
     * @param string $id
35
     *
36
     * @return array
37
     */
38
    public function getSingleRelease(string $id): array
39
    {
40
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/:id',
41
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id));
42
    }
43
44
    /**
45
     * Get the latest release
46
     *
47
     * @link https://developer.github.com/v3/repos/releases/#get-the-latest-release
48
     * @return array
49
     * @throws \Exception
50
     */
51
    public function getLatestRelease(): array
52
    {
53
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/latest',
54
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo()));
55
    }
56
57
    /**
58
     * Get a release by tag name
59
     *
60
     * @link https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
61
     *
62
     * @param string $tag
63
     *
64
     * @return array
65
     * @throws \Exception
66
     */
67
    public function getReleaseByTagName(string $tag): array
68
    {
69
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/tags/:tag',
70
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $tag));
71
    }
72
73
    /**
74
     * Create a release
75
     *
76
     * @link https://developer.github.com/v3/repos/releases/#create-a-release
77
     *
78
     * @param string $tagName
79
     * @param string $targetCommitish
80
     * @param string $name
81
     * @param string $body
82
     * @param bool   $draft
83
     * @param bool   $preRelease
84
     *
85
     * @return array
86
     */
87 View Code Duplication
    public function createRelease(string $tagName, string $targetCommitish = AbstractApi::BRANCH_MASTER,
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...
88
                                  string $name = null, string $body = null, bool $draft = false,
89
                                  bool $preRelease = false): array
90
    {
91
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases',
92
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo()), Request::METHOD_POST, [
93
                'tag_name'         => $tagName,
94
                'target_commitish' => $targetCommitish,
95
                'name'             => $name,
96
                'body'             => $body,
97
                'draft'            => $draft,
98
                'prerelease'       => $preRelease
99
            ]);
100
    }
101
102
    /**
103
     * Edit a release
104
     *
105
     * @link https://developer.github.com/v3/repos/releases/#edit-a-release
106
     *
107
     * @param string $id
108
     * @param string $tagName
109
     * @param string $targetCommitish
110
     * @param string $name
111
     * @param string $body
112
     * @param bool   $draft
113
     * @param bool   $preRelease
114
     *
115
     * @return array
116
     */
117
    public function editRelease(string $id, string $tagName, string $targetCommitish = AbstractApi::BRANCH_MASTER,
118
                                string $name, string $body, bool $draft = false, bool $preRelease = false): array
119
    {
120
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/:id',
121
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id), Request::METHOD_PATCH, [
122
                'tag_name'         => $tagName,
123
                'target_commitish' => $targetCommitish,
124
                'name'             => $name,
125
                'body'             => $body,
126
                'draft'            => $draft,
127
                'prerelease'       => $preRelease
128
            ]);
129
    }
130
131
    /**
132
     * Delete a release
133
     *
134
     * @link https://developer.github.com/v3/repos/releases/#delete-a-release
135
     *
136
     * @param string $id
137
     *
138
     * @return bool
139
     */
140
    public function deleteRelease(string $id): bool
141
    {
142
        $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/:id',
143
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id), Request::METHOD_DELETE);
144
145
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
146
            return true;
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * List assets for a release
154
     *
155
     * @link https://developer.github.com/v3/repos/releases/#list-assets-for-a-release
156
     *
157
     * @param string $id
158
     *
159
     * @return array
160
     */
161
    public function getReleaseAssets(string $id): array
162
    {
163
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/:id/assets',
164
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id));
165
    }
166
167
    /**
168
     * Upload a release asset
169
     *
170
     * @link https://developer.github.com/v3/repos/releases/#upload-a-release-asset
171
     *
172
     * @param string $id
173
     * @param string $contentType
174
     * @param string $name
175
     *
176
     * @return array
177
     */
178
    public function uploadReleaseAsset(string $id, string $contentType, string $name): array
179
    {
180
181
        return $this->getApi()->setApiUrl(AbstractApi::API_UPLOADS)->request($this->getApi()
182
                                                                                  ->sprintf('/repos/:owner/:repo/releases/:id/assets?:arg',
183
                                                                                      $this->getRepositories()
184
                                                                                           ->getOwner(),
185
                                                                                      $this->getRepositories()
186
                                                                                           ->getRepo(), $id,
187
                                                                                      http_build_query(['name' => $name])),
188
            Request::METHOD_POST, [
189
                'Content-Type' => $contentType
190
            ]);
191
    }
192
193
    /**
194
     * Get a single release asset
195
     *
196
     * @link https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
197
     *
198
     * @param string $id
199
     *
200
     * @return array
201
     */
202
    public function getSingleReleaseAsset(string $id): array
203
    {
204
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/assets/:id',
205
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id));
206
    }
207
208
    /**
209
     * Edit a release asset
210
     *
211
     * @link https://developer.github.com/v3/repos/releases/#edit-a-release-asset
212
     *
213
     * @param string $id
214
     * @param string $name
215
     * @param string $label
216
     *
217
     * @return array
218
     */
219
    public function editReleaseAsset(string $id, string $name, string $label = ''): array
220
    {
221
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/assets/:id',
222
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id), Request::METHOD_PATCH, [
223
                'name'  => $name,
224
                'label' => $label
225
            ]);
226
    }
227
228
    /**
229
     * Delete a release asset
230
     *
231
     * @link https://developer.github.com/v3/repos/releases/#delete-a-release-asset
232
     *
233
     * @param string $id
234
     *
235
     * @return bool
236
     */
237
    public function deleteReleaseAsset(string $id): bool
238
    {
239
        $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/releases/assets/:id',
240
            $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $id), Request::METHOD_DELETE);
241
242
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
243
            return true;
244
        }
245
246
        return false;
247
    }
248
}