diff --git a/Cargo.lock b/Cargo.lock index 463fe78..6d05bff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,7 +48,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bs-telegram-bot" -version = "0.7.1" +version = "0.7.2" dependencies = [ "env_logger", "futures", diff --git a/Cargo.toml b/Cargo.toml index c22f1c4..63505a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bs-telegram-bot" -version = "0.7.1" +version = "0.7.2" authors = ["Pig Fang "] edition = "2021" publish = false diff --git a/src/diff.rs b/src/diff.rs index 86db693..452c52f 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -2,6 +2,7 @@ use crate::bot::{Bot, BotError}; use crate::github::{ self, compare::{compare, Compare}, + last_run_id, }; use itertools::Itertools; use thiserror::Error; @@ -10,9 +11,8 @@ pub async fn execute( bot: &Bot, base: Option, head: Option, - run_id: Option, ) -> Result<(), DiffError> { - let git = git(base, head, run_id).await?; + let git = git(base, head).await?; bot.send_message(git, "HTML").await.map_err(DiffError::from) } @@ -25,11 +25,7 @@ pub enum DiffError { Bot(#[from] BotError), } -async fn git( - base: Option, - head: Option, - run_id: Option, -) -> Result { +async fn git(base: Option, head: Option) -> Result { let base = if let Some(base) = base { base } else { @@ -45,7 +41,7 @@ async fn git( let Compare { commits, files } = compare(&base, &head).await?; let log = format_log(&commits); let analysis = analyze_diff(diff(&files)); - let artifact = format!("快照下载", get_artifact_link(run_id)); + let artifact = format!("快照下载", get_artifact_link().await); Ok(format!("{}\n{}\n{}", log, analysis, artifact)) } @@ -153,16 +149,15 @@ fn format_log(log: &[github::Commit]) -> String { .join("\n") } -fn get_artifact_link(run_id: Option) -> String { - if let Some(run_id) = run_id { - format!( - "https://nightly.link/bs-community/blessing-skin-server/actions/runs/{}/artifact.zip", - run_id - ) - } else { - "https://nightly.link/bs-community/blessing-skin-server/workflows/CI/dev/artifact.zip" - .to_string() - } +async fn get_artifact_link() -> String { + let run_id = last_run_id(github::WORKFLOW_ID, "dev") + .await + .expect("Failed to get last run id") + .unwrap(); + format!( + "https://nightly.link/bs-community/blessing-skin-server/actions/runs/{}/artifact.zip", + run_id + ) } fn md2html(text: String) -> String { diff --git a/src/github/checks.rs b/src/github/checks.rs index 34b09fd..4d33217 100644 --- a/src/github/checks.rs +++ b/src/github/checks.rs @@ -8,7 +8,7 @@ struct CheckSuite { #[derive(Deserialize)] struct CheckSuites { - total_count: u8, + total_count: u32, check_suites: Vec, } @@ -36,3 +36,42 @@ pub async fn last_checked_commit() -> Result, reqwest::Error> { }; Ok(sha) } + +#[derive(Deserialize)] +struct WorkflowRun { + id: u64, + workflow_id: u16, +} + +#[derive(Deserialize)] +struct Runs { + total_count: u32, + workflow_runs: Vec, +} + +pub async fn last_run_id(id: &u16, branch: &str) -> Result, reqwest::Error> { + let client = reqwest::ClientBuilder::new() + .user_agent(USER_AGENT) + .build()?; + + let Runs { + total_count, + workflow_runs, + } = client + .get(&format!("{}/actions/runs?branch={}", BASE_URL, branch)) + .header("Accept", "application/vnd.github.antiope-preview+json") + .send() + .await? + .json() + .await?; + + let run_id = if total_count == 0 { + None + } else { + workflow_runs + .into_iter() + .find(|run| run.workflow_id == id.to_owned()) + .map(|run| run.id) + }; + Ok(run_id) +} diff --git a/src/github/mod.rs b/src/github/mod.rs index c2cde3c..9120120 100644 --- a/src/github/mod.rs +++ b/src/github/mod.rs @@ -3,12 +3,14 @@ use serde::Deserialize; mod checks; pub mod compare; -pub use checks::last_checked_commit; +pub use checks::{last_checked_commit, last_run_id}; pub const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); pub const BASE_URL: &str = "https://api.github.com/repos/bs-community/blessing-skin-server"; +pub const WORKFLOW_ID: &u16 = &6505; + #[derive(Deserialize)] pub struct Commit { pub sha: String, diff --git a/src/main.rs b/src/main.rs index a25cc7a..57aac5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,8 +46,7 @@ async fn main() -> Result<(), MainError> { "diff" | "core" => { let base = args.next(); let head = args.next(); - let run_id = env::var("GITHUB_RUN_ID").ok(); - diff::execute(&bot, base, head, run_id) + diff::execute(&bot, base, head) .await .map_err(MainError::from) }