Skip to content

Commit

Permalink
Merge pull request #213 from jerus-org/make-release
Browse files Browse the repository at this point in the history
feat: add make_release method to create a new release on GitHub
  • Loading branch information
jerusdp authored Jul 22, 2024
2 parents 2ab7f18 + ea402e4 commit 303ad8b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- add release function to move changelog unreleased section to new version(pr [#201])
- write release notes(pr [#202])
- add branch check to prevent actions on main or master branches(pr [#210])
- add make_release method to create a new release on GitHub(pr [#213])

### Changed

Expand Down Expand Up @@ -266,6 +267,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#210]: https://github.com/jerus-org/pcu/pull/210
[#211]: https://github.com/jerus-org/pcu/pull/211
[#212]: https://github.com/jerus-org/pcu/pull/212
[#213]: https://github.com/jerus-org/pcu/pull/213
[Unreleased]: https://github.com/jerus-org/pcu/compare/0.1.10...HEAD
[0.1.10]: https://github.com/jerus-org/pcu/compare/0.1.9...0.1.10
[0.1.9]: https://github.com/jerus-org/pcu/compare/0.1.8...0.1.9
Expand Down
54 changes: 45 additions & 9 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
env,
ffi::{OsStr, OsString},
ffi::OsString,
fs,
io::Write,
path::{self, Path},
Expand All @@ -19,6 +19,7 @@ use git2::{BranchType, Cred, Direction, RemoteCallbacks, Repository};
use keep_a_changelog::{
changelog::ChangelogBuilder, ChangeKind, Changelog, ChangelogParseOptions, Release, Version,
};
use octocrab::Octocrab;
use url::Url;

use crate::Error;
Expand All @@ -34,6 +35,7 @@ pub struct Client {
pull_request: Option<PullRequest>,
changelog: OsString,
changelog_update: Option<PrTitle>,
unreleased: Option<String>,
}

impl Client {
Expand Down Expand Up @@ -87,6 +89,7 @@ impl Client {
pull_request,
changelog,
changelog_update: None,
unreleased: None,
})
}

Expand Down Expand Up @@ -303,7 +306,7 @@ impl Client {
}

/// Update the unreleased section to the changelog to `version`
pub fn update_unreleased(&self, version: &str) -> Result<(), Error> {
pub fn update_unreleased(&mut self, version: &str) -> Result<(), Error> {
log::debug!(
"Updating changelog: {:?} with version {:?}",
self.changelog,
Expand All @@ -316,15 +319,15 @@ impl Client {

if !version.is_empty() {
#[allow(clippy::needless_question_mark)]
return Ok(self.release_unreleased(&self.changelog, version)?);
return Ok(self.release_unreleased(version)?);
}

Ok(())
}

pub fn release_unreleased(&self, log_file: &OsStr, version: &str) -> Result<(), Error> {
let Some(log_file) = log_file.to_str() else {
return Err(Error::InvalidPath(log_file.to_owned()));
pub fn release_unreleased(&mut self, version: &str) -> Result<(), Error> {
let Some(log_file) = self.changelog.to_str() else {
return Err(Error::InvalidPath(self.changelog.to_owned()));
};

let repo_url =
Expand Down Expand Up @@ -395,16 +398,49 @@ impl Client {
unreleased.set_date(today);
};

let string = unreleased.to_string();
log::trace!("Release notes:\n\n---\n{}\n---\n\n", string);
let _ = fs::write("release_notes.md", string);
let unreleased_string = unreleased.to_string();
log::trace!("Release notes:\n\n---\n{}\n---\n\n", unreleased_string);
let _ = fs::write("release_notes.md", unreleased_string.clone());

self.unreleased = Some(unreleased_string);

change_log
.save_to_file(log_file)
.map_err(|e| Error::KeepAChangelog(e.to_string()))?;

Ok(())
}

pub async fn make_release(&self, version: &str) -> Result<(), Error> {
let octocrab = Octocrab::default();

let latest_release = octocrab
.repos(self.owner(), self.repo())
.releases()
.get_latest()
.await?;
let release_id = latest_release.id;

let body = if let Some(unreleased) = &self.unreleased {
unreleased.clone()
} else {
String::from("Latest release")
};

let release = octocrab
.repos(self.owner(), self.repo())
.releases()
.update(release_id.into_inner() + 1)
.tag_name(format!("v{version}").as_str())
.name(format!("Version {version}").as_str())
.body(body.as_str())
.send()
.await?;

log::trace!("Release: {:#?}", release);

Ok(())
}
}

impl Client {
Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async fn run_pull_request(sign: Sign, args: PullRequest) -> Result<ClState> {
}

async fn run_release(sign: Sign, args: Release) -> Result<ClState> {
let client = get_client(Commands::Release(args.clone())).await?;
let mut client = get_client(Commands::Release(args.clone())).await?;

let version = args.release;

Expand All @@ -178,6 +178,33 @@ async fn run_release(sign: Sign, args: Release) -> Result<ClState> {

client.update_unreleased(&version)?;

log::debug!("Changelog file name: {}", client.changelog());

if log::log_enabled!(log::Level::Trace) {
print_changelog(client.changelog());
};

let report = client.repo_status()?;
log::debug!("Before commit:Repo state: {report}");
log::debug!("before commit:Branch status: {}", client.branch_status()?);

match sign {
Sign::Gpg => {
client.commit_changelog_gpg()?;
}
Sign::None => {
client.commit_changelog()?;
}
}

log::debug!("After commit: Repo state: {}", client.repo_status()?);
log::debug!("After commit: Branch status: {}", client.branch_status()?);

client.make_release(&version).await?;

client.push_changelog()?;
log::debug!("After push: Branch status: {}", client.branch_status()?);

Ok(ClState::Updated)
}

Expand Down

0 comments on commit 303ad8b

Please sign in to comment.