use super::{Cursor, Event, PrInfo, PrState, RepoId, Role, Target, Trigger, Verb, newer, now_iso, parse_command};
use crate::forge::ForgeKind;
use crate::http::{Api, Got};
use anyhow::{Context, Result};
use reqwest::Method;
use serde::Deserialize;
use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
pub struct GitHub {
pub host: String,
pub me: String,
pub me_id: u64,
pub api: Api,
pub labels: HashMap<String, String>,
pub subscribed: std::collections::HashSet<String>,
pub sweep_every: std::time::Duration,
team_cache: super::Ttl<String, Vec<String>>,
perm_cache: super::Ttl<String, Role>,
}
#[derive(Deserialize)]
struct User {
login: String,
#[serde(default)]
id: u64,
#[serde(rename = "type", default)]
kind: String,
}
impl User {
fn is_bot(&self) -> bool {
self.kind == "Bot" || self.login.ends_with("[bot]")
}
}
#[derive(Deserialize)]
struct Thread {
reason: String,
updated_at: String,
subject: Subject,
repository: Repo,
}
#[derive(Deserialize)]
struct Subject {
#[serde(rename = "type")]
kind: String,
url: Option<String>,
}
#[derive(Deserialize)]
struct Repo {
full_name: String,
}
#[derive(Deserialize)]
struct Comment {
id: u64,
user: User,
#[serde(default)]
author_association: String,
#[serde(default)]
body: String,
#[serde(default)]
html_url: String,
created_at: String,
#[serde(default)]
path: Option<String>,
#[serde(default)]
line: Option<u64>,
#[serde(default)]
in_reply_to_id: Option<u64>,
#[serde(default)]
performed_via_github_app: Option<serde_json::Value>,
}
#[derive(Deserialize)]
struct Review {
id: u64,
user: Option<User>,
#[serde(default)]
author_association: String,
#[serde(default)]
body: String,
state: String,
#[serde(default)]
html_url: String,
#[serde(default)]
submitted_at: Option<String>,
}
#[derive(Deserialize)]
struct IssueEvent {
id: u64,
event: String,
actor: Option<User>,
label: Option<Label>,
created_at: String,
}
#[derive(Deserialize)]
struct Label {
name: String,
}
#[derive(Deserialize)]
struct Pull {
number: u64,
title: String,
#[serde(default)]
body: Option<String>,
user: User,
state: String,
#[serde(default)]
merged: bool,
#[serde(default)]
draft: bool,
#[serde(default)]
mergeable_state: Option<String>,
head: Head,
base: Head,
}
#[derive(Deserialize)]
struct Head {
#[serde(rename = "ref")]
name: String,
sha: String,
repo: Option<HeadRepo>,
}
#[derive(Deserialize)]
struct HeadRepo {
ssh_url: String,
clone_url: String,
}
impl GitHub {
pub fn new(host: &str, api: Api) -> Self {
Self {
host: host.into(),
me: String::new(),
me_id: 0,
api,
labels: HashMap::new(),
subscribed: HashSet::default(),
sweep_every: std::time::Duration::from_secs(600),
team_cache: super::Ttl::default(),
perm_cache: super::Ttl::default(),
}
}
pub fn api_base(host: &str) -> String {
if host == "github.com" { "https://api.github.com".into() } else { format!("https://{host}/api/v3") }
}
fn repo(&self, full: &str) -> RepoId {
RepoId::new(ForgeKind::GitHub, &self.host, full)
}
pub async fn login(&mut self) -> Result<()> {
let u: User = self.api.get("/user").await.context("GitHub login")?;
self.me = u.login;
self.me_id = u.id;
Ok(())
}
pub async fn poll(&mut self, cur: &mut Cursor) -> Result<Vec<Event>> {
let since = cur.since.as_deref().map(super::overlap);
let mut path = "/notifications?all=true&per_page=50".to_string();
if let Some(s) = &since {
let _ = write!(path, "&since={s}");
}
let started = now_iso();
let mut out = Vec::new();
match self.api.get_cond::<Vec<Thread>>(&path, cur.last_modified.as_deref()).await? {
Got::NotModified => {}
Got::Fresh(mut threads, headers) => {
cur.poll_interval_secs = headers.get("x-poll-interval").and_then(|v| v.to_str().ok()?.parse().ok());
let mut next = crate::http::next_link(&headers);
for _ in 0..10 {
let Some(p) = next.take() else { break };
if let Got::Fresh(more, h) = self.api.get_cond::<Vec<Thread>>(&p, None).await? {
threads.extend(more);
next = crate::http::next_link(&h);
}
}
let mut failed = false;
for t in threads.iter().filter(|t| self.subscribed.contains(&t.repository.full_name)) {
if let Err(e) = self.thread_events(t, since.as_deref(), &mut out).await {
tracing::warn!("{}: {e:#}", t.repository.full_name);
failed = true;
}
}
if !failed {
cur.last_modified = headers.get("last-modified").and_then(|v| v.to_str().ok()).map(String::from);
cur.since = Some(started);
}
}
}
let due = cur.last_sweep.is_none_or(|t| crate::state::now() - t >= i64::try_from(self.sweep_every.as_secs()).unwrap_or(i64::MAX));
if due {
cur.last_sweep = Some(crate::state::now());
match self.sweep(cur).await {
Ok(evs) => out.extend(evs),
Err(e) => tracing::warn!("sweep: {e:#}"),
}
}
Ok(out)
}
async fn thread_events(&self, t: &Thread, since: Option<&str>, out: &mut Vec<Event>) -> Result<()> {
let Some(url) = &t.subject.url else { return Ok(()) };
let number: u64 = url.rsplit('/').next().and_then(|n| n.parse().ok()).unwrap_or(0);
if number == 0 {
return Ok(());
}
let start = out.len();
let repo = self.repo(&t.repository.full_name);
let r = &t.repository.full_name;
let since_q = since.map(|s| format!("&since={s}")).unwrap_or_default();
let after = |ts: &str| newer(ts, since);
let (target, mine) = match t.subject.kind.as_str() {
"PullRequest" => {
let p: Pull = self.api.get(&format!("/repos/{r}/pulls/{number}")).await?;
(Target::Pr, p.user.login.eq_ignore_ascii_case(&self.me))
}
"Issue" => (Target::Issue, false),
_ => return Ok(()),
};
tracing::debug!("{r}#{number} reason={} updated={}", t.reason, t.updated_at);
let comments: Vec<Comment> = self.api.get_all(&format!("/repos/{r}/issues/{number}/comments?per_page=100{since_q}"), 10).await?;
for c in comments.into_iter().filter(|c| after(&c.created_at)) {
let verb = parse_command(&c.body);
if !mine && verb.is_none() {
continue;
}
out.push(Self::event(&repo, target, number, Trigger::Comment, format!("gh-{}-ic-{}", self.host, c.id), c, verb));
}
if target == Target::Pr && mine {
let rc: Vec<Comment> = self.api.get_all(&format!("/repos/{r}/pulls/{number}/comments?per_page=100{since_q}"), 10).await?;
for c in rc.into_iter().filter(|c| after(&c.created_at)) {
let thread = Some(c.in_reply_to_id.unwrap_or(c.id).to_string());
let mut e = Self::event(&repo, target, number, Trigger::ReviewComment, format!("gh-{}-rc-{}", self.host, c.id), c, None);
e.thread = thread;
out.push(e);
}
let reviews: Vec<Review> = self.api.get_all(&format!("/repos/{r}/pulls/{number}/reviews?per_page=100"), 20).await?;
for rv in reviews {
let Some(at) = rv.submitted_at.clone() else { continue };
if !after(&at) || rv.state == "APPROVED" || rv.state == "PENDING" || (rv.state == "COMMENTED" && rv.body.trim().is_empty())
{
continue;
}
let Some(user) = rv.user else { continue };
let body =
if rv.body.trim().is_empty() { format!("(review: {})", rv.state.to_lowercase().replace('_', " ")) } else { rv.body };
out.push(Event {
id: format!("gh-{}-rv-{}", self.host, rv.id),
repo: repo.clone(),
target,
number,
trigger: Trigger::Review,
is_bot: user.is_bot(),
author: user.login,
role: Role::from_github(&rv.author_association),
body,
url: rv.html_url,
thread: None,
path: None,
line: None,
created_at: at,
verb: None,
});
}
}
if !mine && let Some(label) = self.labels.get(r) {
let evs: Vec<IssueEvent> = self.api.get_all(&format!("/repos/{r}/issues/{number}/events?per_page=100"), 10).await?;
for ev in evs.into_iter().filter(|e| e.event == "labeled" && after(&e.created_at)) {
let (Some(l), Some(actor)) = (&ev.label, &ev.actor) else { continue };
if !l.name.eq_ignore_ascii_case(label) {
continue;
}
let role = self.role_of(r, &actor.login).await.unwrap_or(Role::None);
out.push(Event {
id: format!("gh-{}-label-{}", self.host, ev.id),
repo: repo.clone(),
target,
number,
trigger: Trigger::Comment,
author: actor.login.clone(),
role,
is_bot: actor.is_bot(),
body: format!("(label `{label}` added)"),
url: String::new(),
thread: None,
path: None,
line: None,
created_at: ev.created_at,
verb: Some(Verb::Fix),
});
}
}
self.verify_roles(&t.repository.full_name, &mut out[start..]).await;
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn event(repo: &RepoId, target: Target, number: u64, trigger: Trigger, id: String, c: Comment, verb: Option<Verb>) -> Event {
Event {
id,
repo: repo.clone(),
target,
number,
trigger,
is_bot: c.user.is_bot(),
author: c.user.login,
role: Role::from_github(&c.author_association),
body: c.body,
url: c.html_url,
thread: None,
path: c.path,
line: c.line,
created_at: c.created_at,
verb,
}
}
async fn role_of(&self, repo: &str, user: &str) -> Result<Role> {
#[derive(Deserialize)]
struct P {
permission: String,
}
let key = format!("{repo}:{}", user.to_ascii_lowercase());
if let Some(r) = self.perm_cache.get(&key) {
return Ok(r);
}
let p: P = self.api.get(&format!("/repos/{repo}/collaborators/{user}/permission")).await?;
let r = match p.permission.as_str() {
"admin" => Role::Owner,
"maintain" => Role::Maintainer,
"write" => Role::Collaborator,
_ => Role::Contributor,
};
self.perm_cache.put(key, r);
Ok(r)
}
async fn verify_roles(&self, repo: &str, events: &mut [Event]) {
for e in events.iter_mut().filter(|e| matches!(e.role, Role::Member | Role::Collaborator) && !e.is_bot) {
e.role = self.role_of(repo, &e.author).await.unwrap_or(Role::Contributor);
}
}
async fn sweep(&self, cur: &mut Cursor) -> Result<Vec<Event>> {
let mut out = Vec::new();
for (repo, n) in self.my_open().await? {
if !self.subscribed.contains(&repo.path) {
continue;
}
match self.sweep_pr(&repo, n).await {
Ok((evs, status)) => {
if cur.swept {
out.extend(evs);
}
cur.seen_status.insert(format!("{}#{n}", repo.key()), status);
}
Err(e) => tracing::warn!("sweep {repo}#{n}: {e:#}"),
}
}
cur.swept = true;
Ok(out)
}
async fn sweep_pr(&self, repo: &RepoId, n: u64) -> Result<(Vec<Event>, String)> {
#[derive(Deserialize)]
struct Runs {
check_runs: Vec<Run>,
}
#[derive(Deserialize)]
struct Run {
name: String,
conclusion: Option<String>,
#[serde(default)]
html_url: String,
output: Option<Out>,
}
#[derive(Deserialize)]
struct Out {
title: Option<String>,
summary: Option<String>,
}
let mut out = Vec::new();
{
let r = &repo.path;
let repo = repo.clone();
let p: Pull = self.api.get(&format!("/repos/{r}/pulls/{n}")).await?;
let mut status = Vec::new();
if p.mergeable_state.as_deref() == Some("dirty") {
status.push("conflict".to_string());
out.push(self.state_event(
&repo,
n,
Trigger::MergeConflict,
&p.head.sha,
format!("This PR has merge conflicts with `{}`.", p.base.name),
));
}
let runs: Runs = self.api.get(&format!("/repos/{r}/commits/{}/check-runs?per_page=100", p.head.sha)).await?;
let failed: Vec<&Run> =
runs.check_runs.iter().filter(|c| matches!(c.conclusion.as_deref(), Some("failure" | "timed_out"))).collect();
if failed.is_empty() {
if runs.check_runs.iter().all(|c| c.conclusion.is_some()) && !runs.check_runs.is_empty() {
status.push("green".into());
}
} else {
status.push("red".into());
let mut body = String::from("CI failed:\n");
for f in failed {
let o = f.output.as_ref();
let _ = writeln!(
body,
"- {} ({}) {}\n {}",
f.name,
f.html_url,
o.and_then(|o| o.title.clone()).unwrap_or_default(),
o.and_then(|o| o.summary.clone()).unwrap_or_default().chars().take(800).collect::<String>()
);
}
out.push(self.state_event(&repo, n, Trigger::CiFailure, &p.head.sha, body));
}
Ok((out, status.join(",")))
}
}
fn state_event(&self, repo: &RepoId, n: u64, trigger: Trigger, sha: &str, body: String) -> Event {
Event {
id: format!("gh-{}-{}-{}#{n}-{sha}", self.host, crate::state::enum_str(&trigger), repo.path),
repo: repo.clone(),
target: Target::Pr,
number: n,
trigger,
author: self.me.clone(),
role: Role::Owner,
is_bot: false,
body,
url: String::new(),
thread: None,
path: None,
line: None,
created_at: now_iso(),
verb: None,
}
}
pub async fn my_open(&self) -> Result<Vec<(RepoId, u64)>> {
#[derive(Deserialize)]
struct S {
items: Vec<I>,
}
#[derive(Deserialize)]
struct I {
number: u64,
repository_url: String,
}
let s: S = self.api.get(&format!("/search/issues?q=is:pr+is:open+author:{}&per_page=100", self.me)).await?;
Ok(s.items
.into_iter()
.filter_map(|i| {
let full = i.repository_url.split("/repos/").nth(1)?;
Some((self.repo(full), i.number))
})
.collect())
}
pub async fn pr(&self, repo: &RepoId, n: u64) -> Result<PrInfo> {
let p: Pull = self.api.get(&format!("/repos/{}/pulls/{n}", repo.path)).await?;
let head_repo = p.head.repo.as_ref();
Ok(PrInfo {
number: p.number,
title: p.title,
author: p.user.login,
state: match (p.state.as_str(), p.merged) {
(_, true) => PrState::Merged,
("closed", _) => PrState::Closed,
_ => PrState::Open,
},
head_ref: p.head.name,
head_sha: p.head.sha,
head_repo_ssh: head_repo.map(|r| r.ssh_url.clone()).unwrap_or_default(),
head_repo_https: head_repo.map(|r| r.clone_url.clone()).unwrap_or_default(),
base_ref: p.base.name,
draft: p.draft,
body: p.body.unwrap_or_default(),
})
}
pub async fn issue(&self, repo: &RepoId, n: u64) -> Result<super::IssueInfo> {
#[derive(Deserialize)]
struct I {
title: String,
body: Option<String>,
user: User,
}
let i: I = self.api.get(&format!("/repos/{}/issues/{n}", repo.path)).await?;
Ok(super::IssueInfo { title: i.title, body: i.body.unwrap_or_default(), author: i.user.login })
}
pub async fn reply(&self, repo: &RepoId, _target: Target, n: u64, to: Option<&Event>, body: &str) -> Result<()> {
let r = &repo.path;
match to {
Some(e) if e.trigger == Trigger::ReviewComment && e.thread.is_some() => {
let id = e.thread.as_deref().unwrap_or_default();
self.api.send(Method::POST, &format!("/repos/{r}/pulls/{n}/comments/{id}/replies"), &json!({ "body": body })).await?;
}
_ => {
self.api.send(Method::POST, &format!("/repos/{r}/issues/{n}/comments"), &json!({ "body": body })).await?;
}
}
Ok(())
}
pub async fn resolve(&self, repo: &RepoId, comment_id: &str) -> Result<()> {
let gql = format!("{}/graphql", self.api.base.trim_end_matches("/v3"));
let node: serde_json::Value = self.api.get(&format!("/repos/{}/pulls/comments/{comment_id}", repo.path)).await?;
let q = json!({
"query": "query($id:ID!){ node(id:$id){ ... on PullRequestReviewComment { pullRequestReview { pullRequest { reviewThreads(first:100){ nodes { id isResolved comments(first:1){ nodes { databaseId } } } } } } } } }",
"variables": { "id": node.get("node_id").and_then(|v| v.as_str()).unwrap_or_default() }
});
let v = self.api.send(Method::POST, &gql, &q).await?;
let want: u64 = comment_id.parse().unwrap_or(0);
let threads = v
.pointer("/data/node/pullRequestReview/pullRequest/reviewThreads/nodes")
.and_then(|t| t.as_array())
.cloned()
.unwrap_or_default();
for t in threads {
let first = t.pointer("/comments/nodes/0/databaseId").and_then(serde_json::Value::as_u64);
if first == Some(want) && t["isResolved"] == false {
let m = json!({ "query": "mutation($id:ID!){ resolveReviewThread(input:{threadId:$id}){ thread { id } } }", "variables": { "id": t["id"] } });
self.api.send(Method::POST, &gql, &m).await?;
}
}
Ok(())
}
pub async fn open_draft(&self, repo: &RepoId, head: &str, base: &str, title: &str, body: &str) -> Result<String> {
let v = self
.api
.send(
Method::POST,
&format!("/repos/{}/pulls", repo.path),
&json!({ "title": title, "head": head, "base": base, "body": body, "draft": true }),
)
.await?;
Ok(v["html_url"].as_str().unwrap_or_default().to_string())
}
pub async fn controls(&self, repo: &RepoId, n: u64, since: &str) -> Result<Vec<super::ControlComment>> {
let cs: Vec<Comment> =
self.api.get_all(&format!("/repos/{}/issues/{n}/comments?per_page=100&since={since}", repo.path), 10).await?;
Ok(cs
.into_iter()
.filter_map(|c| {
let via_app = c.performed_via_github_app.as_ref().is_some_and(|v| !v.is_null());
let control = super::control_of(self.me_id, c.user.id, via_app, &c.body, &c.created_at, since)?;
Some(super::ControlComment { id: c.id.to_string(), control })
})
.collect())
}
pub async fn ack(&self, repo: &RepoId, comment: &str) -> Result<()> {
self.api
.send(Method::POST, &format!("/repos/{}/issues/comments/{comment}/reactions", repo.path), &json!({ "content": "+1" }))
.await?;
Ok(())
}
pub async fn in_team(&self, team: &str, user: &str) -> Result<bool> {
if let Some(members) = self.team_cache.get(&team.to_string()) {
return Ok(members.iter().any(|m| m.eq_ignore_ascii_case(user)));
}
let (org, slug) = team.split_once('/').context("team must be org/team")?;
let members: Vec<User> = self.api.get_all(&format!("/orgs/{org}/teams/{slug}/members?per_page=100"), 100).await?;
let members: Vec<String> = members.into_iter().map(|u| u.login).collect();
let hit = members.iter().any(|m| m.eq_ignore_ascii_case(user));
self.team_cache.put(team.into(), members);
Ok(hit)
}
}