Skip to content

Commit 44937e8

Browse files
authored
feat: support tracking stable branches (#14)
1 parent 02c9aa6 commit 44937e8

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nixpkgs-track/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ thiserror = "2.0.11"
2727
env_logger = "0.11.6"
2828
user_dirs = "0.2.0"
2929
dialoguer = "0.11.0"
30+
regex = "1.11.1"
3031

3132
[lints]
3233
workspace = true

crates/nixpkgs-track/src/main.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55
use clap::{Parser, Subcommand};
66
use dialoguer::MultiSelect;
77
use miette::{Context, IntoDiagnostic};
8+
use regex::Regex;
89
use serde::{Deserialize, Serialize};
910

1011
use chrono::Utc;
@@ -13,7 +14,8 @@ use yansi::hyperlink::HyperlinkExt;
1314
use nixpkgs_track::utils::{format_seconds_to_time_ago, parse_pull_request_id};
1415
use nixpkgs_track_lib::{branch_contains_commit, fetch_nixpkgs_pull_request, NixpkgsTrackError};
1516

16-
static DEFAULT_BRANCHES: [&str; 6] = ["master", "staging", "staging-next", "nixpkgs-unstable", "nixos-unstable-small", "nixos-unstable"];
17+
static ROLLING_BRANCHES: [&str; 6] = ["staging", "staging-next", "master", "nixpkgs-unstable", "nixos-unstable-small", "nixos-unstable"];
18+
static STABLE_BRANCHES_TEMPLATE: [&str; 6] = ["staging-XX.XX", "staging-next-XX.XX", "release-XX.XX", "nixpkgs-XX.XX-darwin", "nixos-XX.XX-small", "nixos-XX.XX"];
1719

1820
#[derive(Parser)]
1921
#[command(version, about, subcommand_negates_reqs = true)]
@@ -93,11 +95,40 @@ async fn check(client: Arc<reqwest::Client>, pull_request: u64, token: Option<&s
9395
.signed_duration_since(pull_request.created_at)
9496
.num_seconds(),
9597
);
98+
let merged_into_branch = pull_request.base.r#ref;
99+
100+
writeln!(
101+
output,
102+
"Merged {merged_at_ago} ago ({merged_at_date}), {creation_to_merge_time} after creation, into branch '{merged_into_branch}'."
103+
)?;
104+
105+
let stable_branches: Option<Vec<String>> = if ROLLING_BRANCHES.contains(&merged_into_branch.as_str()) {
106+
None
107+
} else {
108+
// regex for stable version XX.XX
109+
let stable_version_regex = Regex::new(r"[0-9]+\.[0-9]+$").unwrap();
110+
if let Some(stable_version) = stable_version_regex.find(&merged_into_branch) {
111+
let stable_branches = STABLE_BRANCHES_TEMPLATE
112+
.iter()
113+
.map(|s| s.replace("XX.XX", stable_version.as_str()))
114+
.collect();
115+
Some(stable_branches)
116+
} else {
117+
None
118+
}
119+
};
96120

97-
writeln!(output, "Merged {merged_at_ago} ago ({merged_at_date}), {creation_to_merge_time} after creation.")?;
121+
#[allow(clippy::redundant_closure_for_method_calls)]
122+
let tracked_branches = match stable_branches {
123+
Some(ref stable_branches) => stable_branches
124+
.iter()
125+
.map(|s| s.as_str())
126+
.collect(),
127+
None => Vec::from(ROLLING_BRANCHES),
128+
};
98129

99130
let mut branches = tokio::task::JoinSet::new();
100-
for (i, branch) in DEFAULT_BRANCHES.iter().enumerate() {
131+
for (i, branch) in tracked_branches.iter().enumerate() {
101132
let token_clone = token.map(ToOwned::to_owned);
102133
let branch_clone = (*branch).to_string();
103134
let commit_sha_clone = commit_sha.clone();
@@ -114,7 +145,7 @@ async fn check(client: Arc<reqwest::Client>, pull_request: u64, token: Option<&s
114145

115146
for (i, result) in results {
116147
let has_pull_request = result?;
117-
writeln!(output, "{}: {}", DEFAULT_BRANCHES[i], if has_pull_request { "✅" } else { "🚫" })?;
148+
writeln!(output, "{}: {}", tracked_branches[i], if has_pull_request { "✅" } else { "🚫" })?;
118149
}
119150
} else {
120151
let created_at_ago = format_seconds_to_time_ago(

crates/nixpkgs-track_lib/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ pub async fn fetch_nixpkgs_pull_request(client: impl AsRef<reqwest::Client>, pul
4242
}
4343

4444
pub async fn branch_contains_commit(client: impl AsRef<reqwest::Client>, branch: &str, commit: &str, token: Option<&str>) -> Result<bool, NixpkgsTrackError> {
45-
let url = format!("{BASE_API_URL}/compare/{branch}...{commit}");
45+
// `per_page=1000000&page=100`: a hack for the API, to _not_ return
46+
// information about files or commits, which we do not need here;
47+
// we only need to know whether it's `ahead` or `behind`
48+
let url = format!("{BASE_API_URL}/compare/{branch}...{commit}?per_page=1000000&page=100");
4649
let response = build_request(client, &url, token)
4750
.send()
4851
.await;
@@ -69,6 +72,7 @@ pub struct User {
6972
pub url: String,
7073
}
7174

75+
#[non_exhaustive]
7276
#[derive(Clone, Debug, Deserialize)]
7377
pub struct PullRequest {
7478
pub html_url: String,
@@ -79,6 +83,20 @@ pub struct PullRequest {
7983
pub merged_at: Option<DateTime<Utc>>,
8084
pub merged: bool,
8185
pub merge_commit_sha: Option<String>,
86+
/// Base branch that the pull request is merged into
87+
pub base: ForkBranch,
88+
/// Head branch that the pull request is merged from
89+
pub head: ForkBranch,
90+
}
91+
92+
#[non_exhaustive]
93+
#[derive(Clone, Debug, Deserialize)]
94+
pub struct ForkBranch {
95+
/// Fork and branch name in the format "owner:branch"
96+
pub label: String,
97+
/// Branch name in a given fork
98+
pub r#ref: String,
99+
pub sha: String,
82100
}
83101

84102
#[derive(Clone, Debug, Deserialize)]

0 commit comments

Comments
 (0)