Skip to content

Commit

Permalink
Add Automatic Artifact Downloading
Browse files Browse the repository at this point in the history
Adds automatic artifact downloading, and also fixes some smoll issues
  • Loading branch information
MrSn0wy committed Jul 19, 2024
1 parent f438ee6 commit 32f9d42
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ jobs:
git fetch origin gh-pages
git switch gh-pages --force
mkdir -p /home/runner/work/obhqWebsite/obhqWebsite/compatibility/_images/
mkdir -p /home/runner/work/obhqWebsite/obhqWebsite/download/artifacts/
rm -r -f /home/runner/work/obhqWebsite/obhqWebsite/compatibility/_images/games/
rm -r -f /home/runner/work/obhqWebsite/obhqWebsite/compatibility/_images/hb/
Expand All @@ -97,10 +98,12 @@ jobs:
mv -f /home/runner/work/obhqWebsite/temp/updater/images/* /home/runner/work/obhqWebsite/obhqWebsite/compatibility/_images/
mv -f /home/runner/work/obhqWebsite/temp/updater/database.json /home/runner/work/obhqWebsite/obhqWebsite/compatibility/database.json
mv -f /home/runner/work/obhqWebsite/temp/updater/stats.json /home/runner/work/obhqWebsite/obhqWebsite/stats.json
mv -f /home/runner/work/obhqWebsite/temp/updater/artifacts/* /home/runner/work/obhqWebsite/obhqWebsite/download/artifacts/
git add /home/runner/work/obhqWebsite/obhqWebsite/stats.json -f
git add /home/runner/work/obhqWebsite/obhqWebsite/compatibility/database.json -f
git add /home/runner/work/obhqWebsite/obhqWebsite/compatibility/_images/ -f
git add /home/runner/work/obhqWebsite/obhqWebsite/download/artifacts/ -f
files_to_commit=$(git status --porcelain | grep -E 'M|A|D' | wc -l)
if [ $files_to_commit -gt 0 ]; then
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/public_html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
# restore files that dont need updating
git restore --staged --worktree wiki/
git restore --staged --worktree download/artifacts/
git restore --staged --worktree compatibility/_images/games/
git restore --staged --worktree compatibility/_images/hb/
git restore --staged --worktree compatibility/database.json
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [v0.3.1] (unreleased)
### [v1.0.0] (unreleased)

#### Added
- Added artifact downloading to the rust updater script


#### Changed
- Fixed the amount of skeletonCards in the comp page from 20 to 10.
- Modified workflow files for the new Downloads page

### [v0.3.1]
- Put the tmdb hex into the config, instead of a secret variable
- Added CSP to the `_errorPages` and some other small fixes

Expand Down
2 changes: 1 addition & 1 deletion public_html/_errorPages/errapp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
document.addEventListener('DOMContentLoaded', async function () {
// required.js
adjustScreenSize(700)

await init();
adjustScreenSize(700);

Expand Down
10 changes: 0 additions & 10 deletions public_html/compatibility/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,6 @@
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCard skeletonLoading"></div>
<div class="gameCardE skeletonLoading"></div>
</div>

Expand Down
1 change: 1 addition & 0 deletions updater/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ homebrew_database = "./HBstore.db"
game_skips_database = "./game_skips.json"
database = "/home/runner/work/obhqWebsite/temp/updater/database.json"
stats_file = "/home/runner/work/obhqWebsite/temp/updater/stats.json"
artifacts_folder = "/home/runner/work/obhqWebsite/temp/updater/artifacts/"

tag_homebrew = 6164722453
tag_playable = 6164497050
Expand Down
7 changes: 7 additions & 0 deletions updater/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) struct Config {
pub game_skips_database: String,
pub database: String,
pub stats_file: String,
pub artifacts_folder: String,

pub tag_homebrew: u64,
pub tag_playable: u64,
Expand Down Expand Up @@ -54,6 +55,7 @@ pub(crate) fn config_creator() -> anyhow::Result<(Config, Secrets)> {
game_skips_database: "./game_skips.json".to_string(),
database: "./database.json".to_string(),
stats_file: "./stats.json".to_string(),
artifacts_folder: "./artifacts/".to_string(),

tag_homebrew: 6164722453,
tag_playable: 6164497050,
Expand All @@ -77,6 +79,7 @@ pub(crate) fn config_creator() -> anyhow::Result<(Config, Secrets)> {
// create needed folders
let game_images_folder = Path::new(&config_data.game_images_folder);
let homebrew_images_folder = Path::new(&config_data.homebrew_images_folder);
let artifacts_folder =Path::new(&config_data.artifacts_folder);

let database_folder = Path::new(&config_data.database).parent().unwrap_or_else(|| {
panic_red!("Error while getting the parent folder for: \"{}\"", &config_data.database);
Expand All @@ -103,6 +106,10 @@ pub(crate) fn config_creator() -> anyhow::Result<(Config, Secrets)> {
fs::create_dir_all(homebrew_images_folder)?;
}

if !artifacts_folder.exists() {
fs::create_dir_all(artifacts_folder)?;
}

if !database_folder.exists() {
fs::create_dir_all(database_folder)?;
}
Expand Down
5 changes: 5 additions & 0 deletions updater/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ fn main() {
Err(err) => eprintln_red!("Error creating the \"{}\" file! {}", &config.stats_file, err),
}

// get the artifacts
match utils::github_artifact_downloader(&config.artifacts_folder, &secrets.github_api_token, &config.workflow_url) {
Ok(_) => (),
Err(err) => eprintln_red!("Error Downloading the github artifacts! {}", err),
}

// Calculate the elapsed time
println_green!(
Expand Down
50 changes: 48 additions & 2 deletions updater/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fs;
use std::fs::File;

use rusqlite::Connection;
use serde::Serialize;
use serde_json::Value;

use crate::{eprintln_red, println_cyan, println_green};
use crate::{eprintln_red, github_request, panic_red, println_cyan, println_green};
use crate::config::{Config, Secrets};

pub(crate) enum ImageTypes<'lt> {
Expand Down Expand Up @@ -104,7 +105,6 @@ struct StatsJson {
}

pub(crate) fn stats_creator(path: &str, token: &str, github_main: &str, github_comp: &str, workflow: &str) -> Result<(), anyhow::Error> {
use crate::github_request;

let stars = github_request(github_main, token)
.get("stargazers_count")
Expand Down Expand Up @@ -137,6 +137,52 @@ pub(crate) fn stats_creator(path: &str, token: &str, github_main: &str, github_c
Ok(())
}

pub(crate) fn github_artifact_downloader(path: &str, token: &str, workflow: &str) -> Result<(), anyhow::Error> {

let artifacts_url = github_request(workflow, token)
.get("workflow_runs")
.unwrap()
.get(0)
.unwrap()
.get("artifacts_url")
.and_then(Value::as_str)
.expect("Failed to parse JSON o.o!")
.to_owned();

let artifacts = github_request(&artifacts_url, token)
.get("artifacts")
.and_then(Value::as_array)
.expect("Failed to parse JSON o.o!")
.to_owned();

for artifact in artifacts {
let os = artifact.get("name").and_then(Value::as_str).unwrap_or("");

if !os.is_empty() {
let download_url = artifact.get("archive_download_url").and_then(Value::as_str).unwrap_or_default();

match ureq::get(download_url)
.set("User-Agent", "obliteration.net")
.set("Accept", "*/*")
.set("Authorization", &format!("Bearer {}", token))
.call()
{
Ok(response) => {
let mut reader = response.into_reader();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer)?;

fs::write(format!("{path}{os}.zip"), buffer)?;
},
Err(response) => panic_red!("Github artifact request failed: {}", response),
}
}
}

Ok(())
}


pub(crate) fn homebrew_database_updater(config: &Config, secrets: &Secrets) -> Result<(), anyhow::Error> {
use md5::{Digest, Md5};
use chrono::{Timelike, Utc};
Expand Down

0 comments on commit 32f9d42

Please sign in to comment.