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

Improve rendered link handler with proper edit and sha-based link #1848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 48 additions & 23 deletions src/handlers/rendered_link.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use crate::{
github::{Event, IssuesAction, IssuesEvent},
handlers::Context,
interactions::EditIssueBody,
};

#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
struct RenderedLinkData {
rendered_link: String,
}

pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
let e = if let Event::Issue(e) = event {
e
} else {
return Ok(());
};

if !e.issue.is_pr() {
return Ok(());
}

let repo = e.issue.repository();
let prefix = match (&*repo.organization, &*repo.repository) {
("rust-lang", "rfcs") => "text/",
Expand All @@ -25,32 +35,47 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
}

async fn add_rendered_link(ctx: &Context, e: &IssuesEvent, prefix: &str) -> anyhow::Result<()> {
if e.action == IssuesAction::Opened {
if e.action == IssuesAction::Opened
|| e.action == IssuesAction::Closed
|| e.action == IssuesAction::Reopened
{
let edit = EditIssueBody::new(&e.issue, "RENDERED_LINK");
let current_data: Option<RenderedLinkData> = edit.current_data();

let files = e.issue.files(&ctx.github).await?;

if let Some(file) = files.iter().find(|f| f.filename.starts_with(prefix)) {
if !e.issue.body.contains("[Rendered]") {
// This URL should be stable while the PR is open, even if the
// user pushes new commits.
//
// It will go away if the user deletes their branch, or if
// they reset it (such as if they created a PR from master).
// That should usually only happen after the PR is closed.
// During the closing process, the closer should update the
// Rendered link to the new location (which we should
// automate!).
let head = e.issue.head.as_ref().unwrap();
let url = format!(
"https://github.com/{}/blob/{}/{}",
head.repo.full_name, head.git_ref, file.filename
);
e.issue
.edit_body(
&ctx.github,
&format!("{}\n\n[Rendered]({})", e.issue.body, url),
)
.await?;
}
let mut current_data = current_data.unwrap_or_default();
let head = e.issue.head.as_ref().unwrap();

// This URL should be stable while the PR is open, even if the
// user pushes new commits.
//
// It will go away if the user deletes their branch, or if
// they reset it (such as if they created a PR from master).
// That should usually only happen after the PR is closed
// a which point we switch to a SHA-based url.
current_data.rendered_link = format!(
"https://github.com/{}/blob/{}/{}",
head.repo.full_name,
if e.action == IssuesAction::Closed {
&head.sha
} else {
&head.git_ref
},
file.filename
);

edit.apply(
&ctx.github,
format!("[Rendered]({})", &current_data.rendered_link),
current_data,
)
.await?;
} else if let Some(mut current_data) = current_data {
// No render link to show, but one previously, so remove it
current_data.rendered_link = String::new();
edit.apply(&ctx.github, String::new(), current_data).await?;
}
}

Expand Down
Loading