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

Use milestones for rust-lang/rust changes #77

Merged
merged 2 commits into from
Feb 3, 2022
Merged
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
130 changes: 111 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use askama::Template;
use chrono::prelude::*;
use chrono::Duration;

use reqwest::header::HeaderMap;
use serde_json as json;

type JsonRefArray<'a> = Vec<&'a json::Value>;
Expand Down Expand Up @@ -54,9 +55,10 @@ fn main() {
while today - end > six_weeks {
end = end + six_weeks;
}

let start = end - six_weeks;
let issues = get_issues(start, end, "rust");

let mut issues = get_issues_by_milestone(&version, "rust");
issues.sort_by_cached_key(|issue| issue["number"].as_u64().unwrap());

// Skips `beta-accepted` as those PRs were backported onto the
// previous stable.
Expand Down Expand Up @@ -84,7 +86,8 @@ fn main() {
let (compat_unsorted, libraries_unsorted, language_unsorted, compiler_unsorted, unsorted) =
partition_prs(rest);

let cargo_issues = get_issues(start, end, "cargo");
let mut cargo_issues = get_issues_by_date(start, end, "cargo");
cargo_issues.sort_by_cached_key(|issue| issue["number"].as_u64().unwrap());

let (cargo_relnotes, cargo_unsorted) = {
let (relnotes, rest) = partition_by_tag(cargo_issues.iter(), relnotes_tags);
Expand Down Expand Up @@ -119,21 +122,99 @@ fn main() {
println!("{}", relnotes.render().unwrap());
}

fn get_issues(start: Date<Utc>, end: Date<Utc>, repo_name: &'static str) -> Vec<json::Value> {
fn get_issues_by_milestone(version: &str, repo_name: &'static str) -> Vec<json::Value> {
use reqwest::blocking::Client;
use reqwest::header::*;

let token = env::var("GITHUB_TOKEN").expect("Set GITHUB_TOKEN to a valid token");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());
headers.insert(ACCEPT, "application/json".parse().unwrap());
headers.insert(
AUTHORIZATION,
format!("Bearer {}", token)
.parse()
.unwrap(),
);
headers.insert(USER_AGENT, "Rust-relnotes/0.1.0".parse().unwrap());
let headers = request_header();
let mut args = BTreeMap::new();
args.insert("states", String::from("[MERGED]"));
args.insert("last", String::from("100"));
let mut issues = Vec::new();

loop {
let query = format!(
r#"
query {{
repository(owner: "rust-lang", name: "{repo_name}") {{
milestones(query: "{version}", first: 1) {{
totalCount
nodes {{
pullRequests({args}) {{
nodes {{
number
title
url
labels(last: 100) {{
nodes {{
name
}}
}}
}}
pageInfo {{
startCursor
}}
}}
}}
}}
}}
}}"#,
repo_name = repo_name,
version = version,
args = args
.iter()
.map(|(k, v)| format!("{}: {}", k, v))
.collect::<Vec<_>>()
.join(",")
)
.replace(" ", "")
.replace("\n", " ")
.replace('"', "\\\"");

let json_query = format!("{{\"query\": \"{}\"}}", query);

let client = Client::new();

let json = client
.post("https://api.github.com/graphql")
.headers(headers.clone())
.body(json_query)
.send()
.unwrap()
.json::<json::Value>()
.unwrap();

let milestones_data = json["data"]["repository"]["milestones"].clone();
assert_eq!(
milestones_data["totalCount"].as_u64().unwrap(),
1,
"More than one milestone matched the query \"{version}\". Please be more specific.",
version = version
);
let pull_requests_data = milestones_data["nodes"][0]["pullRequests"].clone();

let mut pull_requests = pull_requests_data["nodes"].as_array().unwrap().clone();
issues.append(&mut pull_requests);

match &pull_requests_data["pageInfo"]["startCursor"] {
json::Value::String(cursor) => {
args.insert("before", format!("\"{}\"", cursor));
}
json::Value::Null => {
break issues;
}
_ => unreachable!(),
}
}
}

fn get_issues_by_date(
start: Date<Utc>,
end: Date<Utc>,
repo_name: &'static str,
) -> Vec<json::Value> {
use reqwest::blocking::Client;

let headers = request_header();
let mut args = BTreeMap::new();
args.insert("states", String::from("[MERGED]"));
args.insert("last", String::from("100"));
Expand All @@ -142,9 +223,9 @@ fn get_issues(start: Date<Utc>, end: Date<Utc>, repo_name: &'static str) -> Vec<

loop {
let query = format!(
"
r#"
query {{
repository(owner: \"rust-lang\", name: \"{repo_name}\") {{
repository(owner: "rust-lang", name: "{repo_name}") {{
pullRequests({args}) {{
nodes {{
mergedAt
Expand All @@ -162,7 +243,7 @@ fn get_issues(start: Date<Utc>, end: Date<Utc>, repo_name: &'static str) -> Vec<
}}
}}
}}
}}",
}}"#,
repo_name = repo_name,
args = args
.iter()
Expand Down Expand Up @@ -229,6 +310,17 @@ fn get_issues(start: Date<Utc>, end: Date<Utc>, repo_name: &'static str) -> Vec<
}
}

fn request_header() -> HeaderMap {
use reqwest::header::*;
let token = env::var("GITHUB_TOKEN").expect("Set GITHUB_TOKEN to a valid token");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());
headers.insert(ACCEPT, "application/json".parse().unwrap());
headers.insert(AUTHORIZATION, format!("Bearer {}", token).parse().unwrap());
headers.insert(USER_AGENT, "Rust-relnotes/0.1.0".parse().unwrap());
headers
}

fn map_to_line_items<'a>(
prefix: &'static str,
iter: impl IntoIterator<Item = &'a json::Value>,
Expand Down