use super::ghlike::{self, ListItem};
use super::{Cursor, Event, PrInfo, RepoId, Role, Target, Trigger, Verb, newer, now_iso, parse_command};
use crate::forge::ForgeKind;
use crate::http::Api;
use anyhow::{Context, Result, bail};
use reqwest::Method;
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
pub const SCOPES: &str = "read:user, read:notification, write:issue, write:repository, read:organization";
fn forgejo_gap(rules: &[serde_json::Value], base: &str) -> Option<String> {
let matches = |name: &str| name == base || name.strip_suffix('*').is_some_and(|prefix| base.starts_with(prefix));
let Some(p) = rules.iter().find(|r| r["rule_name"].as_str().or(r["branch_name"].as_str()).is_some_and(matches)) else {
return Some(format!("no branch protection covers {base}"));
};
if p["enable_status_check"] != true || p["status_check_contexts"].as_array().is_none_or(Vec::is_empty) {
Some("its branch protection doesn't require named status checks".into())
} else if p["required_approvals"].as_u64().unwrap_or(0) < 1 || p["dismiss_stale_approvals"] != true {
Some("its branch protection doesn't require an approval of the latest push".into())
} else {
None
}
}
pub struct Forgejo {
pub host: String,
pub me: String,
pub me_id: u64,
pub api: Api,
pub labels: HashMap<String, String>,
pub subscribed: 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,
}
impl User {
fn is_bot(&self) -> bool {
let l = self.login.to_ascii_lowercase();
l.ends_with("[bot]") || l.ends_with("-bot") || l.ends_with("_bot") || l == "gitea-actions" || l == "forgejo-actions"
}
}
#[derive(Deserialize)]
struct Thread {
subject: Subject,
repository: Repo,
}
#[derive(Deserialize)]
struct Subject {
#[serde(rename = "type")]
kind: String,
#[serde(default)]
url: String,
}
#[derive(Deserialize)]
struct Repo {
full_name: String,
}
#[derive(Deserialize)]
struct Comment {
id: u64,
user: User,
#[serde(default)]
body: String,
#[serde(default)]
html_url: String,
created_at: String,
#[serde(default)]
updated_at: String,
}
#[derive(Deserialize)]
struct Review {
id: u64,
user: Option<User>,
#[serde(default)]
body: String,
state: String,
#[serde(default)]
html_url: String,
#[serde(default)]
submitted_at: Option<String>,
#[serde(default)]
comments_count: u64,
}
#[derive(Deserialize)]
struct ReviewComment {
id: u64,
user: User,
#[serde(default)]
body: String,
#[serde(default)]
html_url: String,
created_at: String,
path: String,
#[serde(default)]
position: u64,
#[serde(default)]
original_position: u64,
}
#[derive(Deserialize)]
struct TimelineItem {
id: u64,
#[serde(rename = "type")]
kind: String,
user: Option<User>,
label: Option<Label>,
#[serde(default)]
body: String,
created_at: String,
}
#[derive(Deserialize)]
struct Label {
name: String,
}
fn role_from(permission: &str) -> Role {
match permission {
"owner" => Role::Owner,
"admin" => Role::Maintainer,
"write" => Role::Collaborator,
"read" => Role::Contributor,
_ => Role::None,
}
}
impl Forgejo {
pub fn new(host: &str, api: Api) -> Self {
Self {
host: host.into(),
me: String::new(),
me_id: 0,
api,
labels: HashMap::new(),
subscribed: HashSet::new(),
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 {
format!("https://{host}/api/v1")
}
fn repo(&self, full: &str) -> RepoId {
RepoId::new(ForgeKind::Forgejo, &self.host, full)
}
pub async fn login(&mut self) -> Result<()> {
let u: User = self.api.get("/user").await.context("Forgejo login")?;
self.me = u.login;
self.me_id = u.id;
Ok(())
}
pub async fn controls(&self, repo: &RepoId, _target: Target, n: u64, since: &str) -> Result<Vec<super::ControlComment>> {
let cs: Vec<Comment> = self.api.get_all(&format!("/repos/{}/issues/{n}/comments?limit=50&since={since}", repo.path), 10).await?;
Ok(cs
.into_iter()
.filter_map(|c| {
let control = super::control_of(self.me_id, c.user.id, false, &c.body, &c.created_at, &c.updated_at, since)?;
Some(super::ControlComment { id: c.id.to_string(), control })
})
.collect())
}
pub async fn ack(&self, repo: &RepoId, _target: Target, _n: u64, comment: &str) -> Result<()> {
ghlike::ack(&self.api, repo, comment).await
}
pub async fn poll(&mut self, cur: &mut Cursor) -> Result<Vec<Event>> {
let since = cur.since.as_deref().map(super::overlap);
let started = now_iso();
let mut path = "/notifications?all=true&limit=50".to_string();
if let Some(s) = &since {
let _ = write!(path, "&since={s}");
}
let mut out = Vec::new();
let threads: Vec<Thread> = self.api.get_all(&path, 10).await?;
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.since = Some(started);
}
let catch_up = super::catch_up_since(cur.last_sweep, cur.swept);
if cur.sweep_due(self.sweep_every) {
match self.sweep(cur, catch_up.as_deref()).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 number: u64 = t.subject.url.rsplit('/').next().and_then(|n| n.parse().ok()).unwrap_or(0);
if number == 0 {
return Ok(());
}
let r = &t.repository.full_name;
let repo = self.repo(r);
let after = |ts: &str| newer(ts, since);
let (target, mine) = match t.subject.kind.as_str() {
"Pull" => {
let p = ghlike::pull(&self.api, r, number).await?;
(Target::Pr, p.user.login.eq_ignore_ascii_case(&self.me))
}
"Issue" => (Target::Issue, false),
_ => return Ok(()),
};
let ev = |trigger, id: String, u: User, body: String, url: String, at: String, verb| Event {
is_bot: u.is_bot(),
author_id: u.id,
author: u.login,
body,
url,
created_at: at,
verb,
..Event::new(id, &repo, target, number, trigger)
};
let start = out.len();
let since_q = since.map(|s| format!("&since={s}")).unwrap_or_default();
let comments: Vec<Comment> = self.api.get_all(&format!("/repos/{r}/issues/{number}/comments?limit=50{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(ev(Trigger::Comment, format!("fj-{}-ic-{}", self.host, c.id), c.user, c.body, c.html_url, c.created_at, verb));
}
if target == Target::Pr && mine {
let reviews: Vec<Review> = self.api.get_all(&format!("/repos/{r}/pulls/{number}/reviews?limit=50"), 20).await?;
let mut first: HashMap<(String, u64, u64), u64> = HashMap::new();
for c in self.review_comments(r, number, &reviews).await? {
let root = *first.entry((c.path.clone(), c.position, c.original_position)).or_insert(c.id);
if after(&c.created_at) {
let mut e =
ev(Trigger::ReviewComment, format!("fj-{}-rc-{}", self.host, c.id), c.user, c.body, c.html_url, c.created_at, None);
(e.thread, e.path, e.line) = (Some(root.to_string()), Some(c.path), (c.position > 0).then_some(c.position));
out.push(e);
}
}
for rv in reviews {
let Some(at) = rv.submitted_at.clone() else { continue };
if !after(&at)
|| matches!(rv.state.as_str(), "APPROVED" | "PENDING" | "REQUEST_REVIEW")
|| (rv.state == "COMMENT" && 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(ev(Trigger::Review, format!("fj-{}-rv-{}", self.host, rv.id), user, body, rv.html_url, at, None));
}
}
if !mine && let Some(label) = self.labels.get(r) {
let tl: Vec<TimelineItem> = self.api.get_all(&format!("/repos/{r}/issues/{number}/timeline?limit=50{since_q}"), 10).await?;
for i in tl.into_iter().filter(|i| i.kind == "label" && i.body == "1" && after(&i.created_at)) {
let (Some(l), Some(u)) = (i.label, i.user) else { continue };
if l.name.eq_ignore_ascii_case(label) {
let body = format!("(label `{label}` added)");
out.push(ev(
Trigger::Comment,
format!("fj-{}-label-{}", self.host, i.id),
u,
body,
String::new(),
i.created_at,
Some(Verb::Fix),
));
}
}
}
for e in out[start..].iter_mut().filter(|e| !e.is_bot && !e.author.eq_ignore_ascii_case(&self.me)) {
e.role = self.role_of(r, &e.author).await;
}
Ok(())
}
async fn review_comments(&self, r: &str, number: u64, reviews: &[Review]) -> Result<Vec<ReviewComment>> {
let mut rcs = Vec::new();
for rv in reviews.iter().filter(|rv| rv.comments_count > 0) {
let cs: Vec<ReviewComment> = self.api.get(&format!("/repos/{r}/pulls/{number}/reviews/{}/comments", rv.id)).await?;
rcs.extend(cs);
}
rcs.sort_by_key(|c| c.id);
Ok(rcs)
}
async fn role_of(&self, repo: &str, user: &str) -> 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 r;
}
let r = match self.api.get::<P>(&format!("/repos/{repo}/collaborators/{user}/permission")).await {
Ok(p) => role_from(&p.permission),
Err(e) if crate::http::status(&e) == Some(403) => {
if repo.split('/').next().is_some_and(|o| o.eq_ignore_ascii_case(user)) {
Role::Owner
} else {
match self.api.send(Method::GET, &format!("/repos/{repo}/collaborators/{user}"), &Value::Null).await {
Ok(_) => Role::Contributor,
Err(e) if crate::http::status(&e) == Some(404) => Role::None,
Err(_) => return Role::None,
}
}
}
Err(e) if crate::http::status(&e) == Some(404) => Role::None,
Err(_) => return Role::None,
};
self.perm_cache.put(key, r);
r
}
async fn sweep(&self, cur: &mut Cursor, catch_up: Option<&str>) -> Result<Vec<Event>> {
let mut out = Vec::new();
for (repo, n) in self.my_open().await?.into_iter().filter(|(r, _)| self.subscribed.contains(&r.path)) {
let key = format!("{}#{n}", repo.key());
let prev = cur.seen_status.get(&key).cloned().unwrap_or_default();
let r = self.sweep_pr(&repo, n, &prev).await;
cur.record(key, r, &mut out);
if let Some(since) = catch_up
&& let Err(e) = self.pr_events(&repo, n, since, &mut out).await
{
tracing::warn!("catch up {repo}#{n}: {e:#}");
}
}
cur.swept = true;
Ok(out)
}
async fn my_open(&self) -> Result<Vec<(RepoId, u64)>> {
#[derive(Deserialize)]
struct I {
number: u64,
repository: Repo,
}
let mine: Vec<I> = self.api.get_all("/repos/issues/search?type=pulls&state=open&created=true&limit=50", 10).await?;
Ok(mine.into_iter().map(|i| (self.repo(&i.repository.full_name), i.number)).collect())
}
async fn pr_events(&self, repo: &RepoId, n: u64, since: &str, out: &mut Vec<Event>) -> Result<()> {
let t = Thread {
subject: Subject { kind: "Pull".into(), url: format!("/repos/{}/pulls/{n}", repo.path) },
repository: Repo { full_name: repo.path.clone() },
};
self.thread_events(&t, Some(since), out).await
}
pub async fn rescan(&self, repo: &RepoId, since: &str) -> Result<Vec<Event>> {
let mut out = Vec::new();
for (r, n) in self.my_open().await?.into_iter().filter(|(r, _)| r.path.eq_ignore_ascii_case(&repo.path)) {
self.pr_events(&r, n, since, &mut out).await?;
}
Ok(out)
}
async fn sweep_pr(&self, repo: &RepoId, n: u64, prev: &str) -> Result<(Vec<Event>, String)> {
#[derive(Deserialize)]
struct Combined {
#[serde(default)]
statuses: Vec<CommitStatus>,
}
#[derive(Deserialize)]
struct CommitStatus {
#[serde(default)]
context: String,
#[serde(default)]
status: String,
#[serde(default)]
target_url: String,
#[serde(default)]
description: String,
}
let r = &repo.path;
let p = ghlike::pull(&self.api, r, n).await?;
let sha = &p.head.sha;
let mut out = Vec::new();
let mut status = Vec::new();
if !p.mergeable.unwrap_or(false) && !p.draft && p.state == "open" {
let mark = format!("conflict@{sha}");
if prev.split(',').any(|s| s == mark) {
out.push(self.state_event(
repo,
n,
Trigger::MergeConflict,
sha,
format!("This PR has merge conflicts with `{}`.", p.base.name),
));
}
status.push(mark);
}
let c: Combined = self.api.get(&format!("/repos/{r}/commits/{sha}/status")).await?;
let failed: Vec<&CommitStatus> = c.statuses.iter().filter(|s| matches!(s.status.as_str(), "failure" | "error")).collect();
if failed.is_empty() {
if !c.statuses.is_empty() && c.statuses.iter().all(|s| matches!(s.status.as_str(), "success" | "warning")) {
status.push("green".into());
}
} else {
status.push("red".into());
let mut body = String::from("CI failed:\n");
for f in failed {
let _ = writeln!(body, "- {} ({}) {}", f.context, f.target_url, f.description.chars().take(800).collect::<String>());
}
out.push(self.state_event(repo, n, Trigger::CiFailure, sha, body));
}
Ok((out, status.join(",")))
}
fn state_event(&self, repo: &RepoId, n: u64, trigger: Trigger, sha: &str, body: String) -> Event {
let id = format!("fj-{}-{}-{}#{n}-{sha}", self.host, crate::state::enum_str(&trigger), repo.path);
Event::state(id, repo, n, trigger, &self.me, body)
}
pub async fn pr(&self, repo: &RepoId, n: u64) -> Result<PrInfo> {
ghlike::pr(&self.api, repo, n).await
}
pub async fn assign(&self, repo: &RepoId, n: u64, on: bool) -> Result<()> {
#[derive(Deserialize)]
struct I {
#[serde(default)]
assignees: Option<Vec<ghlike::Login>>,
}
let i: I = self.api.get(&format!("/repos/{}/issues/{n}", repo.path)).await?;
let mut who: Vec<String> =
i.assignees.unwrap_or_default().into_iter().map(|l| l.login).filter(|l| !l.eq_ignore_ascii_case(&self.me)).collect();
if on {
who.push(self.me.clone());
}
self.api.send(Method::PATCH, &format!("/repos/{}/issues/{n}", repo.path), &json!({ "assignees": who })).await.map(|_| ())
}
pub async fn open_items(&self, repo: &RepoId, target: Target) -> Result<Vec<super::Listed>> {
let kind = if target == Target::Pr { "pulls" } else { "issues" };
let path = format!("/repos/{}/issues?state=open&type={kind}&limit=50", repo.path);
let items: Vec<ListItem> = self.api.get_all(&path, super::LIST_PAGES).await?;
Ok(items.into_iter().map(ListItem::listed).collect())
}
pub async fn issue(&self, repo: &RepoId, n: u64) -> Result<super::IssueInfo> {
ghlike::issue(&self.api, repo, n).await
}
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.path.is_some() && e.line.is_some() => {
let c = json!({ "path": e.path, "new_position": e.line, "body": body });
self.api
.send(
Method::POST,
&format!("/repos/{r}/pulls/{n}/reviews"),
&json!({ "event": "COMMENT", "body": "", "comments": [c] }),
)
.await?;
}
_ => ghlike::comment(&self.api, repo, n, body).await?,
}
Ok(())
}
pub async fn resolve(&self, repo: &RepoId, _n: u64, comment_id: &str) -> Result<()> {
match self.api.send(Method::POST, &format!("/repos/{}/pulls/comments/{comment_id}/resolve", repo.path), &json!({})).await {
Err(e) if matches!(crate::http::status(&e), Some(404 | 405)) => {
bail!("this server has no API to resolve review conversations (Forgejo); resolve it in the web UI")
}
r => r.map(|_| ()),
}
}
pub async fn review(&self, repo: &RepoId, n: u64, verdict: super::ReviewVerdict, body: &str) -> Result<()> {
let event = match verdict {
super::ReviewVerdict::Comment => "COMMENT",
super::ReviewVerdict::RequestChanges => "REQUEST_CHANGES",
super::ReviewVerdict::Approve => "APPROVED",
};
self.api.send(Method::POST, &format!("/repos/{}/pulls/{n}/reviews", repo.path), &json!({ "event": event, "body": body })).await?;
Ok(())
}
pub async fn open_pr(&self, repo: &RepoId, head: &str, base: &str, title: &str, body: &str, draft: bool) -> Result<String> {
let title = if draft { format!("WIP: {title}") } else { title.to_string() };
ghlike::open_pr(&self.api, repo, &json!({ "title": title, "head": head, "base": base, "body": body })).await
}
pub async fn merge_gate(&self, repo: &RepoId, base: &str) -> Result<Option<String>> {
let Ok(rules) = self.api.get::<Vec<serde_json::Value>>(&format!("/repos/{}/branch_protections", repo.path)).await else {
return Ok(Some("its branch protection isn't readable with this token".into()));
};
Ok(forgejo_gap(&rules, base))
}
pub async fn enable_auto_merge(&self, repo: &RepoId, n: u64, sha: &str) -> Result<()> {
let r: serde_json::Value = self.api.get(&format!("/repos/{}", repo.path)).await?;
let style = [("allow_merge_commits", "merge"), ("allow_squash_merge", "squash"), ("allow_rebase", "rebase")]
.into_iter()
.find(|(k, _)| r[*k] != false)
.map_or("merge", |(_, d)| d);
let body = json!({ "Do": style, "merge_when_checks_succeed": true, "head_commit_id": sha });
self.api.send(Method::POST, &format!("/repos/{}/pulls/{n}/merge", repo.path), &body).await.map(|_| ())
}
pub async fn in_team(&self, team: &str, user: &str) -> Result<bool> {
#[derive(Deserialize)]
struct Found {
data: Vec<T>,
}
#[derive(Deserialize)]
struct T {
id: u64,
name: String,
}
let (org, name) = team.split_once('/').context("team must be org/team")?;
let fetch = async {
let found: Found = self.api.get(&format!("/orgs/{org}/teams/search?q={}", crate::http::enc(name))).await?;
let t = found.data.into_iter().find(|t| t.name.eq_ignore_ascii_case(name)).with_context(|| format!("no team {team}"))?;
let members: Vec<User> = self.api.get_every(&format!("/teams/{}/members?limit=50", t.id), 100).await?;
Ok(members.into_iter().map(|u| u.login).collect())
};
self.team_cache.member(team, user, fetch).await
}
}
#[cfg(test)]
mod tests {
#[test]
fn merge_gate_finds_the_rule_for_the_branch() {
use serde_json::json;
let rule = |name: &str, contexts: serde_json::Value| json!({ "rule_name": name, "enable_status_check": true, "status_check_contexts": contexts, "required_approvals": 1, "dismiss_stale_approvals": true });
assert_eq!(super::forgejo_gap(&[rule("main", json!(["ci"]))], "main"), None);
assert_eq!(super::forgejo_gap(&[rule("ma*", json!(["ci"]))], "main"), None, "a pattern");
assert!(super::forgejo_gap(&[rule("release", json!(["ci"]))], "main").unwrap().contains("covers"));
assert!(super::forgejo_gap(&[rule("main", json!([]))], "main").is_some(), "checks on, but none named");
}
#[test]
fn roles_and_bots() {
use super::{Role, User, role_from};
assert_eq!(role_from("owner"), Role::Owner);
assert_eq!(role_from("admin"), Role::Maintainer);
assert_eq!(role_from("write"), Role::Collaborator);
assert_eq!(role_from("read"), Role::Contributor);
assert_eq!(role_from("none"), Role::None);
let bot = |l: &str| User { login: l.into(), id: 0 }.is_bot();
assert!(bot("renovate-bot") && bot("forgejo-actions") && !bot("abbot"));
}
}