-
Notifications
You must be signed in to change notification settings - Fork 97
Update review body with link to the changes since the review #2163
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,64 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::Context as _; | ||
| use axum::{ | ||
| extract::{Path, State}, | ||
| response::{IntoResponse, Redirect, Response}, | ||
| }; | ||
| use hyper::StatusCode; | ||
|
|
||
| use crate::{ | ||
| github, | ||
| handlers::Context, | ||
| utils::{AppError, is_repo_autorized}, | ||
| }; | ||
|
|
||
| /// Redirects to either `/gh-range-diff` (when the base changed) or to GitHub's compare | ||
| /// page (when the base is the same). | ||
| /// | ||
| /// Takes an PR number and an `oldbase..oldhead` representing the range we are starting from. | ||
| pub async fn gh_changes_since( | ||
| Path((owner, repo, pr_num, oldbasehead)): Path<(String, String, u64, String)>, | ||
| State(ctx): State<Arc<Context>>, | ||
| ) -> axum::response::Result<Response, AppError> { | ||
| let Some((oldbase, oldhead)) = oldbasehead.split_once("..") else { | ||
| return Ok(( | ||
| StatusCode::BAD_REQUEST, | ||
| format!("`{oldbasehead}` is not in the form `base..head`"), | ||
| ) | ||
| .into_response()); | ||
| }; | ||
|
|
||
| if !is_repo_autorized(&ctx, &owner, &repo).await? { | ||
| return Ok(( | ||
| StatusCode::UNAUTHORIZED, | ||
| format!("repository `{owner}/{repo}` is not part of the Rust Project team repos"), | ||
| ) | ||
| .into_response()); | ||
| } | ||
|
|
||
| let issue_repo = github::IssueRepository { | ||
| organization: owner.to_string(), | ||
| repository: repo.to_string(), | ||
| }; | ||
|
|
||
| let pr = ctx.github.pull_request(&issue_repo, pr_num).await?; | ||
|
|
||
| let newbase = &pr.base.as_ref().context("no base")?.sha; | ||
| let newhead = &pr.head.as_ref().context("no head")?.sha; | ||
|
|
||
| // Has the base changed? | ||
| if oldbase == newbase { | ||
| // No, redirect to GitHub native compare page | ||
| return Ok(Redirect::to(&format!( | ||
| "https://github.com/{owner}/{repo}/compare/{oldhead}..{newhead}" | ||
| )) | ||
| .into_response()); | ||
| } | ||
|
|
||
| // Yes, use our Github range-diff instead | ||
| Ok(Redirect::to(&format!( | ||
| "/gh-range-diff/{owner}/{repo}/{oldbase}..{oldhead}/{newbase}..{newhead}" | ||
| )) | ||
| .into_response()) | ||
| } | ||
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
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
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,55 @@ | ||
| use anyhow::Context as _; | ||
|
|
||
| use crate::{ | ||
| config::ReviewChangesSinceConfig, | ||
| github::{Comment, Event, Issue, IssueCommentAction, IssueCommentEvent}, | ||
| handlers::Context, | ||
| }; | ||
|
|
||
| /// Checks if this event is a PR review creation and adds in the body a link our `gh-changes-since` | ||
| /// endpoint to view changes since this review. | ||
| pub(crate) async fn handle( | ||
Urgau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx: &Context, | ||
| host: &str, | ||
| event: &Event, | ||
| _config: &ReviewChangesSinceConfig, | ||
| ) -> anyhow::Result<()> { | ||
| if let Event::IssueComment( | ||
| event @ IssueCommentEvent { | ||
| action: IssueCommentAction::Created, | ||
| issue: Issue { | ||
| pull_request: Some(_), | ||
| .. | ||
| }, | ||
| comment: | ||
| Comment { | ||
| pr_review_state: Some(_), | ||
| .. | ||
| }, | ||
| .. | ||
| }, | ||
| ) = event | ||
| { | ||
| // Add link our gh-changes-since endpoint to view changes since this review | ||
|
|
||
| let issue_repo = event.issue.repository(); | ||
| let pr_num = event.issue.number; | ||
|
|
||
| let base = &event.issue.base.as_ref().context("no base")?.sha; | ||
| let head = &event.issue.head.as_ref().context("no head")?.sha; | ||
|
|
||
| let link = format!("https://{host}/gh-changes-since/{issue_repo}/{pr_num}/{base}..{head}"); | ||
| let new_body = format!( | ||
| "{}\n\n*[View changes since this review]({link})*", | ||
| event.comment.body | ||
| ); | ||
|
|
||
| event | ||
| .issue | ||
| .edit_review(&ctx.github, event.comment.id, &new_body) | ||
| .await | ||
| .context("failed to update the review body")?; | ||
| } | ||
|
|
||
| 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
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
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.