From 18113e28f365000462dbcd725120524cd60fca0a Mon Sep 17 00:00:00 2001 From: Julian van Dijk Date: Thu, 5 Dec 2024 22:31:50 +0100 Subject: [PATCH] add docker and ci --- .dockerignore | 4 +++ .github/workflows/ci.yaml | 40 +++++++++++++++++++++++++++++ .github/workflows/docker.yaml | 37 +++++++++++++++++++++++++++ Dockerfile | 10 ++++++++ src/main.rs | 47 +++++++++++++++++++++-------------- 5 files changed, 119 insertions(+), 19 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/docker.yaml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3c49e12 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +target +.git +.idea +.vscode \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..6827967 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,40 @@ +name: CI + +on: + push: + branches: + - main + pull_request_target: + branches: + - main + +jobs: + common: + name: common + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{github.event.pull_request.head.repo.full_name}} + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: rustfmt, clippy + - uses: actions-rs/cargo@v1 + name: format check + with: + command: fmt + args: --all -- --check + - uses: actions-rs/cargo@v1 + name: clippy check + with: + command: clippy + args: -- -D warnings \ No newline at end of file diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..4ada7da --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,37 @@ +name: Docker +on: + push: + branches: + - main + schedule: + - cron: "54 2 2 * *" + workflow_dispatch: +jobs: + buildDockerImage: + name: Build Docker image + runs-on: ubuntu-latest + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: ghcr.io/wisvch/aocbot + tags: type=sha, prefix={{date 'YYYYMMDD'}}- + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + push: ${{ github.ref == 'refs/heads/main' }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f892dc3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM rust:latest AS builder + +WORKDIR /usr/src/aocbot +COPY . . +RUN cargo install --path . + +FROM rust:1-alpine3.20 + +COPY --from=builder /usr/local/cargo/bin/aocbot /usr/local/bin/aocbot +CMD ["aocbot"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0f06433..8e03ae4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,24 +52,28 @@ async fn leaderboard_total( ) -> Result<(), Error> { let aoch_data = get_leaderboard_data().await; - let start_width: usize = aoch_data.total.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap(); let max_stars: usize = aoch_data.total.iter().map(|x| {x.stars.clone().iter().sum::()}).max().unwrap() as usize; + let row_size: usize= start_width+11+max_stars; + let row_count = 1900/row_size; + + let mut table_rows = vec![]; + table_rows.push(format!("{1:0$} | {2:5} | {3:5}",start_width ,"Name" ,"Score" ,"Stars")); + table_rows.push(format!("{:-^row_size$}", "")); - let mut l= aoch_data.total.into_iter().map(|x| { + table_rows.append(aoch_data.total.into_iter().map(|x| { let name: String = remove_emoji(&x.name.unwrap_or_else(|| ANON_USER.to_string())); + let star_count = x.stars.iter().sum(); let mut stars: String = String::new(); for _ in 0..star_count { stars += "*"; } - let mut width = start_width; - format!("{name:0$} | {1:5} | {stars} ",width ,x.score) - }).collect::>(); - let row_size: usize= start_width+11+max_stars; - let row_count = 1900/row_size; - let mut stylized_answer = l[0..row_count].join("\n"); + format!("{name:0$} | {1:5} | {stars} ",start_width ,x.score) + }).collect::>().as_mut()); + + let mut stylized_answer = table_rows[0..row_count].join("\n"); stylized_answer = format!("```\n{}```", truncate(&*stylized_answer, 1990)); ctx.say(stylized_answer).await?; @@ -81,15 +85,19 @@ async fn leaderboard_total( async fn leaderboard_today( ctx: Context<'_> ) -> Result<(), Error> { - let resp = reqwest::get("https://aoch.wisv.ch/data").await?.text().await?; - let p: AochData = serde_json::from_str(&*resp).unwrap(); + let aoch_data = get_leaderboard_data().await; + + let start_width = aoch_data.today.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap(); + let row_size: usize= start_width+16; + let row_count = 1900/row_size; - let ANON_USER: &str = "Anon"; - let start_width = p.today.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap(); + let mut table_rows = vec![]; + table_rows.push(format!("{1:0$} | {2:5} | {3:5}",start_width ,"Name" ,"Stars" ,"Score")); + table_rows.push(format!("{:-^row_size$}", "")); - let l= p.today.into_iter().map(|x| { + table_rows.append(aoch_data.today.into_iter().map(|x| { let name: String = remove_emoji(&x.name.unwrap_or_else(|| ANON_USER.to_string())); - let mut width = start_width; + let mut stars: String = String::new(); if x.star1.is_some(){ stars += "*"; @@ -97,11 +105,12 @@ async fn leaderboard_today( if x.star2.is_some() { stars += "*"; } - format!("{name:0$} | {stars:2} | {1:5}",width ,x.score) - }).collect::>(); - let row_size: usize= start_width+13; - let row_count = 1900/row_size; - let mut stylized_answer = l[0..row_count].join("\n"); + + format!("{name:0$} | {stars:^5} | {1:<5}",start_width ,x.score) + }).collect::>().as_mut()); + + + let mut stylized_answer = table_rows[0..row_count].join("\n"); stylized_answer = format!("```\n{}```", truncate(&*stylized_answer, 1990)); println!("{}", stylized_answer); ctx.say(stylized_answer).await?;