Skip to content

Commit 7a1efd9

Browse files
authored
Merge pull request #30 from jupyterkat/update
Fixes and updates
2 parents fc3e7c1 + 47c3eee commit 7a1efd9

File tree

20 files changed

+2147
-1418
lines changed

20 files changed

+2147
-1418
lines changed

Cargo.lock

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

crates/diffbot_lib/Cargo.toml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,19 @@ edition = "2021"
55
license = "MIT"
66

77
[dependencies]
8-
serde = { version = "1.0.188", features = ["derive"] }
9-
serde_json = "1.0.107"
10-
octocrab = "0.30.1"
11-
eyre = "0.6.8"
12-
derive_builder = "0.12.0"
13-
chrono = "0.4.30"
14-
reqwest = "0.11.20"
15-
futures-lite = "1.13.0"
16-
simple_logger = "4.2.0"
17-
sha2 = "0.10.7"
8+
serde = { version = "1.0.206", features = ["derive"] }
9+
octocrab = "0.39.0"
10+
eyre = "0.6.12"
11+
derive_builder = "0.20.0"
12+
chrono = "0.4.38"
13+
sha2 = "0.10.8"
1814
hmac = "0.12.1"
1915
hex = "0.4.3"
2016
tracing = "0.1"
2117
tracing-subscriber = { version = "0.3", features = ["fmt", "time"] }
22-
tracing-panic = "0.1.1"
23-
tracing-loki = "0.2.4"
18+
tracing-panic = "0.1.2"
19+
tracing-loki = "0.2.5"
2420
flume = "0.11.0"
21+
async-fs = "2.1.2"
2522

26-
actix-web = "4.4.0"
27-
28-
async-fs = "1.6.0"
29-
base64 = "0.21.4"
23+
actix-web = "4.9.0"

crates/diffbot_lib/src/github/github_api.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use crate::github::github_types::{
2-
CreateCheckRun, Output, RawCheckRun, Repository, UpdateCheckRunBuilder,
3-
};
4-
use eyre::{format_err, Context, Result};
5-
use octocrab::models::repos::Content;
1+
use crate::github::github_types::{CreateCheckRun, Output, RawCheckRun, UpdateCheckRunBuilder};
2+
use eyre::{Context, Result};
63
use octocrab::models::InstallationId;
74
use serde::{Deserialize, Serialize};
85
use std::{future::Future, pin::Pin};
9-
use base64::{Engine as _, engine::general_purpose};
106

117
pub struct GithubEvent(pub String, pub Option<Vec<u8>>);
128

@@ -205,50 +201,6 @@ impl CheckRun {
205201
}
206202
}
207203

