1use std::sync::{Arc, OnceLock};
2
3use axum::{
4 Extension, Json, Router,
5 extract::{self, Query},
6 routing::get,
7};
8use chrono::{NaiveDateTime, SecondsFormat};
9use serde::{Deserialize, Serialize};
10
11use crate::db::ContributorSelector;
12use crate::{AppState, Result};
13
14pub fn router() -> Router {
15 Router::new()
16 .route("/contributors", get(get_contributors).post(add_contributor))
17 .route("/contributor", get(check_is_contributor))
18}
19
20async fn get_contributors(Extension(app): Extension<Arc<AppState>>) -> Result<Json<Vec<String>>> {
21 Ok(Json(app.db.get_contributors().await?))
22}
23
24#[derive(Debug, Deserialize)]
25struct CheckIsContributorParams {
26 github_user_id: Option<i32>,
27 github_login: Option<String>,
28}
29
30impl CheckIsContributorParams {
31 fn into_contributor_selector(self) -> Result<ContributorSelector> {
32 if let Some(github_user_id) = self.github_user_id {
33 return Ok(ContributorSelector::GitHubUserId { github_user_id });
34 }
35
36 if let Some(github_login) = self.github_login {
37 return Ok(ContributorSelector::GitHubLogin { github_login });
38 }
39
40 Err(anyhow::anyhow!(
41 "must be one of `github_user_id` or `github_login`."
42 ))?
43 }
44}
45
46#[derive(Debug, Serialize)]
47struct CheckIsContributorResponse {
48 signed_at: Option<String>,
49}
50
51async fn check_is_contributor(
52 Extension(app): Extension<Arc<AppState>>,
53 Query(params): Query<CheckIsContributorParams>,
54) -> Result<Json<CheckIsContributorResponse>> {
55 let params = params.into_contributor_selector()?;
56
57 if CopilotSweAgentBot::is_copilot_bot(¶ms) {
58 return Ok(Json(CheckIsContributorResponse {
59 signed_at: Some(
60 CopilotSweAgentBot::created_at()
61 .and_utc()
62 .to_rfc3339_opts(SecondsFormat::Millis, true),
63 ),
64 }));
65 }
66
67 if Dependabot::is_dependabot(¶ms) {
68 return Ok(Json(CheckIsContributorResponse {
69 signed_at: Some(
70 Dependabot::created_at()
71 .and_utc()
72 .to_rfc3339_opts(SecondsFormat::Millis, true),
73 ),
74 }));
75 }
76
77 if RenovateBot::is_renovate_bot(¶ms) {
78 return Ok(Json(CheckIsContributorResponse {
79 signed_at: Some(
80 RenovateBot::created_at()
81 .and_utc()
82 .to_rfc3339_opts(SecondsFormat::Millis, true),
83 ),
84 }));
85 }
86
87 if ZedZippyBot::is_zed_zippy_bot(¶ms) {
88 return Ok(Json(CheckIsContributorResponse {
89 signed_at: Some(
90 ZedZippyBot::created_at()
91 .and_utc()
92 .to_rfc3339_opts(SecondsFormat::Millis, true),
93 ),
94 }));
95 }
96
97 Ok(Json(CheckIsContributorResponse {
98 signed_at: app
99 .db
100 .get_contributor_sign_timestamp(¶ms)
101 .await?
102 .map(|ts| ts.and_utc().to_rfc3339_opts(SecondsFormat::Millis, true)),
103 }))
104}
105
106/// The Copilot bot GitHub user (`copilot-swe-agent[bot]`).
107///
108/// https://api.github.com/users/copilot-swe-agent[bot]
109struct CopilotSweAgentBot;
110
111impl CopilotSweAgentBot {
112 const LOGIN: &'static str = "copilot-swe-agent[bot]";
113 const USER_ID: i32 = 198982749;
114
115 /// Returns the `created_at` timestamp for the Dependabot bot user.
116 fn created_at() -> &'static NaiveDateTime {
117 static CREATED_AT: OnceLock<NaiveDateTime> = OnceLock::new();
118 CREATED_AT.get_or_init(|| {
119 chrono::DateTime::parse_from_rfc3339("2025-02-12T20:26:08Z")
120 .expect("failed to parse 'created_at' for 'copilot-swe-agent[bot]'")
121 .naive_utc()
122 })
123 }
124
125 /// Returns whether the given contributor selector corresponds to the Copilot bot user.
126 fn is_copilot_bot(contributor: &ContributorSelector) -> bool {
127 match contributor {
128 ContributorSelector::GitHubLogin { github_login } => github_login == Self::LOGIN,
129 ContributorSelector::GitHubUserId { github_user_id } => {
130 github_user_id == &Self::USER_ID
131 }
132 }
133 }
134}
135
136/// The Dependabot bot GitHub user (`dependabot[bot]`).
137///
138/// https://api.github.com/users/dependabot[bot]
139struct Dependabot;
140
141impl Dependabot {
142 const LOGIN: &'static str = "dependabot[bot]";
143 const USER_ID: i32 = 49699333;
144
145 /// Returns the `created_at` timestamp for the Dependabot bot user.
146 fn created_at() -> &'static NaiveDateTime {
147 static CREATED_AT: OnceLock<NaiveDateTime> = OnceLock::new();
148 CREATED_AT.get_or_init(|| {
149 chrono::DateTime::parse_from_rfc3339("2019-04-16T22:34:25Z")
150 .expect("failed to parse 'created_at' for 'dependabot[bot]'")
151 .naive_utc()
152 })
153 }
154
155 /// Returns whether the given contributor selector corresponds to the Dependabot bot user.
156 fn is_dependabot(contributor: &ContributorSelector) -> bool {
157 match contributor {
158 ContributorSelector::GitHubLogin { github_login } => github_login == Self::LOGIN,
159 ContributorSelector::GitHubUserId { github_user_id } => {
160 github_user_id == &Self::USER_ID
161 }
162 }
163 }
164}
165
166/// The Renovate bot GitHub user (`renovate[bot]`).
167///
168/// https://api.github.com/users/renovate[bot]
169struct RenovateBot;
170
171impl RenovateBot {
172 const LOGIN: &'static str = "renovate[bot]";
173 const USER_ID: i32 = 29139614;
174
175 /// Returns the `created_at` timestamp for the Renovate bot user.
176 fn created_at() -> &'static NaiveDateTime {
177 static CREATED_AT: OnceLock<NaiveDateTime> = OnceLock::new();
178 CREATED_AT.get_or_init(|| {
179 chrono::DateTime::parse_from_rfc3339("2017-06-02T07:04:12Z")
180 .expect("failed to parse 'created_at' for 'renovate[bot]'")
181 .naive_utc()
182 })
183 }
184
185 /// Returns whether the given contributor selector corresponds to the Renovate bot user.
186 fn is_renovate_bot(contributor: &ContributorSelector) -> bool {
187 match contributor {
188 ContributorSelector::GitHubLogin { github_login } => github_login == Self::LOGIN,
189 ContributorSelector::GitHubUserId { github_user_id } => {
190 github_user_id == &Self::USER_ID
191 }
192 }
193 }
194}
195
196/// The Zed Zippy bot GitHub user (`zed-zippy[bot]`).
197///
198/// https://api.github.com/users/zed-zippy[bot]
199struct ZedZippyBot;
200
201impl ZedZippyBot {
202 const LOGIN: &'static str = "zed-zippy[bot]";
203 const USER_ID: i32 = 234243425;
204
205 /// Returns the `created_at` timestamp for the Zed Zippy bot user.
206 fn created_at() -> &'static NaiveDateTime {
207 static CREATED_AT: OnceLock<NaiveDateTime> = OnceLock::new();
208 CREATED_AT.get_or_init(|| {
209 chrono::DateTime::parse_from_rfc3339("2025-09-24T17:00:11Z")
210 .expect("failed to parse 'created_at' for 'zed-zippy[bot]'")
211 .naive_utc()
212 })
213 }
214
215 /// Returns whether the given contributor selector corresponds to the Zed Zippy bot user.
216 fn is_zed_zippy_bot(contributor: &ContributorSelector) -> bool {
217 match contributor {
218 ContributorSelector::GitHubLogin { github_login } => github_login == Self::LOGIN,
219 ContributorSelector::GitHubUserId { github_user_id } => {
220 github_user_id == &Self::USER_ID
221 }
222 }
223 }
224}
225
226#[derive(Debug, Deserialize)]
227struct AddContributorBody {
228 github_user_id: i32,
229 github_login: String,
230 github_email: Option<String>,
231 github_name: Option<String>,
232 github_user_created_at: chrono::DateTime<chrono::Utc>,
233}
234
235async fn add_contributor(
236 Extension(app): Extension<Arc<AppState>>,
237 extract::Json(params): extract::Json<AddContributorBody>,
238) -> Result<()> {
239 let initial_channel_id = app.config.auto_join_channel_id;
240 app.db
241 .add_contributor(
242 ¶ms.github_login,
243 params.github_user_id,
244 params.github_email.as_deref(),
245 params.github_name.as_deref(),
246 params.github_user_created_at,
247 initial_channel_id,
248 )
249 .await
250}