-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
github.rs
76 lines (61 loc) · 1.98 KB
/
github.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
use crate::release::Outputs;
use cmd_lib::run_fun;
use serde::Deserialize;
#[derive(Deserialize, Debug, Clone)]
pub struct PullRequest {
number: u32,
}
#[derive(Deserialize, Debug)]
pub struct Labels {
labels: Vec<Label>,
}
#[derive(Deserialize, Debug)]
pub struct Label {
name: String,
}
pub struct GitHub;
impl GitHub {
/// Shells out to GitHub's CLI `gh` to try and determine if the commit belongs to any pull-request
pub fn find_pull_request_by(
sha: &str,
marker_label: &str,
) -> anyhow::Result<Option<PullRequest>> {
log::trace!("listing pull-requests that contain the commit:");
let pulls = run_fun!(gh pr list --state merged --search ${sha} --limit 1 --json number)?;
log::trace!("{pulls}");
let pulls: Vec<PullRequest> = serde_json::from_str(&pulls)?;
if pulls.is_empty() {
log::debug!("the commit sha {sha} is not part of any pull-request");
return Ok(None);
}
let pr = pulls.first().unwrap().clone();
log::trace!("extracted {pr:?}");
log::trace!("getting labels for the possibly qualified pull-request:");
let labels = {
let pr_number = pr.number;
run_fun!(gh pr view ${pr_number} --json labels)?
};
log::trace!("{labels}");
let labels: Labels = serde_json::from_str(&labels)?;
if labels.labels.is_empty() {
log::debug!(
"the commit sha {sha} is not part of any pull-request with the {marker_label} label"
);
return Ok(None);
}
for label in labels.labels {
if label.name == marker_label {
return Ok(Some(pr));
}
}
Ok(None)
}
}
pub struct Actions;
impl Actions {
// Set an "output" in GitHub Actions
pub fn set_output(key: Outputs, value: &str) {
log::debug!("setting output name={key} value={value}");
println!("::set-output name={key}::{value}");
}
}