208-
async fn find_content<S: AsRef<str>>(
209-
installation: &InstallationId,
210-
repo: &Repository,
211-
filename: S,
212-
commit: S,
213-
) -> Result<Content> {
214-
let (owner, repo) = repo.name_tuple();
215-
let items = octocrab::instance()
216-
.installation(*installation)
217-
.repos(owner, repo)
218-
.get_content()
219-
.path(filename.as_ref())
220-
.r#ref(commit.as_ref())
221-
.send()
222-
.await?
223-
.take_items();
224-
225-
if items.len() > 1 {
226-
return Err(format_err!("Directory given to find_content"));
227-
}
228-
229-
items
230-
.into_iter()
231-
.next()
232-
.ok_or_else(|| format_err!("No content was found"))
233-
}
234-
235-
pub async fn download_url<S: AsRef<str>>(
236-
installation: &InstallationId,
237-
repo: &Repository,
238-
filename: S,
239-
commit: S,
240-
) -> Result<Vec<u8>> {
241-
let target = find_content(installation, repo, filename, commit).await?;
242-
243-
let content = target.content
244-
.ok_or_else(|| format_err!("File had no content!"))?
245-
.replace("\n", "");
246-
247-
general_purpose::STANDARD
248-
.decode(content)
249-
.map_err(|decode_error| format_err!("DecodeError: {}", decode_error))
250-
}
251-
252204
/* local test requires commenting out the .installation(...) call in find_content(), a valid github token with access, and the following dep: actix-rt = "2.9.0"
253205
254206
#[actix_web::rt::test]

crates/icondiffbot2/Cargo.toml

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,32 @@ license = "MIT"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
serde = { version = "1.0.188", features = ["derive"] }
10-
serde_json = "1.0.107"
11-
octocrab = "0.30.1"
9+
serde = { version = "1.0.206", features = ["derive"] }
10+
serde_json = "1.0.124"
11+
octocrab = "0.39.0"
1212
dmm-tools = { git = "https://github.com/jupyterkat/SpacemanDMM/" }
13-
dreammaker = { git = "https://github.com/jupyterkat/SpacemanDMM/" }
14-
once_cell = "1.18.0"
15-
jsonwebtoken = "8.3.0"
16-
reqwest = "0.11.20"
13+
jsonwebtoken = "9.3.0"
14+
reqwest = "0.12.5"
1715
diffbot_lib = { path = "../diffbot_lib" }
18-
eyre = "0.6.8"
16+
eyre = "0.6.12"
1917
simple-eyre = "0.3.1"
20-
ndarray = "0.15.6"
21-
rayon = "1.7.0"
22-
toml = "0.8.0"
23-
ahash = "0.8.3"
24-
hashbrown = { version = "0.14.0", features = ["rayon"] }
25-
hex = { version = "0.4.3", features = ["serde"] }
26-
tracing-loki = "0.2.4"
18+
rayon = "1.10.0"
19+
toml = "0.8.19"
20+
ahash = "0.8.11"
21+
hashbrown = { version = "0.14.5", features = ["rayon"] }
22+
tracing-loki = "0.2.5"
2723
flume = "0.11.0"
24+
mysql_async = "0.34.1"
25+
time = "0.3.36"
26+
secrecy = "0.8.0"
27+
percent-encoding = "2.3.1"
2828

29-
actix-web = "4.4.0"
30-
actix-files = "0.6.2"
31-
32-
mysql_async = "0.32.2"
33-
time = "0.3.28"
29+
actix-web = "4.9.0"
30+
actix-files = "0.6.6"
3431

3532
[target.'cfg(not(target_env = "msvc"))'.dependencies]
36-
tikv-jemallocator = "0.5.4"
33+
tikv-jemallocator = "0.6.0"
3734

3835
[dependencies.tracing]
39-
version = "0.1.37"
36+
version = "0.1.40"
4037
features = ["attributes"]

crates/icondiffbot2/config.example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ blacklist_contact = "Good luck!"
55
# Logging level (Optional, defaults to info), Valid values are "info", "warn", "trace", "error", "debug"
66
logging = "info"
77

8+
# Summary message, the message that goes before the diff, (Optional, defaults to below)
9+
# summary_msg = "*Please file any issues [here](https://github.com/spacestation13/BYONDDiffBots/issues).*\n\nIcons with diff:"
10+
811
# Mysql db url, the bots will write down a table that contains necessary information,
912
# for automated deletion (Optional)
1013
# db_url = ""
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use diffbot_lib::github::github_types::Repository;
2+
use eyre::Result;
3+
use octocrab::models::repos::Content;
4+
use octocrab::models::InstallationId;
5+
use secrecy::ExposeSecret;
6+
7+
//https://url.spec.whatwg.org/#c0-control-percent-encode-set
8+
const PATH_ENCODING: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
9+
//query
10+
.add(b' ')
11+
.add(b'"')
12+
.add(b'<')
13+
.add(b'>')
14+
.add(b'`')
15+
//path
16+
.add(b'?')
17+
.add(b'}')
18+
.add(b'{');
19+
20+
async fn find_content<S: AsRef<str>>(
21+
installation: &InstallationId,
22+
repo: &Repository,
23+
filename: S,
24+
commit: S,
25+
) -> Result<Content> {
26+
let (owner, repo) = repo.name_tuple();
27+
let items = octocrab::instance()
28+
.installation(*installation)
29+
.repos(owner, repo)
30+
.get_content()
31+
.path(
32+
percent_encoding::percent_encode(filename.as_ref().as_bytes(), PATH_ENCODING)
33+
.to_string(),
34+
)
35+
.r#ref(commit.as_ref())
36+
.send()
37+
.await?
38+
.take_items();
39+
40+
if items.len() > 1 {
41+
return Err(eyre::eyre!("Directory given to find_content"));
42+
}
43+
44+
items
45+
.into_iter()
46+
.next()
47+
.ok_or_else(|| eyre::eyre!("No content was found"))
48+
}
49+
50+
pub async fn download_url<S: AsRef<str>>(
51+
installation: &InstallationId,
52+
repo: &Repository,
53+
filename: S,
54+
commit: S,
55+
client: reqwest::Client,
56+
) -> Result<Vec<u8>> {
57+
let target = find_content(installation, repo, filename, commit).await?;
58+
59+
let download_url = target
60+
.download_url
61+
.as_ref()
62+
.ok_or_else(|| eyre::eyre!("No download URL given by GitHub"))?;
63+
let (_, token) = octocrab::instance()
64+
.installation_and_token(*installation)
65+
.await?;
66+
let response = client
67+
.get(download_url)
68+
.bearer_auth(token.expose_secret())
69+
.send()
70+
.await?;
71+
Ok(response.bytes().await?.to_vec())
72+
}

crates/icondiffbot2/src/job_processor.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{
1717
};
1818

1919
#[tracing::instrument]
20-
pub fn do_job(job: Job) -> Result<CheckOutputs> {
20+
pub fn do_job(job: Job, client: reqwest::Client) -> Result<CheckOutputs> {
2121
let handle = actix_web::rt::Runtime::new()?;
2222

2323
handle.block_on(async { job.check_run.mark_started().await })?;
@@ -28,16 +28,20 @@ pub fn do_job(job: Job) -> Result<CheckOutputs> {
2828
.iter()
2929
.map(|dmi| {
3030
(
31-
sha_to_iconfile(&job, &dmi.filename, status_to_sha(&job, &dmi.status)),
31+
sha_to_iconfile(
32+
&job,
33+
&dmi.filename,
34+
status_to_sha(&job, &dmi.status),
35+
client.clone(),
36+
),
3237
dmi,
3338
)
3439
})
35-
.map(|(file, dmi)| -> Result<()> {
40+
.try_for_each(|(file, dmi)| -> Result<()> {
3641
let states = render(&job, file?)?;
3742
map.insert(dmi.filename.as_str(), states);
3843
Ok(())
39-
})
40-
.collect::<Result<()>>()?;
44+
})?;
4145

4246
map.build()
4347
}
@@ -70,7 +74,7 @@ fn render(
7074
env!("CARGO_MANIFEST_DIR"),
7175
"/templates/diff_line_error.txt"
7276
)),
73-
error = format_args!("Before icon render failed:\n```{e:?}```"),
77+
error = format_args!("Before icon render failed:\n{e:?}"),
7478
)],
7579
)),
7680

@@ -122,26 +126,24 @@ fn render(
122126
.metadata
123127
.states
124128
.iter()
125-
.map(|(name, vec)| {
129+
.flat_map(|(name, vec)| {
126130
vec.iter()
127131
.enumerate()
128132
.map(|(duplication_index, _)| (duplication_index, name.as_str()))
129133
.collect::<Vec<_>>()
130134
})
131-
.flatten()
132135
.collect();
133136
let after_states: HashSet<(usize, &str), ahash::RandomState> = after
134137
.icon
135138
.metadata
136139
.states
137140
.iter()
138-
.map(|(name, vec)| {
141+
.flat_map(|(name, vec)| {
139142
vec.iter()
140143
.enumerate()
141144
.map(|(duplication_index, _)| (duplication_index, name.as_str()))
142145
.collect::<Vec<_>>()
143146
})
144-
.flatten()
145147
.collect();
146148

147149
let prefix = format!("{}/{}", job.installation, job.pull_request);

crates/icondiffbot2/src/main.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod downloading;
12
mod github_processor;
23
mod job_processor;
34
mod runner;
@@ -10,8 +11,8 @@ use diffbot_lib::{
1011
};
1112
use mysql_async::prelude::Queryable;
1213
use octocrab::OctocrabBuilder;
13-
use once_cell::sync::OnceCell;
1414
use serde::Deserialize;
15+
use std::sync::OnceLock;
1516
use std::{
1617
fs::File,
1718
io::Read,
@@ -64,6 +65,8 @@ pub struct Config {
6465
pub blacklist_contact: String,
6566
#[serde(default = "default_log_level")]
6667
pub logging: String,
68+
#[serde(default = "default_msg")]
69+
pub summary_msg: String,
6770
pub secret: Option<String>,
6871
pub db_url: Option<String>,
6972
pub grafana_loki: Option<GrafanaLoki>,
@@ -73,7 +76,11 @@ fn default_log_level() -> String {
7376
"info".to_string()
7477
}
7578

76-
static CONFIG: OnceCell<Config> = OnceCell::new();
79+
fn default_msg() -> String {
80+
"*Please file any issues [here](https://github.com/spacestation13/BYONDDiffBots/issues).*\n\nIcons with diff:".to_string()
81+
}
82+
83+
static CONFIG: OnceLock<Config> = OnceLock::new();
7784
// static FLAME_LAYER_GUARD: OnceCell<tracing_flame::FlushGuard<std::io::BufWriter<File>>> =
7885
// OnceCell::new();
7986

@@ -87,6 +94,10 @@ fn init_config(path: &Path) -> eyre::Result<&'static Config> {
8794
Ok(CONFIG.get().unwrap())
8895
}
8996

97+
fn read_config() -> &'static Config {
98+
CONFIG.get().unwrap()
99+
}
100+
90101
// fn init_global_subscriber() {
91102
// use tracing_subscriber::prelude::*;
92103

@@ -109,7 +120,7 @@ fn read_key(path: &Path) -> Vec<u8> {
109120
File::open(path).unwrap_or_else(|_| panic!("Unable to find file {}", path.display()));
110121

111122
let mut key = Vec::new();
112-
let _ = key_file
123+
_ = key_file
113124
.read_to_end(&mut key)
114125
.unwrap_or_else(|_| panic!("Failed to read key {}", path.display()));
115126

@@ -151,6 +162,7 @@ async fn main() -> eyre::Result<()> {
151162
.build()
152163
.expect("fucked up octocrab"),
153164
);
165+
let reqwest_client = reqwest::Client::new();
154166

155167
async_fs::create_dir_all("./images").await.unwrap();
156168

@@ -178,7 +190,11 @@ async fn main() -> eyre::Result<()> {
178190
.await?;
179191
}
180192

181-
actix_web::rt::spawn(runner::handle_jobs("IconDiffBot2", job_receiver));
193+
actix_web::rt::spawn(runner::handle_jobs(
194+
"IconDiffBot2",
195+
job_receiver,
196+
reqwest_client,
197+
));
182198

183199
let job_sender: DataJobSender = actix_web::web::Data::new(job_sender);
184200

0 commit comments

Comments
 (0)