Skip to content

Commit b35d26d

Browse files
committed
Add route /importEnglish
1 parent b724319 commit b35d26d

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

DEVELOPMENT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* `/triggerUpdate?secret=X` - Update all repositories
55
* `/triggerUpdate?secret=X&repo=REPO` - Update specific repository
66
* `/importRepository?secret=X&repo=REPO` - Readd repository to Crowdin (both english files and translations)
7+
* `/importEnglish?secret=X&repo=REPO` - Overwrites english files on Crowdin based on GitHub
78

89

910
## Heroku configuration

src/server/debug_routes.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use rocket::get;
22

3-
use crate::{github, webhooks};
4-
use crate::github::extract_mods_from_repository;
5-
use crate::github_mod_name::GithubModName;
6-
use crate::server::trigger_update::get_trigger_update_mutex;
3+
use crate::server::trigger_update::{get_installation_id_and_mods, get_trigger_update_mutex};
4+
use crate::webhooks;
75

86
/// For cases when repository was not imported correctly for some reason and manual intervention is needed
97
#[get("/importRepository?<repo>&<subpath>&<secret>")]
@@ -15,22 +13,30 @@ pub async fn import_repository(
1513
if secret != Some(dotenv::var("WEBSERVER_SECRET").unwrap()) {
1614
return "Missing secret";
1715
}
18-
1916
let _lock = get_trigger_update_mutex().await;
20-
let installation_id = match github::get_installation_id_for_repo(&repo).await {
21-
Some(id) => id,
22-
None => return "Can't find installation for repository",
17+
let (installation_id, mods) = match get_installation_id_and_mods(&repo, subpath).await {
18+
Ok(value) => value,
19+
Err(value) => return value,
2320
};
21+
webhooks::on_repository_added(&repo, mods, installation_id).await;
22+
"Ok."
23+
}
2424

25-
let mods = if subpath.is_some() {
26-
vec![GithubModName::new(&repo, subpath)]
27-
} else {
28-
let api = github::as_installation(installation_id);
29-
extract_mods_from_repository(&api, &repo).await
30-
};
31-
if mods.is_empty() {
32-
return "No mods.";
25+
/// Overwrites all english file on crowdin based on github
26+
#[get("/importEnglish?<repo>&<subpath>&<secret>")]
27+
pub async fn import_english(
28+
repo: String,
29+
subpath: Option<String>,
30+
secret: Option<String>,
31+
) -> &'static str {
32+
if secret != Some(dotenv::var("WEBSERVER_SECRET").unwrap()) {
33+
return "Missing secret";
3334
}
34-
webhooks::on_repository_added(&repo, mods, installation_id).await;
35+
let _lock = get_trigger_update_mutex().await;
36+
let (installation_id, mods) = match get_installation_id_and_mods(&repo, subpath).await {
37+
Ok(value) => value,
38+
Err(value) => return value,
39+
};
40+
webhooks::import_english(&repo, mods, installation_id).await;
3541
"Ok."
3642
}

src/server/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub async fn main() {
3535
trigger_update::trigger_update,
3636
version,
3737
debug_routes::import_repository,
38+
debug_routes::import_english,
3839
example_error_routes::error1,
3940
example_error_routes::error2,
4041
];

src/server/trigger_update.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tokio::time::sleep;
1111

1212
use crate::{crowdin, git_util, github, util};
1313
use crate::crowdin::{get_crowdin_directory_name, normalize_language_code, replace_ini_to_cfg};
14-
use crate::github::as_personal_account;
14+
use crate::github::{as_personal_account, extract_mods_from_repository};
1515
use crate::github_mod_name::GithubModName;
1616
use crate::mod_directory::ModDirectory;
1717

@@ -26,8 +26,7 @@ pub async fn trigger_update(
2626
}
2727
match repo {
2828
Some(repo) => {
29-
let github_name = GithubModName::new(&repo, subpath);
30-
trigger_update_single_repository(&repo, github_name).await
29+
trigger_update_single_repository(&repo, subpath).await
3130
}
3231
None => {
3332
let task = trigger_update_all_repositories();
@@ -46,13 +45,14 @@ pub async fn get_trigger_update_mutex() -> impl Drop {
4645
MUTEX.lock().await
4746
}
4847

49-
async fn trigger_update_single_repository(full_name: &str, github_name: GithubModName) -> &'static str {
48+
async fn trigger_update_single_repository(full_name: &str, subpath: Option<String>) -> &'static str {
5049
let _lock = get_trigger_update_mutex().await;
5150
info!("\n[update-github-from-crowdin] [{}] starting...", full_name);
52-
let Some(installation_id) = github::get_installation_id_for_repo(full_name).await else {
53-
return "Can't find github installation";
51+
let (installation_id, mods) = match get_installation_id_and_mods(&full_name, subpath).await {
52+
Ok(value) => value,
53+
Err(message) => return message,
5454
};
55-
let repositories = vec![(full_name.to_owned(), vec![github_name], installation_id)];
55+
let repositories = vec![(full_name.to_owned(), mods, installation_id)];
5656
let success = push_crowdin_changes_to_repositories(repositories).await;
5757
if !success {
5858
return "Can't find mod directory on crowdin";
@@ -61,6 +61,27 @@ async fn trigger_update_single_repository(full_name: &str, github_name: GithubMo
6161
"Ok"
6262
}
6363

64+
pub async fn get_installation_id_and_mods(
65+
repo: &str,
66+
subpath: Option<String>,
67+
) -> Result<(InstallationId, Vec<GithubModName>), &'static str> {
68+
let installation_id = match github::get_installation_id_for_repo(&repo).await {
69+
Some(id) => id,
70+
None => return Err("Can't find installation for repository"),
71+
};
72+
73+
let mods = if subpath.is_some() {
74+
vec![GithubModName::new(&repo, subpath)]
75+
} else {
76+
let api = github::as_installation(installation_id);
77+
extract_mods_from_repository(&api, &repo).await
78+
};
79+
if mods.is_empty() {
80+
return Err("No mods.");
81+
}
82+
Ok((installation_id, mods))
83+
}
84+
6485
async fn trigger_update_all_repositories() {
6586
let _lock = get_trigger_update_mutex().await;
6687
info!("\n[update-github-from-crowdin] [*] starting...");

src/webhooks.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,21 @@ pub async fn on_repository_added(full_name: &str, mods: Vec<GithubModName>, inst
9393
let (crowdin_directory, _) = CrowdinDirectory::get_or_create(mod_directory).await;
9494
crowdin_directory.add_english_and_localization_files().await;
9595
}
96-
// explicit drop to ensure that cloned directory is deleted only here
97-
drop(repository_directory);
9896
info!("[add-repository] [{}] success", full_name);
9997
}
10098

99+
pub async fn import_english(full_name: &str, mods: Vec<GithubModName>, installation_id: InstallationId) {
100+
let repository_directory = github::clone_repository(&full_name, installation_id).await;
101+
for mod_ in mods {
102+
let mod_directory = ModDirectory::new(&repository_directory, mod_);
103+
if !mod_directory.check_for_locale_folder() { continue; }
104+
105+
if !CrowdinDirectory::has_existing(&mod_directory).await { continue; }
106+
let (crowdin_directory, _) = CrowdinDirectory::get_or_create(mod_directory).await;
107+
crowdin_directory.add_english_files().await;
108+
}
109+
}
110+
101111
pub async fn on_push_event(
102112
event: &PushWebhookEventPayload,
103113
installation_id: InstallationId,

0 commit comments

Comments
 (0)