1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by PhpStorm, User: jonphipps, Date: 2018-02-17, Time: 6:48 PM */ |
4
|
|
|
|
5
|
|
|
namespace App\Services\Publish; |
6
|
|
|
|
7
|
|
|
use App\Models\Release; |
8
|
|
|
use Github\Client as GitHubClient; |
9
|
|
|
use Github\Exception\RuntimeException; |
10
|
|
|
use GrahamCampbell\GitHub\Facades\GitHub; |
11
|
|
|
|
12
|
|
|
class GitHubService |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \App\Models\Release |
16
|
|
|
*/ |
17
|
|
|
private $release; |
18
|
|
|
private $owner; |
19
|
|
|
private $repo; |
20
|
|
|
private $github_id; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* GitHubService constructor. |
24
|
|
|
* |
25
|
|
|
* @param \App\Models\Release $release |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Release $release) |
28
|
|
|
{ |
29
|
|
|
$this->release = $release; |
30
|
|
|
$path = $release->project->repo; |
31
|
|
|
if ($path) { |
32
|
|
|
$this->owner = str_before($path, '/'); |
33
|
|
|
$this->repo = str_after($path, '/'); |
34
|
|
|
$this->github_id = $release->github_id; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $path |
40
|
|
|
* |
41
|
|
|
* @return string|bool json |
42
|
|
|
*/ |
43
|
|
|
public static function GetPublicRepo($path) |
44
|
|
|
{ |
45
|
|
|
$owner = str_before($path, '/'); |
46
|
|
|
$repo = str_after($path, '/'); |
47
|
|
|
try { |
48
|
|
|
return GitHub::repo()->show($owner, $repo); |
49
|
|
|
} |
50
|
|
|
catch (RuntimeException $e) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getRelease() |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
return GitHub::repo()->releases()->show($this->owner, $this->repo, $this->github_id); |
59
|
|
|
} |
60
|
|
|
catch (RuntimeException $e) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \App\Models\Release |
67
|
|
|
* @throws \Github\Exception\InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
public function setRelease() |
70
|
|
|
{ |
71
|
|
|
//GET /repos/:owner/:repo/releases/tags/:tag |
72
|
|
|
|
73
|
|
|
$content = [ |
74
|
|
|
'tag_name' => $this->release->tag_name, |
75
|
|
|
'target_commitish' => $this->release->target_commitish, |
76
|
|
|
'name' => $this->release->name, |
77
|
|
|
'body' => $this->release->body, |
78
|
|
|
'draft' => $this->release->is_draft ?? false, |
79
|
|
|
'prerelease' => $this->release->is_prerelease ?? false, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$client = new GitHubClient(); |
83
|
|
|
$token = auth()->user()->githubToken; |
84
|
|
|
$nickname = auth()->user()->nickname; |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
$client->authenticate($nickname, $token, GitHubClient::AUTH_HTTP_PASSWORD); |
88
|
|
|
|
89
|
|
|
if ($this->github_id) { //if there is a release, update it |
90
|
|
|
$gitHub = $client->api('repo')->releases()->edit($this->owner, $this->repo, $this->github_id, $content); |
91
|
|
|
} else { //if there's no release, make a new one |
92
|
|
|
$gitHub = $client->api('repo')->releases()->create($this->owner, $this->repo, $content); |
93
|
|
|
//we're going to want to store the GitHub release ID in the record |
94
|
|
|
$this->release->github_id = $gitHub->id; |
95
|
|
|
} |
96
|
|
|
$this->release->github_response = $gitHub; |
97
|
|
|
//need to do something special with changing prerelease to release here |
98
|
|
|
$this->release->save(); |
99
|
|
|
|
100
|
|
|
return $this->release; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|