config.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. use crate::changelogs::ChangelogFormat;
  2. use crate::github::{GithubClient, Repository};
  3. use std::collections::{HashMap, HashSet};
  4. use std::fmt;
  5. use std::sync::{Arc, RwLock};
  6. use std::time::{Duration, Instant};
  7. use tracing as log;
  8. static CONFIG_FILE_NAME: &str = "triagebot.toml";
  9. const REFRESH_EVERY: Duration = Duration::from_secs(2 * 60); // Every two minutes
  10. lazy_static::lazy_static! {
  11. static ref CONFIG_CACHE:
  12. RwLock<HashMap<String, (Result<Arc<Config>, ConfigurationError>, Instant)>> =
  13. RwLock::new(HashMap::new());
  14. }
  15. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  16. #[serde(rename_all = "kebab-case")]
  17. pub(crate) struct Config {
  18. pub(crate) relabel: Option<RelabelConfig>,
  19. pub(crate) assign: Option<AssignConfig>,
  20. pub(crate) ping: Option<PingConfig>,
  21. pub(crate) nominate: Option<NominateConfig>,
  22. pub(crate) prioritize: Option<PrioritizeConfig>,
  23. pub(crate) major_change: Option<MajorChangeConfig>,
  24. pub(crate) glacier: Option<GlacierConfig>,
  25. pub(crate) close: Option<CloseConfig>,
  26. pub(crate) autolabel: Option<AutolabelConfig>,
  27. pub(crate) notify_zulip: Option<NotifyZulipConfig>,
  28. pub(crate) github_releases: Option<GitHubReleasesConfig>,
  29. pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
  30. pub(crate) review_requested: Option<ReviewRequestedConfig>,
  31. pub(crate) shortcut: Option<ShortcutConfig>,
  32. pub(crate) note: Option<NoteConfig>,
  33. pub(crate) mentions: Option<MentionsConfig>,
  34. pub(crate) no_merges: Option<NoMergesConfig>,
  35. }
  36. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  37. pub(crate) struct NominateConfig {
  38. // team name -> label
  39. pub(crate) teams: HashMap<String, String>,
  40. }
  41. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  42. pub(crate) struct PingConfig {
  43. // team name -> message
  44. // message will have the cc string appended
  45. #[serde(flatten)]
  46. teams: HashMap<String, PingTeamConfig>,
  47. }
  48. impl PingConfig {
  49. pub(crate) fn get_by_name(&self, team: &str) -> Option<(&str, &PingTeamConfig)> {
  50. if let Some((team, cfg)) = self.teams.get_key_value(team) {
  51. return Some((team, cfg));
  52. }
  53. for (name, cfg) in self.teams.iter() {
  54. if cfg.alias.contains(team) {
  55. return Some((name, cfg));
  56. }
  57. }
  58. None
  59. }
  60. }
  61. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  62. pub(crate) struct PingTeamConfig {
  63. pub(crate) message: String,
  64. #[serde(default)]
  65. pub(crate) alias: HashSet<String>,
  66. pub(crate) label: Option<String>,
  67. }
  68. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  69. pub(crate) struct AssignConfig {
  70. /// If `true`, then posts a warning comment if the PR is opened against a
  71. /// different branch than the default (usually master or main).
  72. #[serde(default)]
  73. pub(crate) warn_non_default_branch: bool,
  74. /// A URL to include in the welcome message.
  75. pub(crate) contributing_url: Option<String>,
  76. /// Ad-hoc groups that can be referred to in `owners`.
  77. #[serde(default)]
  78. pub(crate) adhoc_groups: HashMap<String, Vec<String>>,
  79. /// Users to assign when a new PR is opened.
  80. /// The key is a gitignore-style path, and the value is a list of
  81. /// usernames, team names, or ad-hoc groups.
  82. #[serde(default)]
  83. pub(crate) owners: HashMap<String, Vec<String>>,
  84. #[serde(default)]
  85. pub(crate) users_on_vacation: HashSet<String>,
  86. }
  87. impl AssignConfig {
  88. pub(crate) fn is_on_vacation(&self, user: &str) -> bool {
  89. let name_lower = user.to_lowercase();
  90. self.users_on_vacation
  91. .iter()
  92. .any(|vacationer| name_lower == vacationer.to_lowercase())
  93. }
  94. }
  95. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  96. pub(crate) struct NoMergesConfig {
  97. /// No action will be taken on PRs with these labels.
  98. #[serde(default)]
  99. pub(crate) exclude_labels: Vec<String>,
  100. /// Set these labels on the PR when merge commits are detected.
  101. #[serde(default)]
  102. pub(crate) labels: Vec<String>,
  103. /// Override the default message to post when merge commits are detected.
  104. ///
  105. /// This message will always be followed up with
  106. /// "The following commits are merge commits:" and then
  107. /// a list of the merge commits.
  108. pub(crate) message: Option<String>,
  109. }
  110. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  111. pub(crate) struct NoteConfig {
  112. #[serde(default)]
  113. _empty: (),
  114. }
  115. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  116. pub(crate) struct MentionsConfig {
  117. #[serde(flatten)]
  118. pub(crate) paths: HashMap<String, MentionsPathConfig>,
  119. }
  120. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  121. pub(crate) struct MentionsPathConfig {
  122. pub(crate) message: Option<String>,
  123. #[serde(default)]
  124. pub(crate) cc: Vec<String>,
  125. }
  126. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  127. #[serde(rename_all = "kebab-case")]
  128. pub(crate) struct RelabelConfig {
  129. #[serde(default)]
  130. pub(crate) allow_unauthenticated: Vec<String>,
  131. }
  132. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  133. pub(crate) struct ShortcutConfig {
  134. #[serde(default)]
  135. _empty: (),
  136. }
  137. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  138. pub(crate) struct PrioritizeConfig {
  139. pub(crate) label: String,
  140. }
  141. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  142. pub(crate) struct AutolabelConfig {
  143. #[serde(flatten)]
  144. pub(crate) labels: HashMap<String, AutolabelLabelConfig>,
  145. }
  146. impl AutolabelConfig {
  147. pub(crate) fn get_by_trigger(&self, trigger: &str) -> Vec<(&str, &AutolabelLabelConfig)> {
  148. let mut results = Vec::new();
  149. for (label, cfg) in self.labels.iter() {
  150. if cfg.trigger_labels.iter().any(|l| l == trigger) {
  151. results.push((label.as_str(), cfg));
  152. }
  153. }
  154. results
  155. }
  156. }
  157. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  158. pub(crate) struct AutolabelLabelConfig {
  159. #[serde(default)]
  160. pub(crate) trigger_labels: Vec<String>,
  161. #[serde(default)]
  162. pub(crate) exclude_labels: Vec<String>,
  163. #[serde(default)]
  164. pub(crate) trigger_files: Vec<String>,
  165. #[serde(default)]
  166. pub(crate) new_pr: bool,
  167. #[serde(default)]
  168. pub(crate) new_issue: bool,
  169. }
  170. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  171. pub(crate) struct NotifyZulipConfig {
  172. #[serde(flatten)]
  173. pub(crate) labels: HashMap<String, NotifyZulipLabelConfig>,
  174. }
  175. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  176. pub(crate) struct NotifyZulipLabelConfig {
  177. pub(crate) zulip_stream: u64,
  178. pub(crate) topic: String,
  179. pub(crate) message_on_add: Option<String>,
  180. pub(crate) message_on_remove: Option<String>,
  181. pub(crate) message_on_close: Option<String>,
  182. pub(crate) message_on_reopen: Option<String>,
  183. #[serde(default)]
  184. pub(crate) required_labels: Vec<String>,
  185. }
  186. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  187. pub(crate) struct MajorChangeConfig {
  188. /// A username (typically a group, e.g. T-lang) to ping on Zulip for newly
  189. /// opened proposals.
  190. pub(crate) zulip_ping: String,
  191. /// This label allows an issue to participate in the major change process
  192. /// (i.e., creates a Zulip thread, tracks seconding, etc.)
  193. // This has a default primarily for backwards compatibility.
  194. #[serde(default = "MajorChangeConfig::enabling_label_default")]
  195. pub(crate) enabling_label: String,
  196. /// This is the label applied when issuing a `@rustbot second` command, it
  197. /// indicates that the proposal has moved into the 10 day waiting period.
  198. pub(crate) second_label: String,
  199. /// This is the label applied after the waiting period has successfully
  200. /// elapsed (currently not automatically applied; this must be done
  201. /// manually).
  202. // This has a default primarily for backwards compatibility.
  203. #[serde(default = "MajorChangeConfig::accept_label_default")]
  204. pub(crate) accept_label: String,
  205. /// This is the label to be added to newly opened proposals, so they can be
  206. /// discussed in a meeting.
  207. pub(crate) meeting_label: String,
  208. pub(crate) zulip_stream: u64,
  209. pub(crate) open_extra_text: Option<String>,
  210. }
  211. impl MajorChangeConfig {
  212. fn enabling_label_default() -> String {
  213. String::from("major-change")
  214. }
  215. fn accept_label_default() -> String {
  216. String::from("major-change-accepted")
  217. }
  218. }
  219. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  220. pub(crate) struct GlacierConfig {}
  221. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  222. pub(crate) struct CloseConfig {}
  223. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  224. pub(crate) struct ReviewSubmittedConfig {
  225. pub(crate) review_labels: Vec<String>,
  226. pub(crate) reviewed_label: String,
  227. }
  228. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  229. pub(crate) struct ReviewRequestedConfig {
  230. pub(crate) remove_labels: Vec<String>,
  231. pub(crate) add_labels: Vec<String>,
  232. }
  233. pub(crate) async fn get(
  234. gh: &GithubClient,
  235. repo: &Repository,
  236. ) -> Result<Arc<Config>, ConfigurationError> {
  237. if let Some(config) = get_cached_config(&repo.full_name) {
  238. log::trace!("returning config for {} from cache", repo.full_name);
  239. config
  240. } else {
  241. log::trace!("fetching fresh config for {}", repo.full_name);
  242. let res = get_fresh_config(gh, repo).await;
  243. CONFIG_CACHE
  244. .write()
  245. .unwrap()
  246. .insert(repo.full_name.to_string(), (res.clone(), Instant::now()));
  247. res
  248. }
  249. }
  250. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  251. #[serde(rename_all = "kebab-case")]
  252. pub(crate) struct GitHubReleasesConfig {
  253. pub(crate) format: ChangelogFormat,
  254. pub(crate) project_name: String,
  255. pub(crate) changelog_path: String,
  256. pub(crate) changelog_branch: String,
  257. }
  258. fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
  259. let cache = CONFIG_CACHE.read().unwrap();
  260. cache.get(repo).and_then(|(config, fetch_time)| {
  261. if fetch_time.elapsed() < REFRESH_EVERY {
  262. Some(config.clone())
  263. } else {
  264. None
  265. }
  266. })
  267. }
  268. async fn get_fresh_config(
  269. gh: &GithubClient,
  270. repo: &Repository,
  271. ) -> Result<Arc<Config>, ConfigurationError> {
  272. let contents = gh
  273. .raw_file(&repo.full_name, &repo.default_branch, CONFIG_FILE_NAME)
  274. .await
  275. .map_err(|e| ConfigurationError::Http(Arc::new(e)))?
  276. .ok_or(ConfigurationError::Missing)?;
  277. let config = Arc::new(toml::from_slice::<Config>(&contents).map_err(ConfigurationError::Toml)?);
  278. log::debug!("fresh configuration for {}: {:?}", repo.full_name, config);
  279. Ok(config)
  280. }
  281. #[derive(Clone, Debug)]
  282. pub enum ConfigurationError {
  283. Missing,
  284. Toml(toml::de::Error),
  285. Http(Arc<anyhow::Error>),
  286. }
  287. impl std::error::Error for ConfigurationError {}
  288. impl fmt::Display for ConfigurationError {
  289. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  290. match self {
  291. ConfigurationError::Missing => write!(
  292. f,
  293. "This repository is not enabled to use triagebot.\n\
  294. Add a `triagebot.toml` in the root of the default branch to enable it."
  295. ),
  296. ConfigurationError::Toml(e) => {
  297. write!(f, "Malformed `triagebot.toml` in default branch.\n{e}")
  298. }
  299. ConfigurationError::Http(e) => {
  300. write!(
  301. f,
  302. "Failed to query configuration for this repository.\n{e:?}"
  303. )
  304. }
  305. }
  306. }
  307. }
  308. #[cfg(test)]
  309. mod tests {
  310. use super::*;
  311. #[test]
  312. fn sample() {
  313. let config = r#"
  314. [relabel]
  315. allow-unauthenticated = [
  316. "C-*"
  317. ]
  318. [assign]
  319. users_on_vacation = ["jyn514"]
  320. [note]
  321. [ping.compiler]
  322. message = """\
  323. So many people!\
  324. """
  325. label = "T-compiler"
  326. [ping.wg-meta]
  327. message = """\
  328. Testing\
  329. """
  330. [nominate.teams]
  331. compiler = "T-compiler"
  332. release = "T-release"
  333. core = "T-core"
  334. infra = "T-infra"
  335. [shortcut]
  336. "#;
  337. let config = toml::from_str::<Config>(&config).unwrap();
  338. let mut ping_teams = HashMap::new();
  339. ping_teams.insert(
  340. "compiler".to_owned(),
  341. PingTeamConfig {
  342. message: "So many people!".to_owned(),
  343. label: Some("T-compiler".to_owned()),
  344. alias: HashSet::new(),
  345. },
  346. );
  347. ping_teams.insert(
  348. "wg-meta".to_owned(),
  349. PingTeamConfig {
  350. message: "Testing".to_owned(),
  351. label: None,
  352. alias: HashSet::new(),
  353. },
  354. );
  355. let mut nominate_teams = HashMap::new();
  356. nominate_teams.insert("compiler".to_owned(), "T-compiler".to_owned());
  357. nominate_teams.insert("release".to_owned(), "T-release".to_owned());
  358. nominate_teams.insert("core".to_owned(), "T-core".to_owned());
  359. nominate_teams.insert("infra".to_owned(), "T-infra".to_owned());
  360. assert_eq!(
  361. config,
  362. Config {
  363. relabel: Some(RelabelConfig {
  364. allow_unauthenticated: vec!["C-*".into()],
  365. }),
  366. assign: Some(AssignConfig {
  367. warn_non_default_branch: false,
  368. contributing_url: None,
  369. adhoc_groups: HashMap::new(),
  370. owners: HashMap::new(),
  371. users_on_vacation: HashSet::from(["jyn514".into()]),
  372. }),
  373. note: Some(NoteConfig { _empty: () }),
  374. ping: Some(PingConfig { teams: ping_teams }),
  375. nominate: Some(NominateConfig {
  376. teams: nominate_teams
  377. }),
  378. shortcut: Some(ShortcutConfig { _empty: () }),
  379. prioritize: None,
  380. major_change: None,
  381. glacier: None,
  382. close: None,
  383. autolabel: None,
  384. notify_zulip: None,
  385. github_releases: None,
  386. review_submitted: None,
  387. review_requested: None,
  388. mentions: None,
  389. no_merges: None,
  390. }
  391. );
  392. }
  393. }