Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replace update option #1067

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const_format = { version = "0.2.34", features = ["derive"] }
crc32fast = "1.4.2"
crossterm = "0.28.1"
cynic = { version = "3.9.1", features = ["http-reqwest"] }
derive-new = "0.7.0"
derive_more = { version = "2.0.1", features = ["as_ref", "constructor", "debug", "deref", "deref_mut", "display", "from_str", "into_iterator"] }
encoding_rs = "0.8.35"
flate2 = "1.0.35"
Expand Down
24 changes: 23 additions & 1 deletion src/commands/update_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::num::{NonZeroU32, NonZeroU8};
use anstream::println;
use camino::Utf8PathBuf;
use clap::Parser;
use color_eyre::eyre::Result;
use color_eyre::eyre::{bail, Result};
use indicatif::ProgressBar;
use owo_colors::OwoColorize;
use reqwest::Client;
Expand Down Expand Up @@ -83,6 +83,10 @@ pub struct UpdateVersion {
#[arg(long, env = "DRY_RUN")]
dry_run: bool,

/// Package version to replace
#[arg(short, long, num_args = 0..=1, default_missing_value = "latest")]
replace: Option<PackageVersion>,

/// GitHub personal access token with the `public_repo` scope
#[arg(short, long, env = "GITHUB_TOKEN")]
token: Option<String>,
Expand All @@ -107,6 +111,23 @@ impl UpdateVersion {
self.package_identifier
);

let replace_version = self
.replace
.as_ref()
.map(|version| {
version
.is_latest()
.then_some(latest_version)
.unwrap_or(version)
})
.filter(|&version| version != &self.package_version);

if let Some(version) = replace_version {
if !versions.contains(version) {
bail!("Replacement version {version} does not exist in {WINGET_PKGS_FULL_NAME}")
}
}

if let Some(pull_request) = existing_pr.await? {
if !self.dry_run
&& !prompt_existing_pull_request(
Expand Down Expand Up @@ -250,6 +271,7 @@ impl UpdateVersion {
.version(&self.package_version)
.versions(&versions)
.changes(changes)
.maybe_replace_version(replace_version)
.maybe_issue_resolves(self.resolves)
.maybe_created_with(self.created_with)
.maybe_created_with_url(self.created_with_url)
Expand Down
36 changes: 25 additions & 11 deletions src/github/github_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::types::urls::publisher_url::PublisherUrl;
use crate::types::urls::release_notes_url::ReleaseNotesUrl;
use crate::types::urls::url::DecodedUrl;
use crate::update_state::UpdateState;
use base64ct::Encoding;
use base64ct::{Base64, Encoding};
use bon::bon;
use const_format::{formatcp, str_repeat};
use cynic::http::{CynicReqwestError, ReqwestExt};
Expand Down Expand Up @@ -734,17 +734,14 @@ impl GitHub {
)
.await?;
let commit_title = get_commit_title(identifier, version, &UpdateState::RemoveVersion);
let directory_content = self
let deletions = self
.get_directory_content(
fork_owner,
&branch_name,
&get_package_path(identifier, Some(version), None),
)
.await?
.collect::<Vec<_>>();
let deletions = directory_content
.iter()
.map(|path| FileDeletion { path })
.map(|path| FileDeletion::new(path))
.collect::<Vec<_>>();
let _commit_url = self
.create_commit()
Expand Down Expand Up @@ -785,6 +782,7 @@ impl GitHub {
version: &PackageVersion,
versions: Option<&BTreeSet<PackageVersion>>,
changes: Vec<(String, String)>,
replace_version: Option<&PackageVersion>,
issue_resolves: Option<Vec<NonZeroU32>>,
created_with: Option<String>,
created_with_url: Option<DecodedUrl>,
Expand All @@ -799,19 +797,35 @@ impl GitHub {
.await?;
let commit_title =
get_commit_title(identifier, version, &UpdateState::get(version, versions));
let changes = changes
let additions = changes
.iter()
.map(|(path, content)| FileAddition {
contents: Base64String::new(base64ct::Base64::encode_string(content.as_bytes())),
path,
.map(|(path, content)| {
FileAddition::new(
Base64String::new(Base64::encode_string(content.as_bytes())),
path,
)
})
.collect::<Vec<_>>();
let mut deletions = None;
if replace_version.is_some() {
deletions = Some(
self.get_directory_content(
&current_user,
&branch_name,
&get_package_path(identifier, replace_version, None),
)
.await?
.map(|path| FileDeletion::new(path))
.collect::<Vec<_>>(),
)
}
let _commit_url = self
.create_commit()
.branch_id(&pull_request_branch.id)
.head_sha(pull_request_branch.target.map(|target| target.oid).unwrap())
.message(&commit_title)
.additions(changes)
.additions(additions)
.maybe_deletions(deletions)
.send()
.await?;
self.create_pull_request(
Expand Down
16 changes: 10 additions & 6 deletions src/github/graphql/create_commit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::github::graphql::github_schema::github_schema as schema;
use crate::github::graphql::types::{Base64String, GitObjectId};
use derive_new::new;
use std::borrow::Cow;
use url::Url;

#[derive(cynic::QueryVariables)]
Expand Down Expand Up @@ -46,16 +48,18 @@ pub struct FileChanges<'a> {
}

/// <https://docs.github.com/graphql/reference/input-objects#filedeletion>
#[derive(cynic::InputObject)]
pub struct FileDeletion<'a> {
pub path: &'a str,
#[derive(cynic::InputObject, new)]
pub struct FileDeletion<'path> {
#[new(into)]
pub path: Cow<'path, str>,
}

/// <https://docs.github.com/graphql/reference/input-objects#fileaddition>
#[derive(cynic::InputObject)]
pub struct FileAddition<'a> {
#[derive(cynic::InputObject, new)]
pub struct FileAddition<'path> {
pub contents: Base64String,
pub path: &'a str,
#[new(into)]
pub path: Cow<'path, str>,
}

/// <https://docs.github.com/graphql/reference/input-objects#committablebranch>
Expand Down
6 changes: 6 additions & 0 deletions src/types/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ impl Version {
pub fn as_str(&self) -> &str {
&self.raw
}

pub fn is_latest(&self) -> bool {
const LATEST: &str = "latest";

self.raw.eq_ignore_ascii_case(LATEST)
}
}

impl PartialEq for Version {
Expand Down