Selaa lähdekoodia

Merge pull request #1647 from ehuss/config-default-branch

Fetch triagebot.toml config from the default branch.
Mark Rousskov 2 vuotta sitten
vanhempi
commit
128349ad52
7 muutettua tiedostoa jossa 35 lisäystä ja 26 poistoa
  1. 2 0
      src/actions.rs
  2. 14 11
      src/config.rs
  3. 6 5
      src/github.rs
  4. 4 1
      src/handlers.rs
  5. 7 7
      src/handlers/github_releases.rs
  6. 1 1
      src/handlers/nominate.rs
  7. 1 1
      src/handlers/ping.rs

+ 2 - 0
src/actions.rs

@@ -109,6 +109,8 @@ impl<'a> Action for Step<'a> {
             for repo in repos {
                 let repository = Repository {
                     full_name: format!("{}/{}", repo.0, repo.1),
+                    // This is unused for query.
+                    default_branch: "master".to_string(),
                 };
 
                 for QueryMap { name, kind, query } in queries {

+ 14 - 11
src/config.rs

@@ -1,5 +1,5 @@
 use crate::changelogs::ChangelogFormat;
-use crate::github::GithubClient;
+use crate::github::{GithubClient, Repository};
 use std::collections::{HashMap, HashSet};
 use std::fmt;
 use std::sync::{Arc, RwLock};
@@ -209,17 +209,20 @@ pub(crate) struct ReviewSubmittedConfig {
     pub(crate) reviewed_label: String,
 }
 
-pub(crate) async fn get(gh: &GithubClient, repo: &str) -> Result<Arc<Config>, ConfigurationError> {
-    if let Some(config) = get_cached_config(repo) {
-        log::trace!("returning config for {} from cache", repo);
+pub(crate) async fn get(
+    gh: &GithubClient,
+    repo: &Repository,
+) -> Result<Arc<Config>, ConfigurationError> {
+    if let Some(config) = get_cached_config(&repo.full_name) {
+        log::trace!("returning config for {} from cache", repo.full_name);
         config
     } else {
-        log::trace!("fetching fresh config for {}", repo);
+        log::trace!("fetching fresh config for {}", repo.full_name);
         let res = get_fresh_config(gh, repo).await;
         CONFIG_CACHE
             .write()
             .unwrap()
-            .insert(repo.to_string(), (res.clone(), Instant::now()));
+            .insert(repo.full_name.to_string(), (res.clone(), Instant::now()));
         res
     }
 }
@@ -246,15 +249,15 @@ fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationErro
 
 async fn get_fresh_config(
     gh: &GithubClient,
-    repo: &str,
+    repo: &Repository,
 ) -> Result<Arc<Config>, ConfigurationError> {
     let contents = gh
-        .raw_file(repo, "master", CONFIG_FILE_NAME)
+        .raw_file(&repo.full_name, &repo.default_branch, CONFIG_FILE_NAME)
         .await
         .map_err(|e| ConfigurationError::Http(Arc::new(e)))?
         .ok_or(ConfigurationError::Missing)?;
     let config = Arc::new(toml::from_slice::<Config>(&contents).map_err(ConfigurationError::Toml)?);
-    log::debug!("fresh configuration for {}: {:?}", repo, config);
+    log::debug!("fresh configuration for {}: {:?}", repo.full_name, config);
     Ok(config)
 }
 
@@ -273,10 +276,10 @@ impl fmt::Display for ConfigurationError {
             ConfigurationError::Missing => write!(
                 f,
                 "This repository is not enabled to use triagebot.\n\
-                 Add a `triagebot.toml` in the root of the master branch to enable it."
+                 Add a `triagebot.toml` in the root of the default branch to enable it."
             ),
             ConfigurationError::Toml(e) => {
-                write!(f, "Malformed `triagebot.toml` in master branch.\n{}", e)
+                write!(f, "Malformed `triagebot.toml` in default branch.\n{}", e)
             }
             ConfigurationError::Http(_) => {
                 write!(f, "Failed to query configuration for this repository.")

+ 6 - 5
src/github.rs

@@ -906,6 +906,7 @@ pub struct IssueSearchResult {
 #[derive(Clone, Debug, serde::Deserialize)]
 pub struct Repository {
     pub full_name: String,
+    pub default_branch: String,
 }
 
 #[derive(Copy, Clone)]
@@ -1225,12 +1226,12 @@ pub enum Event {
 }
 
 impl Event {
-    pub fn repo_name(&self) -> String {
+    pub fn repo(&self) -> &Repository {
         match self {
-            Event::Create(event) => event.repository.full_name.clone(),
-            Event::IssueComment(event) => event.repository.full_name.clone(),
-            Event::Issue(event) => event.repository.full_name.clone(),
-            Event::Push(event) => event.repository.full_name.clone(),
+            Event::Create(event) => &event.repository,
+            Event::IssueComment(event) => &event.repository,
+            Event::Issue(event) => &event.repository,
+            Event::Push(event) => &event.repository,
         }
     }
 

+ 4 - 1
src/handlers.rs

@@ -44,7 +44,10 @@ mod rustc_commits;
 mod shortcut;
 
 pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
-    let config = config::get(&ctx.github, &event.repo_name()).await;
+    let config = config::get(&ctx.github, event.repo()).await;
+    if let Err(e) = &config {
+        log::warn!("failed to load configuration: {e}");
+    }
     let mut errors = Vec::new();
 
     if let (Ok(config), Event::Issue(event)) = (config.as_ref(), event) {

+ 7 - 7
src/handlers/github_releases.rs

@@ -31,7 +31,7 @@ pub(super) async fn handle(
         format!(
             "failed to load changelog file {} from repo {} in branch {}",
             config.changelog_path,
-            event.repo_name(),
+            event.repo().full_name,
             config.changelog_branch
         )
     })?;
@@ -40,7 +40,7 @@ pub(super) async fn handle(
     log::debug!("loading the git tags");
     let tags = load_paginated(
         ctx,
-        &format!("repos/{}/git/matching-refs/tags", event.repo_name()),
+        &format!("repos/{}/git/matching-refs/tags", event.repo().full_name),
         |git_ref: &GitRef| {
             git_ref
                 .name
@@ -54,7 +54,7 @@ pub(super) async fn handle(
     log::debug!("loading the existing releases");
     let releases = load_paginated(
         ctx,
-        &format!("repos/{}/releases", event.repo_name()),
+        &format!("repos/{}/releases", event.repo().full_name),
         |release: &Release| release.tag_name.clone(),
     )
     .await?;
@@ -65,7 +65,7 @@ pub(super) async fn handle(
 
             if let Some(release) = releases.get(tag) {
                 if release.name != expected_name || release.body != expected_body {
-                    log::info!("updating release {} on {}", tag, event.repo_name());
+                    log::info!("updating release {} on {}", tag, event.repo().full_name);
                     let _: serde_json::Value = ctx
                         .octocrab
                         .patch(
@@ -81,11 +81,11 @@ pub(super) async fn handle(
                     continue;
                 }
             } else {
-                log::info!("creating release {} on {}", tag, event.repo_name());
+                log::info!("creating release {} on {}", tag, event.repo().full_name);
                 let e: octocrab::Result<serde_json::Value> = ctx
                     .octocrab
                     .post(
-                        format!("repos/{}/releases", event.repo_name()),
+                        format!("repos/{}/releases", event.repo().full_name),
                         Some(&serde_json::json!({
                             "tag_name": tag,
                             "name": expected_name,
@@ -125,7 +125,7 @@ async fn load_changelog(
     let resp = ctx
         .github
         .raw_file(
-            &event.repo_name(),
+            &event.repo().full_name,
             &config.changelog_branch,
             &config.changelog_path,
         )

+ 1 - 1
src/handlers/nominate.rs

@@ -60,7 +60,7 @@ pub(super) async fn handle_command(
                 &event.issue().unwrap(),
                 format!(
                     "This team (`{}`) cannot be nominated for via this command;\
-                     it may need to be added to `triagebot.toml` on the master branch.",
+                     it may need to be added to `triagebot.toml` on the default branch.",
                     cmd.team,
                 ),
             );

+ 1 - 1
src/handlers/ping.rs

@@ -41,7 +41,7 @@ pub(super) async fn handle_command(
                 &event.issue().unwrap(),
                 format!(
                     "This team (`{}`) cannot be pinged via this command; \
-                    it may need to be added to `triagebot.toml` on the master branch.",
+                    it may need to be added to `triagebot.toml` on the default branch.",
                     team_name.team,
                 ),
             );