Skip to content

Commit

Permalink
ci: add tests and update CI file
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoOwO committed Jan 3, 2025
1 parent d918956 commit 3012f47
Show file tree
Hide file tree
Showing 20 changed files with 873 additions and 186 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL=sqlite:data/bot.db
DATABASE_URL=sqlite:data/bot.db
CONFIG_PATH=data/config.yaml
33 changes: 21 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ on:

env:
CARGO_TERM_COLOR: always
DATABASE_URL: sqlite:data/bot.db
DATABASE_URL: sqlite:/tmp/bot.db
CONFIG_PATH: /tmp/config.yaml
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

Expand All @@ -28,12 +29,12 @@ jobs:
- name: Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Create data directory
run: mkdir -p data
- name: Copy config
run: cp config.example.yaml /tmp/config.yaml

- name: Install sqlx-cli
run: cargo install sqlx-cli

- name: Create database
run: |
sqlx database create
Expand All @@ -59,15 +60,26 @@ jobs:

- name: Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Copy config
run: cp config.example.yaml /tmp/config.yaml

- name: Install sqlx-cli
run: cargo install sqlx-cli

- name: Create database
run: |
sqlx database create
sqlx migrate run
- name: Build
run: cargo build --release

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: invitationbot
path: target/release/invitationbot
name: InvitationBot
path: target/release/InvitationBot

release:
name: Release
Expand All @@ -82,20 +94,17 @@ jobs:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: invitationbot
name: InvitationBot
path: ./

- name: Make binary executable
run: chmod +x invitationbot
run: chmod +x InvitationBot

- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: |
invitationbot
LICENSE
README.md
config.example.yaml
InvitationBot
generate_release_notes: true

docker:
Expand Down
39 changes: 38 additions & 1 deletion Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ axum = "0.8.1"
tower-http = { version = "0.6.2", features = ["cors"] }
chrono = { version = "0.4", features = ["serde"] }
once_cell = "1.20.2"
rust-embed = "8.0"
rust-embed = "8.0"

[dev-dependencies]
tempfile = "3.9"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
uuid = { version = "1.0", features = ["v4"] }
tower = { version = "0.4", features = ["util"] }
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.75-slim-bookworm as builder
FROM rust:1.83-slim-bookworm as builder

WORKDIR /usr/src/app
COPY . .
Expand All @@ -8,18 +8,24 @@ RUN apt-get update && apt-get install -y \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*

ENV DATABASE_URL=sqlite:/tmp/bot.db
RUN cargo install sqlx-cli && \
sqlx database create && \
sqlx migrate run

RUN cargo build --release

# Use distroless as runtime image
FROM gcr.io/distroless/cc-debian12

WORKDIR /app

COPY --from=builder /usr/src/app/target/release/invitationbot /app/
COPY --from=builder /usr/src/app/target/release/InvitationBot /app/
COPY --from=builder /usr/src/app/migrations /app/migrations

ENV DATABASE_URL=sqlite:data/bot.db
ENV CONFIG_PATH=data/config.yaml

VOLUME ["/app/data"]
USER nonroot
ENTRYPOINT ["/app/invitationbot"]
ENTRYPOINT ["/app/InvitationBot"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cp config.example.yaml data/config.yaml

# Set up the database
echo "DATABASE_URL=sqlite:data/bot.db" > .env
echo "CONFIG_PATH=data/config.yaml" >> .env
cargo install sqlx-cli
sqlx database create
sqlx migrate run
Expand Down
13 changes: 8 additions & 5 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use poise::serenity_prelude::{self as serenity};
use crate::Data;
use poise::serenity_prelude::{self as serenity};

pub async fn handle_guild_member_addition(
ctx: &serenity::Context,
Expand All @@ -11,7 +11,9 @@ pub async fn handle_guild_member_addition(
if let Ok(invites) = guild_id.invites(&ctx.http).await {
for invite in invites {
// Check if this is our invite code
if let Ok(Some(invite_id)) = crate::utils::db::find_invite_by_code(&data.db, &invite.code).await {
if let Ok(Some(invite_id)) =
crate::utils::db::find_invite_by_code(&data.db, &invite.code).await
{
// Check if this invite is set to 2 uses, has been used once, and was created by us
// If these conditions are met, we can assume this invite was used by the new member
if invite.max_uses == 2 && invite.uses == 1 {
Expand All @@ -20,13 +22,14 @@ pub async fn handle_guild_member_addition(
&data.db,
&invite_id,
&new_member.user.id.to_string(),
).await;

)
.await;

// Delete the invite link
let _ = invite.delete(&ctx.http).await;
break;
}
}
}
}
}
}
Loading

0 comments on commit 3012f47

Please sign in to comment.