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

milestone_prs: try to fetch src/version first #837

Merged
merged 1 commit into from
Sep 18, 2020
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
79 changes: 62 additions & 17 deletions src/handlers/milestone_prs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
handlers::Context,
};
use anyhow::Context as _;
use reqwest::StatusCode;

pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
let e = if let Event::Issue(e) = event {
Expand Down Expand Up @@ -39,8 +40,65 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
return Ok(());
};

// Fetch channel.rs from the upstream repository
// Fetch the version from the upstream repository.
let version = if let Some(version) = get_version_standalone(ctx, merge_sha).await? {
version
} else if let Some(version) = get_version_channelrs(ctx, merge_sha).await? {
version
} else {
log::error!("could not find the version of {:?}", merge_sha);
return Ok(());
};

if !version.starts_with("1.") && version.len() < 8 {
log::error!("Weird version {:?} for {:?}", version, merge_sha);
return Ok(());
}

// Associate this merged PR with the version it merged into.
//
// Note that this should work for rollup-merged PRs too. It will *not*
// auto-update when merging a beta-backport, for example, but that seems
// fine; we can manually update without too much trouble in that case, and
// eventually automate it separately.
e.issue.set_milestone(&ctx.github, &version).await?;

Ok(())
}

async fn get_version_standalone(ctx: &Context, merge_sha: &str) -> anyhow::Result<Option<String>> {
let resp = ctx
.github
.raw()
.get(&format!(
"https://github.com/rust-lang/rust/{}/src/version",
merge_sha
))
.send()
.await
.with_context(|| format!("retrieving src/version for {}", merge_sha))?;

match resp.status() {
StatusCode::OK => {}
// Don't treat a 404 as a failure, we'll try another way to retrieve the version.
StatusCode::NOT_FOUND => return Ok(None),
status => anyhow::bail!(
"unexpected status code {} while retrieving src/version for {}",
status,
merge_sha
),
}

Ok(Some(
resp.text()
.await
.with_context(|| format!("deserializing src/version for {}", merge_sha))?
.trim()
.to_string(),
))
}

async fn get_version_channelrs(ctx: &Context, merge_sha: &str) -> anyhow::Result<Option<String>> {
let resp = ctx
.github
.raw()
Expand All @@ -66,28 +124,15 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
prefix,
merge_sha
);
return Ok(());
return Ok(None);
};
let after = &resp[start..];
let end = if let Some(idx) = after.find('"') {
idx
} else {
log::error!("No suffix in contents of channel.rs at {:?}", merge_sha);
return Ok(());
return Ok(None);
};
let version = &after[..end];
if !version.starts_with("1.") && version.len() < 8 {
log::error!("Weird version {:?} for {:?}", version, merge_sha);
return Ok(());
}

// Associate this merged PR with the version it merged into.
//
// Note that this should work for rollup-merged PRs too. It will *not*
// auto-update when merging a beta-backport, for example, but that seems
// fine; we can manually update without too much trouble in that case, and
// eventually automate it separately.
e.issue.set_milestone(&ctx.github, version).await?;

Ok(())
Ok(Some(after[..end].to_string()))
}