Skip to content

Commit 91c2cfb

Browse files
committed
Merge pull request #20 from amfoss/develop
release v1.1.0
2 parents 4d912f4 + 0d5b1a6 commit 91c2cfb

File tree

15 files changed

+395
-309
lines changed

15 files changed

+395
-309
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
on:
2+
pull_request:
3+
branches: [ "main", "develop" ]
4+
5+
name: Cargo Clippy and Format
6+
jobs:
7+
run_clippy_and_format:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v1
11+
12+
- run: rustup component add clippy
13+
14+
- uses: actions-rs/clippy-check@v1
15+
with:
16+
token: ${{ secrets.GITHUB_TOKEN }}
17+
args: --all-features
18+
19+
- name: Check formatting with rustfmt
20+
run: cargo fmt -- --check

.github/workflows/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "cargo"
4+
directory: "/" # Location of package manifests
5+
schedule:
6+
interval: "weekly"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Release on Tag Push
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
release:
10+
name: Build & Release ${{ matrix.target }}
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
target: [aarch64-apple-darwin, x86_64-pc-windows-gnu, x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Add targets
22+
run: rustup target add "${{ matrix.target }}"
23+
24+
- name: Compile binary
25+
run: cargo build --release --target ${{ matrix.target }}
26+
27+
- name: Upload GitHub Release
28+
uses: softprops/action-gh-release@v2
29+
with:
30+
files: target/${{ matrix.target }}/release/*
31+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/rust.yml renamed to .github/workflows/rust-build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name: Rust
33
on:
44
pull_request:
55
branches: [ "main", "develop" ]
6-
push:
7-
branches: [ "main", "develop" ]
86

97
env:
108
CARGO_TERM_COLOR: always

Cargo.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ anyhow = "1.0.95"
88
async-trait = "0.1.83"
99
chrono = "0.4.38"
1010
chrono-tz = "0.10.0"
11-
poise = "0.6.1"
1211
reqwest = { version = "0.12.5", features = ["json"] }
1312
serde = { version = "1.0.203", features = ["derive"] }
1413
serde_json = "1.0.117"
15-
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "current" }
1614
tokio = { version = "1.26.0", features = ["rt-multi-thread", "macros"] }
1715
tracing = "0.1.37"
1816
dotenv = "0.15.0"
19-
20-
[patch.crates-io]
21-
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "current" }
17+
serenity = { version = "0.12.4", features = ["chrono"] }
18+
poise = "0.6.1"

config.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1334963922205147176
2-
1334963925585891419
3-
1334963927678849145
4-
1334963929922670682
1+
1336777074354028574
2+
1336746702178095144
3+
1336752144560160823
4+
1336036250590904332

src/commands.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ async fn amdctl(ctx: Context<'_>) -> Result<(), Error> {
2323
Ok(())
2424
}
2525

26-
/// Every function that is defined *should* be added to the
27-
/// returned vector in get_commands to ensure it is registered (available for the user)
28-
/// when the bot goes online.
26+
/// Returns a vector containg [Poise Commands][`poise::Command`]
2927
pub fn get_commands() -> Vec<poise::Command<Data, Error>> {
3028
vec![amdctl()]
3129
}

src/graphql/models.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ pub struct Streak {
2525
pub max_streak: i32,
2626
}
2727

28+
/// Represents a record of the Member relation in [Root][https://www.github.com/amfoss/root].
2829
#[derive(Clone, Debug, Deserialize)]
2930
pub struct Member {
3031
#[serde(rename = "memberId")]
3132
pub member_id: i32,
3233
pub name: String,
3334
#[serde(rename = "discordId")]
3435
pub discord_id: String,
35-
#[serde(rename = "groupId")]
36-
pub group_id: u32,
3736
#[serde(default)]
38-
pub streak: Vec<Streak>,
37+
pub streak: Vec<Streak>, // Note that Root will NOT have multiple Streak elements but it may be an empty list which is why we use a vector here
3938
}

src/graphql/queries.rs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818
use crate::graphql::models::{Member, Streak};
19-
use anyhow::Context;
19+
use anyhow::{anyhow, Context};
2020

21-
pub async fn fetch_members() -> Result<Vec<Member>, anyhow::Error> {
22-
let request_url = std::env::var("ROOT_URL").expect("ROOT_URL not found");
21+
pub async fn fetch_members() -> anyhow::Result<Vec<Member>> {
22+
let request_url = std::env::var("ROOT_URL").context("ROOT_URL not found in ENV")?;
2323

2424
let client = reqwest::Client::new();
2525
let query = r#"
@@ -28,7 +28,6 @@ pub async fn fetch_members() -> Result<Vec<Member>, anyhow::Error> {
2828
memberId
2929
name
3030
discordId
31-
groupId
3231
streak {
3332
currentStreak
3433
maxStreak
@@ -43,6 +42,13 @@ pub async fn fetch_members() -> Result<Vec<Member>, anyhow::Error> {
4342
.await
4443
.context("Failed to successfully post request")?;
4544

45+
if !response.status().is_success() {
46+
return Err(anyhow!(
47+
"Server responded with an error: {:?}",
48+
response.status()
49+
));
50+
}
51+
4652
let response_json: serde_json::Value = response
4753
.json()
4854
.await
@@ -52,7 +58,12 @@ pub async fn fetch_members() -> Result<Vec<Member>, anyhow::Error> {
5258
.get("data")
5359
.and_then(|data| data.get("members"))
5460
.and_then(|members| members.as_array())
55-
.ok_or_else(|| anyhow::anyhow!("Malformed response: 'members' field missing or invalid"))?;
61+
.ok_or_else(|| {
62+
anyhow::anyhow!(
63+
"Malformed response: Could not access Members from {}",
64+
response_json
65+
)
66+
})?;
5667

5768
let members: Vec<Member> = serde_json::from_value(serde_json::Value::Array(members.clone()))
5869
.context("Failed to parse 'members' into Vec<Member>")?;
@@ -61,7 +72,7 @@ pub async fn fetch_members() -> Result<Vec<Member>, anyhow::Error> {
6172
}
6273

6374
pub async fn increment_streak(member: &mut Member) -> anyhow::Result<()> {
64-
let request_url = std::env::var("ROOT_URL").context("ROOT_URL was not found")?;
75+
let request_url = std::env::var("ROOT_URL").context("ROOT_URL was not found in ENV")?;
6576

6677
let client = reqwest::Client::new();
6778
let mutation = format!(
@@ -73,12 +84,20 @@ pub async fn increment_streak(member: &mut Member) -> anyhow::Result<()> {
7384
}}"#,
7485
member.member_id
7586
);
87+
7688
let response = client
7789
.post(request_url)
7890
.json(&serde_json::json!({"query": mutation}))
7991
.send()
8092
.await
81-
.context("Root Request failed")?;
93+
.context("Failed to succesfully post query to Root")?;
94+
95+
if !response.status().is_success() {
96+
return Err(anyhow!(
97+
"Server responded with an error: {:?}",
98+
response.status()
99+
));
100+
}
82101

83102
// Handle the streak vector
84103
if member.streak.is_empty() {
@@ -101,7 +120,7 @@ pub async fn increment_streak(member: &mut Member) -> anyhow::Result<()> {
101120
}
102121

103122
pub async fn reset_streak(member: &mut Member) -> anyhow::Result<()> {
104-
let request_url = std::env::var("ROOT_URL").context("ROOT_URL was not found")?;
123+
let request_url = std::env::var("ROOT_URL").context("ROOT_URL was not found in the ENV")?;
105124

106125
let client = reqwest::Client::new();
107126
let mutation = format!(
@@ -120,34 +139,50 @@ pub async fn reset_streak(member: &mut Member) -> anyhow::Result<()> {
120139
.json(&serde_json::json!({ "query": mutation }))
121140
.send()
122141
.await
123-
.context("Root Request failed")?;
142+
.context("Failed to succesfully post query to Root")?;
143+
144+
if !response.status().is_success() {
145+
return Err(anyhow!(
146+
"Server responded with an error: {:?}",
147+
response.status()
148+
));
149+
}
124150

125151
let response_json: serde_json::Value = response
126152
.json()
127153
.await
128154
.context("Failed to parse response JSON")?;
155+
129156
if let Some(data) = response_json
130157
.get("data")
131158
.and_then(|data| data.get("resetStreak"))
132159
{
133-
let current_streak = data.get("currentStreak").and_then(|v| v.as_i64()).unwrap();
134-
135-
let max_streak = data.get("maxStreak").and_then(|v| v.as_i64()).unwrap();
160+
let current_streak =
161+
data.get("currentStreak")
162+
.and_then(|v| v.as_i64())
163+
.ok_or_else(|| anyhow!("current_streak was parsed as None"))? as i32;
164+
let max_streak =
165+
data.get("maxStreak")
166+
.and_then(|v| v.as_i64())
167+
.ok_or_else(|| anyhow!("max_streak was parsed as None"))? as i32;
136168

137169
// Update the member's streak vector
138170
if member.streak.is_empty() {
139171
// If the streak vector is empty, initialize it with the returned values
140172
member.streak.push(Streak {
141-
current_streak: current_streak as i32,
142-
max_streak: max_streak as i32,
173+
current_streak,
174+
max_streak,
143175
});
144176
} else {
145177
// Otherwise, update the first streak entry
146178
for streak in &mut member.streak {
147-
streak.current_streak = current_streak as i32;
148-
streak.max_streak = max_streak as i32;
179+
streak.current_streak = current_streak;
180+
streak.max_streak = max_streak;
149181
}
150182
}
183+
} else {
184+
return Err(anyhow!("Failed to access data from {}", response_json));
151185
}
186+
152187
Ok(())
153188
}

0 commit comments

Comments
 (0)