This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompanion.rs
313 lines (305 loc) · 7.11 KB
/
companion.rs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use regex::Regex;
use snafu::ResultExt;
use tokio::process::Command;
use crate::{error::*, github_bot::GithubBot, Result};
pub async fn companion_update(
github_bot: &GithubBot,
base_owner: &str,
base_repo: &str,
head_owner: &str,
head_repo: &str,
branch: &str,
) -> Result<Option<String>> {
let res = companion_update_inner(
github_bot, base_owner, base_repo, head_owner, head_repo, branch,
)
.await;
// checkout origin master
log::info!("Checking out master.");
Command::new("git")
.arg("checkout")
.arg("master")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// delete temp branch
log::info!("Deleting head branch.");
Command::new("git")
.arg("branch")
.arg("-D")
.arg(format!("{}", branch))
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// remove temp remote
log::info!("Removing temp remote.");
Command::new("git")
.arg("remote")
.arg("remove")
.arg("temp")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
res
}
async fn companion_update_inner(
github_bot: &GithubBot,
base_owner: &str,
base_repo: &str,
head_owner: &str,
head_repo: &str,
branch: &str,
) -> Result<Option<String>> {
let token = github_bot.client.auth_key().await?;
let mut updated_sha = None;
// clone in case the local clone doesn't exist
log::info!("Cloning repo.");
Command::new("git")
.arg("clone")
.arg("-v")
.arg(format!(
"https://x-access-token:{token}@github.com/{owner}/{repo}.git",
token = token,
owner = base_owner,
repo = base_repo,
))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// add temp remote
log::info!("Adding temp remote.");
Command::new("git")
.arg("remote")
.arg("add")
.arg("temp")
.arg(format!(
"https://x-access-token:{token}@github.com/{owner}/{repo}.git",
token = token,
owner = head_owner,
repo = head_repo,
))
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// fetch temp
log::info!("Fetching temp.");
Command::new("git")
.arg("fetch")
.arg("temp")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// checkout temp branch
log::info!("Checking out head branch.");
let checkout = Command::new("git")
.arg("checkout")
.arg("-b")
.arg(format!("{}", branch))
.arg(format!("temp/{}", branch))
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
if checkout.success() {
// merge origin master
log::info!("Merging master.");
let merge_master = Command::new("git")
.arg("merge")
.arg("origin/master")
.arg("--no-ff")
.arg("--no-edit")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
if merge_master.success() {
// update
log::info!("Updating substrate.");
Command::new("cargo")
.arg("update")
.arg("-vp")
.arg("sp-io")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// commit
log::info!("Committing changes.");
Command::new("git")
.arg("commit")
.arg("-am")
.arg("\"Update Substrate\"")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// push
log::info!("Pushing changes.");
Command::new("git")
.arg("push")
.arg("temp")
.arg(format!("{}", branch))
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
// rev-parse
log::info!("Parsing SHA.");
let output = Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.current_dir(format!("./{}", base_repo))
.output()
.await
.context(Tokio)?;
updated_sha = Some(
String::from_utf8(output.stdout)
.context(Utf8)?
.trim()
.to_string(),
);
} else {
// abort merge
log::info!("Aborting merge.");
Command::new("git")
.arg("merge")
.arg("--abort")
.current_dir(format!("./{}", base_repo))
.spawn()
.context(Tokio)?
.await
.context(Tokio)?;
}
}
Ok(updated_sha)
}
pub fn companion_parse(body: &str) -> Option<(String, String, String, i64)> {
companion_parse_long(body).or(companion_parse_short(body))
}
fn companion_parse_long(body: &str) -> Option<(String, String, String, i64)> {
let re = Regex::new(
r"companion.*(?P<html_url>https://github.com/(?P<owner>[[:alpha:]]+)/(?P<repo>[[:alpha:]]+)/pull/(?P<number>[[:digit:]]+))"
)
.unwrap();
let caps = re.captures(&body)?;
let html_url = caps.name("html_url")?.as_str().to_owned();
let owner = caps.name("owner")?.as_str().to_owned();
let repo = caps.name("repo")?.as_str().to_owned();
let number = caps
.name("number")?
.as_str()
.to_owned()
.parse::<i64>()
.ok()?;
Some((html_url, owner, repo, number))
}
fn companion_parse_short(body: &str) -> Option<(String, String, String, i64)> {
let re = Regex::new(
r"companion.*: (?P<owner>[[:alpha:]]+)/(?P<repo>[[:alpha:]]+)#(?P<number>[[:digit:]]+)"
)
.unwrap();
let caps = re.captures(&body)?;
let owner = caps.name("owner")?.as_str().to_owned();
let repo = caps.name("repo")?.as_str().to_owned();
let number = caps
.name("number")?
.as_str()
.to_owned()
.parse::<i64>()
.ok()?;
let html_url = format!(
"https://github.com/{owner}/{repo}/pull/{number}",
owner = owner,
repo = repo,
number = number
);
Some((html_url, owner, repo, number))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_companion_parse() {
assert_eq!(
companion_parse(
"companion: https://github.com/paritytech/polkadot/pull/1234"
),
Some((
"https://github.com/paritytech/polkadot/pull/1234".to_owned(),
"paritytech".to_owned(),
"polkadot".to_owned(),
1234
))
);
assert_eq!(
companion_parse(
"\nthis is a companion pr https://github.com/paritytech/polkadot/pull/1234"
),
Some((
"https://github.com/paritytech/polkadot/pull/1234".to_owned(),
"paritytech".to_owned(),
"polkadot".to_owned(),
1234
))
);
assert_eq!(
companion_parse(
"\nthis is some other pr https://github.com/paritytech/polkadot/pull/1234"
),
None,
);
assert_eq!(
companion_parse(
"\nthis is a companion pr https://github.com/paritytech/polkadot/pull/1234/plus+some&other_stuff"
),
Some((
"https://github.com/paritytech/polkadot/pull/1234".to_owned(),
"paritytech".to_owned(),
"polkadot".to_owned(),
1234
))
);
assert_eq!(
companion_parse("companion\nparitytech/polkadot#1234"),
None
);
assert_eq!(
companion_parse("companion: paritytech/polkadot#1234"),
Some((
"https://github.com/paritytech/polkadot/pull/1234".to_owned(),
"paritytech".to_owned(),
"polkadot".to_owned(),
1234
))
);
assert_eq!(
companion_parse("companion: paritytech/polkadot/1234"),
None
);
assert_eq!(
companion_parse("stuff\ncompanion pr: paritytech/polkadot#1234"),
Some((
"https://github.com/paritytech/polkadot/pull/1234".to_owned(),
"paritytech".to_owned(),
"polkadot".to_owned(),
1234
))
);
}
}