Skip to content

Commit ad86e13

Browse files
committed
Don't require GitHub credentials to fetch Rustup metadata
1 parent 883b5ed commit ad86e13

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/rustup.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::fs;
22
use std::path::{Path, PathBuf};
33

4-
use anyhow::{anyhow, Context as AnyhowContext, Error};
4+
use anyhow::{anyhow, Error};
5+
use curl::easy::Easy;
56
use serde::Deserialize;
67

78
use crate::config::Channel;
9+
use crate::curl_helper::BodyExt;
810
use crate::{run, Context};
911

1012
impl Context {
@@ -79,31 +81,57 @@ impl Context {
7981
}
8082

8183
fn get_head_sha_for_rustup(&self) -> anyhow::Result<String> {
82-
self.config
83-
.github()
84-
.context("failed to get HEAD SHA from GitHub - credentials not configured")?
85-
.token("rust-lang/rustup")?
86-
.get_ref("heads/stable")
84+
#[derive(Deserialize)]
85+
struct Commit {
86+
sha: String,
87+
}
88+
89+
let url = format!(
90+
"https://api.github.com/repos/rust-lang/rustup/commits/{}",
91+
self.config.channel
92+
);
93+
94+
let mut client = Easy::new();
95+
client.url(&url)?;
96+
client.useragent("rust-lang/promote-release")?;
97+
98+
let commit: Commit = client.without_body().send_with_response()?;
99+
100+
Ok(commit.sha)
87101
}
88102

89103
fn get_next_rustup_version(&self, sha: &str) -> anyhow::Result<String> {
90104
println!("Getting next Rustup version from Cargo.toml...");
91105

92-
#[derive(Debug, Deserialize)]
106+
#[derive(Deserialize)]
107+
struct Content {
108+
content: String,
109+
}
110+
111+
#[derive(Deserialize)]
93112
struct CargoToml {
113+
package: Package,
114+
}
115+
116+
#[derive(Deserialize)]
117+
struct Package {
94118
version: String,
95119
}
96120

97-
let cargo_toml = self
98-
.config
99-
.github()
100-
.context("failed to get new rustup version from GitHub - credentials not configured")?
101-
.token("rust-lang/rustup")?
102-
.read_file(Some(sha), "Cargo.toml")?;
121+
let url =
122+
format!("https://api.github.com/repos/rust-lang/rustup/contents/Cargo.toml?ref={sha}");
123+
124+
let mut client = Easy::new();
125+
client.url(&url)?;
126+
client.useragent("rust-lang/promote-release")?;
127+
128+
let content: Content = client.without_body().send_with_response()?;
129+
let decoded_content = base64::decode(&content.content.replace('\n', ""))?;
130+
let cargo_toml = String::from_utf8(decoded_content)?;
103131

104-
let toml: CargoToml = toml::from_str(&cargo_toml.content()?)?;
132+
let toml: CargoToml = toml::from_str(&cargo_toml)?;
105133

106-
Ok(toml.version)
134+
Ok(toml.package.version)
107135
}
108136

109137
fn download_rustup_artifacts(&mut self, sha: &str) -> Result<PathBuf, Error> {

0 commit comments

Comments
 (0)