Skip to content

Commit 4d7126f

Browse files
Add rendered link when RFCs are opened
1 parent e19ff14 commit 4d7126f

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/github.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,26 @@ impl Issue {
745745
let diff = client.send_req(req).await?;
746746
Ok(Some(String::from(String::from_utf8_lossy(&diff))))
747747
}
748+
749+
pub async fn files(&self, client: &GithubClient) -> anyhow::Result<Vec<PullRequestFile>> {
750+
if !self.is_pr() {
751+
return Ok(vec![]);
752+
}
753+
754+
let req = client.get(&format!(
755+
"{}/pulls/{}/files",
756+
self.repository().url(),
757+
self.number
758+
));
759+
Ok(client.json(req).await?)
760+
}
761+
}
762+
763+
#[derive(Debug, serde::Deserialize)]
764+
pub struct PullRequestFile {
765+
pub sha: String,
766+
pub filename: String,
767+
pub blob_url: String,
748768
}
749769

750770
#[derive(serde::Serialize)]
@@ -853,7 +873,7 @@ pub struct IssuesEvent {
853873
#[derive(Debug, serde::Deserialize)]
854874
struct PullRequestEventFields {}
855875

856-
#[derive(Default, Clone, Debug, serde::Deserialize)]
876+
#[derive(Clone, Debug, serde::Deserialize)]
857877
pub struct CommitBase {
858878
sha: String,
859879
}

src/handlers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod ping;
3838
mod prioritize;
3939
mod relabel;
4040
mod review_submitted;
41+
mod rfc_helper;
4142
mod rustc_commits;
4243
mod shortcut;
4344

@@ -77,6 +78,14 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
7778
);
7879
}
7980

81+
if let Err(e) = rfc_helper::handle(ctx, event).await {
82+
log::error!(
83+
"failed to process event {:?} with rfc_helper handler: {:?}",
84+
event,
85+
e
86+
);
87+
}
88+
8089
if let Some(config) = config
8190
.as_ref()
8291
.ok()

src/handlers/rfc_helper.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::{
2+
github::{Event, IssuesAction, IssuesEvent},
3+
handlers::Context,
4+
};
5+
6+
pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
7+
let e = if let Event::Issue(e) = event {
8+
e
9+
} else {
10+
return Ok(());
11+
};
12+
13+
let repo = e.issue.repository();
14+
if !(repo.organization == "rust-lang" && repo.repository == "rfcs") {
15+
return Ok(());
16+
}
17+
18+
if let Err(e) = add_rendered_link(&ctx, &e).await {
19+
tracing::error!("Error adding rendered link: {:?}", e);
20+
}
21+
22+
Ok(())
23+
}
24+
25+
async fn add_rendered_link(ctx: &Context, e: &IssuesEvent) -> anyhow::Result<()> {
26+
if e.action == IssuesAction::Opened || e.action == IssuesAction::Synchronize {
27+
let files = e.issue.files(&ctx.github).await?;
28+
29+
if let Some(file) = files.iter().find(|f| f.filename.starts_with("text/")) {
30+
if !e.issue.body.contains("[Rendered]") {
31+
e.issue
32+
.edit_body(
33+
&ctx.github,
34+
&format!("{}\n\n[Rendered]({})", e.issue.body, file.blob_url),
35+
)
36+
.await?;
37+
}
38+
}
39+
}
40+
41+
Ok(())
42+
}

0 commit comments

Comments
 (0)