Skip to content

Commit

Permalink
fix: fetch corresponding run instead of current
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Apr 17, 2022
1 parent c78f514 commit a95983e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bs-telegram-bot"
version = "0.7.1"
version = "0.7.2"
authors = ["Pig Fang <[email protected]>"]
edition = "2021"
publish = false
Expand Down
31 changes: 13 additions & 18 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,9 +11,8 @@ pub async fn execute(
bot: &Bot,
base: Option<String>,
head: Option<String>,
run_id: Option<String>,
) -> 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)
}

Expand All @@ -25,11 +25,7 @@ pub enum DiffError {
Bot(#[from] BotError),
}

async fn git(
base: Option<String>,
head: Option<String>,
run_id: Option<String>,
) -> Result<String, reqwest::Error> {
async fn git(base: Option<String>, head: Option<String>) -> Result<String, reqwest::Error> {
let base = if let Some(base) = base {
base
} else {
Expand All @@ -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!("<a href=\"{}\">快照下载</a>", get_artifact_link(run_id));
let artifact = format!("<a href=\"{}\">快照下载</a>", get_artifact_link().await);

Ok(format!("{}\n{}\n{}", log, analysis, artifact))
}
Expand Down Expand Up @@ -153,16 +149,15 @@ fn format_log(log: &[github::Commit]) -> String {
.join("\n")
}

fn get_artifact_link(run_id: Option<String>) -> 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 {
Expand Down
41 changes: 40 additions & 1 deletion src/github/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct CheckSuite {

#[derive(Deserialize)]
struct CheckSuites {
total_count: u8,
total_count: u32,
check_suites: Vec<CheckSuite>,
}

Expand Down Expand Up @@ -36,3 +36,42 @@ pub async fn last_checked_commit() -> Result<Option<String>, reqwest::Error> {
};
Ok(sha)
}

#[derive(Deserialize)]
struct WorkflowRun {
id: u64,
workflow_id: u16,
}

#[derive(Deserialize)]
struct Runs {
total_count: u32,
workflow_runs: Vec<WorkflowRun>,
}

pub async fn last_run_id(id: &u16, branch: &str) -> Result<Option<u64>, 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)
}
4 changes: 3 additions & 1 deletion src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a95983e

Please sign in to comment.