diff options
| author | seth <[email protected]> | 2024-06-16 07:15:13 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-16 07:15:13 -0400 |
| commit | d25129d829e0ebd70b4e60e399fe91c0d80aa1ad (patch) | |
| tree | 2a62992f2980f9fed2204ef5ef708a0228998cf1 /src/command/track.rs | |
| parent | a0bfcc1587e3cef1b8f6fa0508a280fc48c82231 (diff) | |
use libgit2 to track PRs (#10)v0.2.0
* nix: don't depend on registry for nixpkgs input
* use libgit2 to track PRs
* nix: don't use ci devShell as defaul
* crates: bump serenity from `9ad74d4` to `0.12.2
* nix: fix cross compiled builds
* crates: split more from client
* bot-jobs: update remote refs more efficiently
* git-tracker: account for HEAD commits
* bot-config: use nixpkgs branches from environment
* bot-commands: don't display branches prs haven't landed in
* git-tracker: return false when commits aren't found
this is annoying as a hard error since it turns out github will report
garbage merge commit SHAs for PRs that *haven't* been merged yet. yay
* bot: improve docs in some places
* bot-client: display invite link on start
* bot-http: add TeawieClientExt
* bot-commands: add /about
* docs: update readme todos
* nix: enable StateDirectory in module
* crates: bump to 0.2.0
Diffstat (limited to 'src/command/track.rs')
| -rw-r--r-- | src/command/track.rs | 139 |
1 files changed, 0 insertions, 139 deletions
diff --git a/src/command/track.rs b/src/command/track.rs deleted file mode 100644 index 45715f4..0000000 --- a/src/command/track.rs +++ /dev/null @@ -1,139 +0,0 @@ -use crate::http::{Client, GithubClientExt, GITHUB_URL}; - -use eyre::Result; -use futures::future::try_join_all; -use serenity::all::CreateEmbed; -use serenity::builder::{CreateCommand, CreateCommandOption, CreateInteractionResponseFollowup}; -use serenity::model::application::{ - CommandInteraction, CommandOptionType, InstallationContext, ResolvedOption, ResolvedValue, -}; -use serenity::prelude::Context; -use tracing::{instrument, trace}; - -/// All of our tracked branches in nixpkgs -const BRANCHES: [&str; 8] = [ - "master", - "staging", - "nixos-unstable", - "nixos-unstable-small", - "nixos-24.05-small", - "release-24.05", - "nixos-23.11-small", - "release-23.11", -]; - -#[derive(Clone, Debug, Default)] -struct BranchStatus { - repo_owner: String, - repo_name: String, - name: String, -} - -impl BranchStatus { - fn new(repo_owner: String, repo_name: String, name: String) -> Self { - Self { - repo_owner, - repo_name, - name, - } - } - - /// Make a nice friendly string displaying if this branch has a PR merged into it - fn to_status_string(&self, has_pr: bool) -> String { - let emoji = if has_pr { "✅" } else { "❌" }; - format!("`{}` {emoji}", &self.name) - } - - /// Check if this branch has the specified pull request merged into it - #[instrument(skip(http))] - async fn has_pr(&self, http: &Client, pr: u64) -> Result<bool> { - let commit = http - .merge_commit_for( - &self.repo_owner, - &self.repo_name, - u64::try_from(pr).unwrap(), - ) - .await?; - - let has_pr = http - .is_commit_in_branch(&self.repo_owner, &self.repo_name, &self.name, &commit) - .await?; - - Ok(has_pr) - } -} - -/// async wrapper for [BranchStatus::to_status_string()] -#[instrument(skip(http))] -async fn collect_status( - http: &Client, - repo_owner: String, - repo_name: String, - branch: String, - pr: u64, -) -> Result<String> { - let status = BranchStatus::new(repo_owner, repo_name, branch); - let has_pr = status.has_pr(http, pr).await?; - let res = status.to_status_string(has_pr); - - Ok(res) -} - -#[instrument(skip_all)] -pub async fn respond(ctx: &Context, http: &Client, command: &CommandInteraction) -> Result<()> { - trace!("Responding to track command"); - - // this will probably take a while - command.defer(&ctx).await?; - - // TODO: make these configurable for nixpkgs forks...or other github repos ig - const REPO_OWNER: &str = "NixOS"; - const REPO_NAME: &str = "nixpkgs"; - - let options = command.data.options(); - - let response = if let Some(ResolvedOption { - value: ResolvedValue::Integer(pr), - .. - }) = options.first() - { - if *pr < 0 { - CreateInteractionResponseFollowup::new().content("PR numbers aren't negative...") - } else { - // TODO: this is gross - let statuses = try_join_all(BRANCHES.iter().map(|&branch| { - collect_status( - http, - REPO_OWNER.to_string(), - REPO_NAME.to_string(), - branch.to_string(), - u64::try_from(*pr).unwrap(), - ) - })) - .await?; - - let embed = CreateEmbed::new() - .title(format!("Nixpkgs PR #{} Status", *pr)) - .url(format!("{GITHUB_URL}/{REPO_OWNER}/{REPO_NAME}/pull/{}", pr)) - .description(statuses.join("\n")); - - CreateInteractionResponseFollowup::new().embed(embed) - } - } else { - CreateInteractionResponseFollowup::new().content("Please provide a valid commit!") - }; - - command.create_followup(&ctx, response).await?; - - Ok(()) -} - -pub fn register() -> CreateCommand { - CreateCommand::new("track") - .description("Track a nixpkgs PR") - .add_integration_type(InstallationContext::User) - .add_option( - CreateCommandOption::new(CommandOptionType::Integer, "pull_request", "PR to track") - .required(true), - ) -} |
