-
Notifications
You must be signed in to change notification settings - Fork 14
Post internals + blog PR after publishing dev-static releases #45
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use anyhow::Context; | ||
use curl::easy::Easy; | ||
|
||
pub trait BodyExt { | ||
fn with_body<S>(&mut self, body: S) -> Request<'_, S>; | ||
fn without_body(&mut self) -> Request<'_, ()>; | ||
} | ||
|
||
impl BodyExt for Easy { | ||
fn with_body<S>(&mut self, body: S) -> Request<'_, S> { | ||
Request { | ||
body: Some(body), | ||
client: self, | ||
} | ||
} | ||
fn without_body(&mut self) -> Request<'_, ()> { | ||
Request { | ||
body: None, | ||
client: self, | ||
} | ||
} | ||
} | ||
|
||
pub struct Request<'a, S> { | ||
body: Option<S>, | ||
client: &'a mut Easy, | ||
} | ||
|
||
impl<S: serde::Serialize> Request<'_, S> { | ||
pub fn send_with_response<T: serde::de::DeserializeOwned>(self) -> anyhow::Result<T> { | ||
use std::io::Read; | ||
let mut response = Vec::new(); | ||
let body = self.body.map(|body| serde_json::to_vec(&body).unwrap()); | ||
{ | ||
let mut transfer = self.client.transfer(); | ||
// The unwrap in the read_function is basically guaranteed to not | ||
// happen: reading into a slice can't fail. We can't use `?` since the | ||
// return type inside transfer isn't compatible with io::Error. | ||
if let Some(mut body) = body.as_deref() { | ||
transfer.read_function(move |dest| Ok(body.read(dest).unwrap()))?; | ||
} | ||
transfer.write_function(|new_data| { | ||
response.extend_from_slice(new_data); | ||
Ok(new_data.len()) | ||
})?; | ||
transfer.perform()?; | ||
} | ||
serde_json::from_slice(&response) | ||
.with_context(|| format!("{}", String::from_utf8_lossy(&response))) | ||
} | ||
|
||
pub fn send(self) -> anyhow::Result<()> { | ||
use std::io::Read; | ||
let body = self.body.map(|body| serde_json::to_vec(&body).unwrap()); | ||
{ | ||
let mut transfer = self.client.transfer(); | ||
// The unwrap in the read_function is basically guaranteed to not | ||
// happen: reading into a slice can't fail. We can't use `?` since the | ||
// return type inside transfer isn't compatible with io::Error. | ||
if let Some(mut body) = body.as_deref() { | ||
transfer.read_function(move |dest| Ok(body.read(dest).unwrap()))?; | ||
} | ||
transfer.perform()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::curl_helper::BodyExt; | ||
use curl::easy::Easy; | ||
|
||
pub struct Discourse { | ||
root: String, | ||
api_key: String, | ||
api_username: String, | ||
client: Easy, | ||
} | ||
|
||
impl Discourse { | ||
pub fn new(root: String, api_username: String, api_key: String) -> Discourse { | ||
Discourse { | ||
root, | ||
api_key, | ||
api_username, | ||
client: Easy::new(), | ||
} | ||
} | ||
|
||
fn start_new_request(&mut self) -> anyhow::Result<()> { | ||
self.client.reset(); | ||
self.client.useragent("rust-lang/promote-release")?; | ||
let mut headers = curl::easy::List::new(); | ||
headers.append(&format!("Api-Key: {}", self.api_key))?; | ||
headers.append(&format!("Api-Username: {}", self.api_username))?; | ||
headers.append("Content-Type: application/json")?; | ||
self.client.http_headers(headers)?; | ||
Ok(()) | ||
} | ||
|
||
/// Returns a URL to the topic | ||
pub fn create_topic( | ||
&mut self, | ||
category: u32, | ||
title: &str, | ||
body: &str, | ||
) -> anyhow::Result<String> { | ||
#[derive(serde::Serialize)] | ||
struct Request<'a> { | ||
title: &'a str, | ||
#[serde(rename = "raw")] | ||
body: &'a str, | ||
category: u32, | ||
archetype: &'a str, | ||
} | ||
#[derive(serde::Deserialize)] | ||
struct Response { | ||
topic_id: u32, | ||
topic_slug: String, | ||
} | ||
self.start_new_request()?; | ||
self.client.post(true)?; | ||
self.client.url(&format!("{}/posts.json", self.root))?; | ||
let resp = self | ||
.client | ||
.with_body(Request { | ||
title, | ||
body, | ||
category, | ||
archetype: "regular", | ||
}) | ||
.send_with_response::<Response>()?; | ||
Ok(format!( | ||
"{}/t/{}/{}", | ||
self.root, resp.topic_slug, resp.topic_id | ||
)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.