Skip to content

Commit

Permalink
fix: successful release log when token is invalid
Browse files Browse the repository at this point in the history
We also undo the commit and the tag created in case of issues.
  • Loading branch information
mstrk committed Jul 27, 2023
1 parent 152b180 commit 3e9f02c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Result, bail};
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
use serde::Serialize;
use semver::Version;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl GithubApi {
};

let client = reqwest::Client::new();
let _ = client
let response = client
.post(format!("{}/releases", &self.api_url))
.header(CONTENT_TYPE, &self.content_type)
.header(USER_AGENT, &self.user_agent)
Expand All @@ -43,6 +43,13 @@ impl GithubApi {
.send()
.await?;

if !response.status().is_success() {
// get error message from response
let error_message = response.text().await?;
println!("error: {}", error_message);
bail!(error_message);
}

Ok(())
}
}
Expand Down
63 changes: 54 additions & 9 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ impl Git {
Ok(())
}

pub fn push_with_tags(&self) -> Result<()> {
Command::new("git")
.args(["push", "--follow-tags", &format!("--repo={}", &self.repo_url.as_str())])
.output()
.expect("[push_with_tags] failed to fetch");

Ok(())
}

pub fn commit(&self, message: &str) -> Result<()> {
Command::new("git")
.args(["add", "--all",])
Expand All @@ -182,6 +173,60 @@ impl Git {

Ok(())
}

// push commit
pub fn push(&self) -> Result<()> {
let output = Command::new("git")
.args(["push", &format!("--repo={}", &self.repo_url.as_str())])
.output()?;

let output = String::from_utf8_lossy(&output.stdout).trim().to_string();
println!("output: {}", output);

// check if push was successful
if !output.contains("Everything up-to-date") {
self.undo_commit()?;
bail!("failed to push changes token may be invalid");
}

Ok(())
}

// push tag
pub fn push_tag(&self, tag: &str) -> Result<()> {
let output = Command::new("git")
.args(["push", &format!("--repo={}", &self.repo_url.as_str()), "origin", tag])
.output()?;

let output = String::from_utf8_lossy(&output.stdout).trim().to_string();
println!("output: {}", output);

// check if push was successful
if !output.contains("[new tag]") {
self.undo_tag(tag)?;
bail!("failed to push tag");
}

Ok(())
}

// undo last tag
pub fn undo_tag(&self, tag: &str) -> Result<()> {
Command::new("git")
.args(["tag", "-d", tag])
.output()?;

Ok(())
}

// undo last commit and changes
pub fn undo_commit(&self) -> Result<()> {
Command::new("git")
.args(["reset", "--hard", "HEAD^"])
.output()?;

Ok(())
}
}

#[derive(Debug)]
Expand Down
17 changes: 7 additions & 10 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,21 @@ impl Pkg {

// Release commit
git
.commit(release_message.replace("%s", &self.changelog.next_release_version).as_str())
.context("failed to commit release")?;
.commit(release_message.replace("%s", &self.changelog.next_release_version).as_str())?;

// Push to remote
git.push()?;

// Release tag
git.tag(&self.changelog.next_release_version)
.context("failed to tag release")?;
git.tag(&self.changelog.next_release_version)?;
git.push_tag(&self.changelog.next_release_version)?;

// Push to remote
git.push_with_tags()
.context("failed to push release tag")?;

// Create release on GitHub
api.publish_release(
&self.changelog.next_release_version,
&self.tag_prefix,
&self.changelog.notes)
.await
.context("failed to publish release")?;
.await?;

Ok(())
}
Expand Down

0 comments on commit 3e9f02c

Please sign in to comment.