-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
195 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::time::Duration; | ||
|
||
use clap::Parser; | ||
use color_eyre::Result; | ||
use crossterm::style::Stylize; | ||
use indicatif::ProgressBar; | ||
|
||
use crate::credential::handle_token; | ||
use crate::github::github_client::GitHub; | ||
use crate::hyperlink::Hyperlink; | ||
|
||
/// Merges changes from microsoft/winget-pkgs into the fork repository | ||
#[derive(Parser)] | ||
pub struct MergeUpstream { | ||
/// Merges changes even if the fork's default branch is not fast-forward. This is not | ||
/// recommended as you should instead have a clean default branch that has not diverged from the | ||
/// upstream default branch | ||
#[arg(short, long)] | ||
force: bool, | ||
|
||
/// GitHub personal access token with the `public_repo` scope | ||
#[arg(short, long, env = "GITHUB_TOKEN")] | ||
token: Option<String>, | ||
} | ||
|
||
impl MergeUpstream { | ||
pub async fn run(self) -> Result<()> { | ||
let token = handle_token(self.token).await?; | ||
let github = GitHub::new(&token)?; | ||
|
||
// Fetch repository data from both upstream and fork repositories asynchronously | ||
let winget_pkgs = github.get_winget_pkgs(None); | ||
let winget_pkgs_fork = github | ||
.get_winget_pkgs(Some(&github.get_username().await?)) | ||
.await?; | ||
let winget_pkgs = winget_pkgs.await?; | ||
|
||
// Create hyperlinks to the repository's URLs when their full name is printed | ||
let winget_pkgs_hyperlink = winget_pkgs.full_name.hyperlink(winget_pkgs.url).blue(); | ||
let winget_pkgs_fork_hyperlink = winget_pkgs_fork | ||
.full_name | ||
.hyperlink(winget_pkgs_fork.url) | ||
.blue(); | ||
|
||
// Check whether the fork is already up-to-date with upstream by their latest commit OID's | ||
if winget_pkgs.default_branch_oid == winget_pkgs_fork.default_branch_oid { | ||
println!( | ||
"{winget_pkgs_fork_hyperlink} is already {} with {winget_pkgs_hyperlink}", | ||
"up-to-date".green() | ||
); | ||
return Ok(()); | ||
} | ||
|
||
// Show an indeterminate progress bar while upstream changes are being merged | ||
let pb = ProgressBar::new_spinner().with_message(format!( | ||
"Merging upstream changes from {} into {}", | ||
winget_pkgs.full_name.as_str().blue(), | ||
winget_pkgs_fork.full_name.as_str().blue(), | ||
)); | ||
pb.enable_steady_tick(Duration::from_millis(50)); | ||
|
||
github | ||
.merge_upstream( | ||
&winget_pkgs_fork.default_branch_ref_id, | ||
winget_pkgs.default_branch_oid, | ||
self.force, | ||
) | ||
.await?; | ||
|
||
pb.finish_with_message(format!( | ||
"{} merged upstream changes from {winget_pkgs_hyperlink} into {winget_pkgs_fork_hyperlink}", | ||
"Successfully".green(), | ||
)); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::github::graphql::github_schema::github_schema as schema; | ||
use crate::github::graphql::types::GitObjectId; | ||
|
||
/* | ||
mutation MergeUpstream($branchRefId: ID!, $upstreamTargetOid: GitObjectID!) { | ||
updateRef(input: { | ||
refId: $branchRefId, | ||
oid: $upstreamTargetOid, | ||
}) { | ||
clientMutationId | ||
} | ||
} | ||
*/ | ||
|
||
#[derive(cynic::QueryVariables)] | ||
pub struct MergeUpstreamVariables<'a> { | ||
pub branch_ref_id: &'a cynic::Id, | ||
pub upstream_target_oid: GitObjectId, | ||
pub force: bool, | ||
} | ||
|
||
#[derive(cynic::QueryFragment)] | ||
#[cynic(graphql_type = "Mutation", variables = "MergeUpstreamVariables")] | ||
pub struct MergeUpstream { | ||
#[arguments(input: { oid: $upstream_target_oid, refId: $branch_ref_id })] | ||
pub update_ref: Option<UpdateRefPayload>, | ||
} | ||
|
||
#[derive(cynic::QueryFragment)] | ||
pub struct UpdateRefPayload { | ||
pub client_mutation_id: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::github::graphql::github_schema::github_schema as schema; | ||
|
||
#[derive(cynic::Scalar)] | ||
pub struct Base64String(pub String); | ||
|
||
#[derive(cynic::Scalar, PartialEq, Eq)] | ||
#[cynic(graphql_type = "GitObjectID")] | ||
pub struct GitObjectId(pub String); | ||
|
||
#[derive(cynic::Scalar)] | ||
#[cynic(graphql_type = "GitRefname")] | ||
pub struct GitRefName(pub String); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub trait Hyperlink<S: AsRef<str>, T: AsRef<str>> { | ||
fn hyperlink(&self, url: T) -> String; | ||
} | ||
|
||
impl<S: AsRef<str>, T: AsRef<str>> Hyperlink<S, T> for S { | ||
fn hyperlink(&self, url: T) -> String { | ||
format!( | ||
"\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\", | ||
url.as_ref(), | ||
self.as_ref() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters