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

Expand release automation #1078

Merged
merged 8 commits into from
Sep 13, 2022
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
1 change: 1 addition & 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 tools/automator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chrono = "0.4.22"
map-macro = "0.2.4"
octocrab = "0.17.0"
semver = "1.0.13"
serde_json = "1.0.85"
url = "2.3.0"

[dependencies.autolib]
Expand Down
27 changes: 22 additions & 5 deletions tools/automator/src/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ use std::{collections::HashSet, fmt::Write, path::PathBuf};
use anyhow::Context;
use chrono::{Datelike, Utc};
use map_macro::set;
use octocrab::Octocrab;
use tokio::{
fs::{self, File},
io::AsyncWriteExt,
};

use crate::pull_requests::{Author, PullRequest, PullRequestsSinceLastRelease};

pub async fn create_release_announcement() -> anyhow::Result<()> {
pub async fn create_release_announcement(
octocrab: &Octocrab,
) -> anyhow::Result<()> {
let now = Utc::now();

let year = now.year();
let week = now.iso_week().week();
let date = format!("{year}-{:02}-{:02}", now.month(), now.day());

let pull_requests_since_last_release =
PullRequestsSinceLastRelease::fetch().await?;
PullRequestsSinceLastRelease::fetch(octocrab).await?;

let pull_requests =
pull_requests_since_last_release.pull_requests.into_values();
Expand All @@ -30,8 +34,14 @@ pub async fn create_release_announcement() -> anyhow::Result<()> {
version.minor += 1;

let mut file = create_file(year, week).await?;
generate_announcement(week, version.to_string(), pull_requests, &mut file)
.await?;
generate_announcement(
week,
date,
version.to_string(),
pull_requests,
&mut file,
)
.await?;

Ok(())
}
Expand All @@ -53,6 +63,7 @@ async fn create_file(year: i32, week: u32) -> anyhow::Result<File> {

async fn generate_announcement(
week: u32,
date: String,
version: String,
pull_requests: impl IntoIterator<Item = PullRequest>,
file: &mut File,
Expand Down Expand Up @@ -107,6 +118,8 @@ async fn generate_announcement(
"\
+++
title = \"Weekly Release - 2022-W{week}\"
# TASK: Uncomment this date, once the announcement is ready to be published.
# date = {date}

[extra]
version = \"{version}\"
Expand All @@ -119,7 +132,11 @@ version = \"{version}\"

Fornjot is supported by [@webtrax-oz](https://github.com/webtrax-oz), [@lthiery](https://github.com/lthiery), [@Yatekii](https://github.com/Yatekii), [@martindederer](https://github.com/martindederer), [@hobofan](https://github.com/hobofan), [@ahdinosaur](https://github.com/ahdinosaur), [@thawkins](https://github.com/thawkins), [@bollian](https://github.com/bollian), [@rozgo](https://github.com/rozgo), [@reivilibre](https://github.com/reivilibre), and [my other awesome sponsors](https://github.com/sponsors/hannobraun). Thank you!

If you want Fornjot to be stable and sustainable long-term, please consider [supporting me](https://github.com/sponsors/hannobraun) too.
<strong class=\"call-to-action\">
<p>
If you want Fornjot to be sustainable long-term, please consider <a href=\"https://github.com/sponsors/hannobraun\">supporting me</a> too.
</p>
</strong>


### End-user improvements
Expand Down
1 change: 1 addition & 0 deletions tools/automator/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[derive(clap::Parser)]
pub enum Args {
CreateReleaseAnnouncement(CreateReleaseAnnouncement),
Sponsors,
}

impl Args {
Expand Down
10 changes: 8 additions & 2 deletions tools/automator/src/pull_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use autolib::find_version_in_str;
use octocrab::{
models::pulls::PullRequest as OctoPullRequest,
params::{pulls::Sort, Direction, State},
Octocrab,
};
use url::Url;

Expand All @@ -14,15 +15,15 @@ pub struct PullRequestsSinceLastRelease {
}

impl PullRequestsSinceLastRelease {
pub async fn fetch() -> anyhow::Result<Self> {
pub async fn fetch(octocrab: &Octocrab) -> anyhow::Result<Self> {
let mut pull_requests = BTreeMap::new();
let mut page = 1u32;

let version_of_last_release = 'outer: loop {
const MAX_RESULTS_PER_PAGE: u8 = 100;

println!("Fetching page {}...", page);
let pull_request_page = octocrab::instance()
let pull_request_page = octocrab
.pulls("hannobraun", "Fornjot")
.list()
.state(State::Closed)
Expand Down Expand Up @@ -63,6 +64,11 @@ impl PullRequestsSinceLastRelease {
}
}

if pull_request.merged_at.is_none() {
// If it wasn't merged, we're not interested.
continue;
}

let number = pull_request.number;
let title = pull_request
.title
Expand Down
16 changes: 15 additions & 1 deletion tools/automator/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use std::env;

use anyhow::Context;
use octocrab::Octocrab;

use crate::{announcement::create_release_announcement, args::Args};

pub async fn run() -> anyhow::Result<()> {
let token = env::var("GITHUB_TOKEN")
.context("Loading env variable `GITHUB_TOKEN`")?;
let octocrab = Octocrab::builder().personal_token(token).build()?;

match Args::parse() {
Args::CreateReleaseAnnouncement(_) => {
create_release_announcement()
create_release_announcement(&octocrab)
.await
.context("Failed to create release announcement")?;
}
Args::Sponsors => {
let response: serde_json::Value =
octocrab.graphql("query { viewer { login }}").await?;
println!("{response}");

todo!("Querying sponsors is not supported yet.")
}
}

Ok(())
Expand Down