Skip to content

Commit

Permalink
merge(better_actions): fix actions & add fmt checks (#25);
Browse files Browse the repository at this point in the history
closes #23 

I refactored all GitHub actions, I don't really know if they will work
but meh.
I also added an action that runs `cargo fmt --check`

The goal is to make them more reliable and fix this error:
```
Error: Resource not accessible by integration
```
  • Loading branch information
5-pebbles authored Apr 30, 2024
2 parents af8825e + 21a383f commit 4acacf5
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 65 deletions.
69 changes: 39 additions & 30 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,49 @@ env:
CARGO_TERM_COLOR: always

jobs:
tests:
clippy:
runs-on: ubuntu-latest

permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Rust Toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install Hurl
uses: gacts/install-hurl@v1
with:
version: 4.1.0
- name: Run Tests
uses: actions-rs/cargo@v1
with:
command: test
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Cargo Cache
uses: swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Install SARIF tools
run: cargo install clippy-sarif sarif-fmt
- name: Run Clippy
run: >
cargo clippy --all-features --all --message-format=json
| clippy-sarif
| tee clippy-results.sarif
| sarif-fmt
continue-on-error: true
- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: clippy-results.sarif
wait-for-processing: true

lints:
rustfmt:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Rust Toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
override: true
- name: Run Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features -- -D warnings
- name: Checkout Code
uses: actions/checkout@v4
- name: Rust Toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Run Rustfmt
run: cargo fmt --check
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "**" ]

env:
CARGO_TERM_COLOR: always

jobs:
tests:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Rust Toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install Hurl
uses: gacts/install-hurl@v1
with:
version: 4.1.0
- name: Build Tests
run: cargo test --no-run
- name: Run Tests
run: cargo test --no-fail-fast
9 changes: 4 additions & 5 deletions src/api/data/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ pub enum Permission {
/// or an error if the permissions could not be extracted or converted.
///
pub fn permissions_from_row(row: &Row) -> Result<Vec<Permission>, Error> {
Ok(row.get::<&str, Option<String>>("permissions")?.map_or_else(
Vec::new,
|v| {
Ok(row
.get::<&str, Option<String>>("permissions")?
.map_or_else(Vec::new, |v| {
v.split(',')
.filter_map(|s| Permission::from_str(s).ok())
.collect()
},
))
}))
}
2 changes: 1 addition & 1 deletion src/api/data/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rocket::{
request::{self, FromRequest, Request},
};
use rocket_sync_db_pools::rusqlite::{params, params_from_iter, Error, Row, Transaction};
use utoipa::{ToSchema, schema};
use utoipa::{schema, ToSchema};

use crate::{
api::data::permissions::{permissions_from_row, Permission},
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/albums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use rocket_sync_db_pools::rusqlite::{params, Error::QueryReturnedNoRows, ToSql};

use crate::{
api::data::{albums::Album, permissions::Permission, users::User},
error::ApiError,
database::MyDatabase,
error::ApiError,
};

type Result<T> = std::result::Result<T, ApiError>;
Expand Down
4 changes: 2 additions & 2 deletions src/api/endpoints/artists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use rocket::{fairing::AdHoc, http::Status, serde::json::Json};
use rocket_sync_db_pools::rusqlite::{params, Error::QueryReturnedNoRows, ToSql};

use crate::{
error::ApiError,
database::MyDatabase,
api::data::{artists::Artist, permissions::Permission, users::User},
database::MyDatabase,
error::ApiError,
};

type Result<T> = std::result::Result<T, ApiError>;
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
permissions::{permissions_from_row, Permission},
users::User,
},
error::ApiError,
database::MyDatabase,
error::ApiError,
};

type Result<T> = std::result::Result<T, ApiError>;
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use rocket_sync_db_pools::rusqlite::{params, Error::QueryReturnedNoRows, ToSql};

use crate::{
api::data::{permissions::Permission, tracks::Track, users::User},
error::ApiError,
database::MyDatabase,
error::ApiError,
};

type Result<T> = std::result::Result<T, ApiError>;
Expand Down
23 changes: 11 additions & 12 deletions src/api/endpoints/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use rocket_sync_db_pools::rusqlite::{params, params_from_iter};
use strum::IntoEnumIterator;

use crate::{
error::ApiError,
database::MyDatabase,
api::data::{
permissions::{Permission, permissions_from_row},
permissions::{permissions_from_row, Permission},
users::{DangerousLogin, User},
},
database::MyDatabase,
error::ApiError,
};

type Result<T> = std::result::Result<T, ApiError>;

/// Creates the first user in the database.
///
/// This endpoint only works if the database is empty.
/// It allows the creation of the first user, who can then invite all other users.
/// This endpoint only works if the database is empty.
/// It allows the creation of the first user, who can then invite all other users.
/// The first user has all permissions available.
#[utoipa::path(
request_body(
Expand All @@ -43,7 +43,7 @@ async fn user_init(db: MyDatabase, login: Json<DangerousLogin>) -> Result<()> {
)? {
Err(Status::Conflict)?
};

login.insert_user_into_transaction(Permission::iter(), &tx)?;

tx.commit()?;
Expand Down Expand Up @@ -144,14 +144,13 @@ async fn user_delete(db: MyDatabase, user: User, username: &str) -> Result<()> {

if username != user.username {
// we cant select directly from the user_permissions table because the user might not have any permissions
let mut required_permissions = tx
.query_row(
"SELECT GROUP_CONCAT(DISTINCT user_permissions.id) AS permissions FROM users
let mut required_permissions = tx.query_row(
"SELECT GROUP_CONCAT(DISTINCT user_permissions.id) AS permissions FROM users
LEFT JOIN user_permissions ON users.username = user_permissions.username
WHERE users.username = ? GROUP BY users.username",
params![username],
permissions_from_row,
)?;
params![username],
permissions_from_row,
)?;

required_permissions.push(Permission::UserDelete);

Expand Down
6 changes: 1 addition & 5 deletions src/database/connection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use rocket::{serde, Build, Rocket};
use rocket_sync_db_pools::{
r2d2,
rusqlite,
Config, PoolResult, Poolable,
};
use rocket_sync_db_pools::{r2d2, rusqlite, Config, PoolResult, Poolable};

use std::ops::{Deref, DerefMut};
use std::time::Duration;
Expand Down
9 changes: 4 additions & 5 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ impl MyDatabase {

pub fn fairing() -> AdHoc {
AdHoc::on_ignite("Database Systems", |rocket| async {
rocket.attach(MyDatabase::fairing()).attach(AdHoc::on_ignite(
"Database Migrations",
|rocket| async {
rocket
.attach(MyDatabase::fairing())
.attach(AdHoc::on_ignite("Database Migrations", |rocket| async {
<MyDatabase>::get_one(&rocket)
.await
.expect("Mount Database")
.migrations()
.await
.expect("Database Migrations Failed");
rocket
},
))
}))
})
}
2 changes: 1 addition & 1 deletion src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::api::{
permissions::Permission,
users::{DangerousLogin, User},
},
endpoints::{genres, invites, permissions, tokens, users, audio},
endpoints::{audio, genres, invites, permissions, tokens, users},
};

#[derive(OpenApi)]
Expand Down
3 changes: 2 additions & 1 deletion tests/hurl_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn hurl_tests() {
.unwrap_or_else(|_| panic!("Failed to kill cargo: pid = {}", cargo_pid));
panic!("No stdout to parse...");
}
}).lines();
})
.lines();

for line in lines {
match line {
Expand Down

0 comments on commit 4acacf5

Please sign in to comment.