-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushCommitToGithub.php
215 lines (185 loc) · 5.75 KB
/
PushCommitToGithub.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
class PushCommitToGithub extends AvivUtilController {
public function processRequest() {
$request = $this->getRequest();
$viewer = $request->getUser();
$pwd = '/tmp/phwork/tst__workspace/';
return $this->build(
$this->pushLastCommit($pwd, 'avivey/test-repo', $viewer));
}
public function pushLastCommit($workdir, $github_repo, $viewer) {
$pwd = $workdir;
$future = new ExecFuture(
'/bin/bash -c %s',
'comm -23 <(git ls-tree -r HEAD |sort) <(git ls-tree -r HEAD~ |sort)');
$future->setCwd($pwd);
list($err, $stdout, $stderr) = $future->resolve();
$dd = array('exit'=>$err, 'out'=>$stdout, 'err'=>$stderr);
$parsed = $this->parseGitTree($stdout);
$dd['parsed'] = $parsed;
$new_blobs = array();
foreach ($parsed as $data) {
if ($data['type'] === 'blob') {
$new_blobs[$data['sha']] = $data;
}
}
$access_token = GithubApiCallFuture::getAccessToken($viewer);
$dd['token'] = $access_token;
$futures = array();
foreach ($new_blobs as $blob) {
$content = file_get_contents($pwd.$blob['path']);
$content = base64_encode($content);
$future =
self::makePushBlobFuture('avivey/test-repo', $content, $access_token);
$futures[$blob['sha']] = $future;
}
$repo = 'avivey/test-repo';
$parsed = array();
foreach ($futures as $hash => $future) {
$res = self::parseGithubApiFuture($future);
$res['WIN'] = idx($res['body'], 'sha') == $hash;
$parsed[$hash] = $res;
}
$dd['creat blobs call'] = $parsed;
$future = new ExecFuture(
'/bin/bash -c %s',
'git cat-file commit HEAD');
$future->setCwd($pwd);
$commit_info = $future->resolvex();
$commit_info = $commit_info[0];
$commit_info = $this->parseCommit($commit_info);
$dd['wanted tree hash'] = $commit_info;
$future = new ExecFuture(
'/bin/bash -c %s',
'git ls-tree -r HEAD');
$future->setCwd($pwd);
$head_tree = $future->resolvex();
$head_tree = $this->parseGitTree($head_tree[0]);
$dd['tree'] = json_encode($head_tree);
$future = $this->makeCreateTreeFuture('avivey/test-repo', $head_tree, $access_token);
$dd['create tree call'] = self::parseGithubApiFuture($future);
// todo parse author info + time.
$future = $this->makeCommitFuture(
'avivey/test-repo',
$commit_info['message'],
$commit_info['tree'],
$commit_info['parent'],
array(), // TODO add author data.
$access_token
);
$commit = self::parseGithubApiFuture($future);
$dd['create make commit'] = $commit;
$commit = idx($commit, 'body');
$commit = idx($commit, 'sha');
if ($commit) {
$future = $this->makeUpdateMasterFuture($repo, $commit, $access_token);
$dd['update'] = self::parseGithubApiFuture($future);
}
return $dd;
}
function makeCommitFuture($repo, $message, $tree, $parent, array $more, $access_token) {
// todo add committer data
// todo get information from phabricator commit object.
$author = idx($more, 'author');
$data = array(
'message' => $message,
'tree' => $tree,
'parents' => array($parent),
);
if ($author) {
$data['author'] = $author;
}
$future = new GithubApiCallFuture(
"repos/$repo/git/commits",
$access_token,
$data);
$future->setMethod('POST');
return $future->start();
}
function makeUpdateMasterFuture($repo, $commit, $access_token) {
$data = array('sha' => $commit);
$future = new GithubApiCallFuture(
"repos/$repo/git/refs/heads/master",
$access_token,
$data);
$future->setMethod('PATCH');
return $future->start();
}
function makeCreateTreeFuture($repo, $tree, $access_token) {
$data = array('tree' => $tree);
$future = new GithubApiCallFuture(
"repos/$repo/git/trees",
$access_token,
$data);
$future->setMethod('POST');
return $future->start();
}
function makePushBlobFuture($repo, $blob_in_base64, $access_token) {
$data = array(
'encoding' => 'base64',
'content' => $blob_in_base64,
);
$future = new GithubApiCallFuture(
"repos/$repo/git/blobs",
$access_token,
$data);
$future->setMethod('POST');
$future->start();
return $future;
}
function parseGithubApiFuture($future) {
$dd = array();
// $dd['uri'] = ''.$future->uri;
list($status, $body, $headers) = $future->resolve();
$dd['api status'] = $status->getStatusCode();
// $dd['headers'] = $headers;
$dd['body'] = json_decode($body, true);
return $dd;
}
private function parseGitTree($stdout) {
$result = array();
$stdout = trim($stdout);
if (!strlen($stdout)) {
return $result;
}
$lines = explode("\n", $stdout);
foreach ($lines as $line) {
$matches = array();
$ok = preg_match(
'/^(\d{6}) (blob|tree|commit) ([a-z0-9]{40})[\t](.*)$/',
$line,
$matches);
if (!$ok) {
throw new Exception("Failed to parse git ls-tree output!");
}
// this now matches github's api.
$result[] = array(
'mode' => $matches[1],
'type' => $matches[2],
'sha' => $matches[3],
'path' => $matches[4],
);
}
return $result;
}
function parseCommit($commit) {
$commit = trim($commit);
$text = explode("\n", $commit);
$data = array();
$message = array();
$in_message = false;
while(!empty($text)) {
$line = array_shift($text);
if ($in_message) {
array_push($message, $line);
} else if (empty($line)) {
$in_message = true;
} else {
$line = explode(' ', $line, 2);
$data[$line[0]] = $line[1];
}
}
$data['message'] = implode("\n", $message);
return $data;
}
}