-
Notifications
You must be signed in to change notification settings - Fork 1
/
shifter.php
executable file
·200 lines (156 loc) · 5.77 KB
/
shifter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env php
<?php
use CzProject\GitPhp\GitException;
use CzProject\GitPhp\GitRepository;
require_once __DIR__ . '/vendor/autoload.php';
$action = $argv[1] ?? '';
$shifter = new Shifter();
$shifter->execute($action);
class Shifter
{
const REPO_DESCRIPTION = 'Shifter Temporary Repo, can be deleted after shifting';
/**
* @var \Github\Client
*/
protected $gitHub;
/**
* @var string|null
*/
protected $userName = null;
/**
* @var string|null
*/
protected $temporaryRepoName = null;
/**
* @var array
*/
protected $temporaryRepo;
/**
* @var string
*/
protected $currentBranch;
/**
* @var string
*/
protected $token;
public function __construct()
{
$this->gitHub = new \Github\Client();
$this->temporaryRepoName = 'shift';
}
public function execute($action)
{
switch ($action) {
case "push":
$this->authenticate();
$this->ensureTemporaryRepositoryExists();
$this->push();
$this->sayHowToShift();
break;
case "show":
$this->authenticate();
$this->exportPullRequest();
break;
case "clean":
$this->authenticate();
$this->removeTemporaryRepository();
break;
default:
$this->showHelp();
break;
}
}
protected function showHelp()
{
echo 'You can use the following parameters (in the order of the lifecycle)' . PHP_EOL;
echo PHP_EOL;
echo 'shifter push (Step 1)' . PHP_EOL;
echo '(do your shift now)' . PHP_EOL;
echo '(merge back)' . PHP_EOL;
echo 'shifter show > my-shift.md (show the latest merge request, dump to file)' . PHP_EOL;
echo 'shifter clean (remove the repo from github)' . PHP_EOL;
}
protected function authenticate()
{
$tokenFile = __DIR__ . '/.github_token';
$this->token = trim(@file_get_contents($tokenFile));
if ( ! $this->token) {
echo "Go to https://github.com/settings/tokens , create a token for repo access and repo_delete and put the token into $tokenFile" . PHP_EOL;
die();
}
$this->gitHub->authenticate($this->token, '', \Github\Client::AUTH_HTTP_TOKEN);
$this->userName = $this->gitHub->api('current_user')->show()['login'];
}
protected function ensureTemporaryRepositoryExists()
{
try {
$this->temporaryRepo = $this->gitHub->api('repo')->show($this->userName, $this->temporaryRepoName);
if ( ! $this->temporaryRepo ['private']) {
throw \Exception('Refusing to work on public repos.');
}
} catch (\Github\Exception\RuntimeException $exception) {
$this->temporaryRepo = $this->gitHub->api('repo')->create($this->temporaryRepoName,
self::REPO_DESCRIPTION, '', false);
}
}
protected function push()
{
$repoUrl = $this->buildRepoUrl();
$repo = new GitRepository(getcwd());
$this->currentBranch = $repo->getCurrentBranchName();
try {
$repo->removeRemote('shifter');
} catch (GitException $e) {
echo "Not necessary to remove remote" . PHP_EOL;
}
$repo->addRemote('shifter', $repoUrl);
echo 'Pushing...';
$repo->push('shifter');
echo 'done' . PHP_EOL;
}
protected function sayHowToShift()
{
echo "Now please go to https://laravelshift.com/shifts purchase a shift and enter the following repo name:" . PHP_EOL;
echo $this->temporaryRepo['full_name'] . PHP_EOL;
echo "And this branch:" . PHP_EOL;
echo $this->currentBranch . PHP_EOL;
echo "After you are finished, review the merge request at github, and next, merge back into your local repository using" . PHP_EOL;
echo "git fetch shifter && git merge shifter/" . $this->currentBranch . PHP_EOL;
}
protected function removeTemporaryRepository()
{
$this->temporaryRepo = $this->gitHub->api('repo')->show($this->userName, $this->temporaryRepoName);
if ($this->temporaryRepo['description'] != self::REPO_DESCRIPTION) {
throw new Exception('We will work only on repos with the description "' . self::REPO_DESCRIPTION . '"');
}
$this->gitHub->api('repo')->remove($this->userName, $this->temporaryRepo['name']);
echo 'GitHub temporary repository deleted' . PHP_EOL;
$repo = new GitRepository(getcwd());
try {
$repo->removeRemote('shifter');
} catch (GitException $e) {
echo "Not necessary to remove remote" . PHP_EOL;
}
}
protected function exportPullRequest()
{
$issues = $this->gitHub->api('issues')->all($this->userName, $this->temporaryRepoName);
$lastIssue = array_pop($issues);
$result = sprintf('# Pull Request %d', $lastIssue['number']) . PHP_EOL . PHP_EOL;
$result .= $lastIssue['body'] . PHP_EOL . PHP_EOL;;
$comments = $this->gitHub->api('issue')->comments()->all($this->userName, $this->temporaryRepoName, $lastIssue['number']);
foreach($comments as $sequence => $comment) {
$result .= sprintf('## Comment %d', $sequence + 1) . PHP_EOL . PHP_EOL;
$result .= $comment['body'] . PHP_EOL . PHP_EOL;
}
echo $result;
}
/**
* @return string
*/
protected function buildRepoUrl(): string
{
$parts = parse_url($this->temporaryRepo['clone_url']);
return $parts['scheme'] . '://' . $this->userName . ':' . $this->token . '@' . $parts['host'] . $parts['path'];
}
}