Skip to content

Commit

Permalink
Fix factorio-mods-helper contributions counting
Browse files Browse the repository at this point in the history
  • Loading branch information
dima74 committed Jun 22, 2024
1 parent 72460ec commit 3d938e2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
14 changes: 14 additions & 0 deletions examples/github_fork_all_not_forked_repositories.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use fml::github;

#[tokio::main]
async fn main() {
fml::init();

let not_forked = github::get_not_forked_repositories().await.not_forked;

let api_personal = github::as_personal_account();
for full_name in not_forked {
println!("Forking {}", full_name);
github::fork_repository_without_check(&api_personal, &full_name).await;
}
}
43 changes: 37 additions & 6 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,29 @@ pub fn as_personal_account() -> Octocrab {
.unwrap()
}

pub async fn fork_repository(personal_api: &Octocrab, owner: &str, repo: &str) -> bool {
if let Some(result) = check_fork_exists(personal_api, owner, repo).await {
return result;
pub async fn fork_repository(personal_api: &Octocrab, full_name: &str) -> bool {
if let Some(is_fork_name_correct) = check_fork_exists(personal_api, full_name).await {
return is_fork_name_correct;
}
fork_repository_without_check(personal_api, full_name).await;
true
}

info!("[update-github-from-crowdin] [{}/{}] forking repository...", owner, repo);
pub async fn fork_repository_without_check(personal_api: &Octocrab, full_name: &str) {
let (owner, repo) = full_name.split_once('/').unwrap();
info!("[update-github-from-crowdin] [{}] forking repository...", full_name);
personal_api
.repos(owner, repo)
.create_fork()
.send().await.unwrap();
sleep(Duration::from_secs(120)).await;
true
}

async fn check_fork_exists(api: &Octocrab, owner: &str, repo: &str) -> Option<bool> {
// None => no fork
// Some(false) => fork with different name
// Some(true) => fork exists and can be used
async fn check_fork_exists(api: &Octocrab, full_name: &str) -> Option<bool> {
let (owner, repo) = full_name.split_once('/').unwrap();
let forks = api
.repos(owner, repo)
.list_forks()
Expand All @@ -272,6 +280,29 @@ async fn check_fork_exists(api: &Octocrab, owner: &str, repo: &str) -> Option<bo
None
}

#[derive(Default)]
pub struct GetNotForkedResult {
pub not_forked: Vec<String>,
pub forked_with_diferrent_name: Vec<String>,
}

pub async fn get_not_forked_repositories() -> GetNotForkedResult {
let api_app = as_app();
let repositories = get_all_repositories(&api_app).await;

let api_personal = as_personal_account();
let mut result = GetNotForkedResult::default();
for (repo_info, _id) in repositories {
let full_name = repo_info.full_name;
match check_fork_exists(&api_personal, &full_name).await {
None => result.not_forked.push(full_name),
Some(false) => result.forked_with_diferrent_name.push(full_name),
Some(true) => continue,
}
}
result
}

pub async fn star_repository(api: &Octocrab, full_name: &str) {
let _response: octocrab::Result<EmptyBody> = api
.put(format!("/user/starred/{}", full_name), None::<&()>)
Expand Down
4 changes: 2 additions & 2 deletions src/server/trigger_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ async fn push_crowdin_changes_to_repository(

async fn push_changes_using_pull_request(path: &Path, full_name: &str, base_branch: &str) {
let personal_api = as_personal_account();
let (owner, repo) = full_name.split_once('/').unwrap();
if !github::fork_repository(&personal_api, owner, repo).await {
if !github::fork_repository(&personal_api, full_name).await {
return;
}
let (_owner, repo) = full_name.split_once('/').unwrap();
let pushed = git_util::push_to_my_fork(path, repo);
if pushed {
sleep(Duration::from_secs(30)).await;
Expand Down
17 changes: 12 additions & 5 deletions src/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ async fn on_repositories_added(repositories: Vec<InstallationEventRepository>, i
continue;
};
on_repository_added(repo_info, installation_id).await;

let api_personal = github::as_personal_account();
github::star_repository(&api_personal, &repository).await;
star_and_fork_repository(&repository).await;
}
}

Expand Down Expand Up @@ -137,8 +135,7 @@ pub async fn on_push_event(
info!("[push-webhook] [{}] success", full_name);

if created {
let api_personal = github::as_personal_account();
github::star_repository(&api_personal, &full_name).await;
star_and_fork_repository(&full_name).await;
}
}

Expand Down Expand Up @@ -174,3 +171,13 @@ fn get_all_changed_files(event: &PushWebhookEventPayload) -> impl Iterator<Item=
added.chain(modified).chain(removed).map(Deref::deref)
})
}

// This is needed for correct counting of contributions,
// so they will be displayed at https://github.com/factorio-mods-helper.
// Previously it was enough to star repository, but it was changed somewhere in 2023-2024.
// https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/managing-contribution-settings-on-your-profile/why-are-my-contributions-not-showing-up-on-my-profile
async fn star_and_fork_repository(repository: &str) {
let api_personal = github::as_personal_account();
github::star_repository(&api_personal, repository).await;
github::fork_repository(&api_personal, repository).await;
}
23 changes: 23 additions & 0 deletions tests/all_github_repositories_are_forked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use fml::github;

#[tokio::test]
async fn main() {
fml::init();
let result = github::get_not_forked_repositories().await;

let forked_with_diferrent_name = result.forked_with_diferrent_name;
if !forked_with_diferrent_name.is_empty() {
for full_name in &forked_with_diferrent_name {
println!("{}", full_name);
}
panic!("{} repositories have forks with different name", forked_with_diferrent_name.len());
}

let not_forked = result.not_forked;
if !not_forked.is_empty() {
for full_name in &not_forked {
println!("{}", full_name);
}
panic!("{} repositories not forked", not_forked.len());
}
}

0 comments on commit 3d938e2

Please sign in to comment.