Skip to content

Commit ccd848f

Browse files
committed
Split fetching and processing in range-diff endpoint
1 parent 9473f65 commit ccd848f

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/gh_range_diff.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use imara_diff::{
2020
use pulldown_cmark_escape::FmtWriter;
2121
use regex::Regex;
2222

23+
use crate::github::GithubCompare;
2324
use crate::{github, handlers::Context, utils::AppError};
2425

2526
static MARKER_RE: LazyLock<Regex> =
@@ -31,7 +32,7 @@ static MARKER_RE: LazyLock<Regex> =
3132
pub async fn gh_range_diff(
3233
Path((owner, repo, basehead)): Path<(String, String, String)>,
3334
State(ctx): State<Arc<Context>>,
34-
Host(host): Host,
35+
host: Host,
3536
) -> axum::response::Result<impl IntoResponse, AppError> {
3637
let Some((oldhead, newhead)) = basehead.split_once("..") else {
3738
return Ok((
@@ -41,9 +42,6 @@ pub async fn gh_range_diff(
4142
));
4243
};
4344

44-
// Configure unified diff
45-
let config = UnifiedDiffConfig::default();
46-
4745
let repos = ctx
4846
.team
4947
.repos()
@@ -96,18 +94,14 @@ pub async fn gh_range_diff(
9694
.sha;
9795

9896
// Get the comparison between the oldbase..oldhead
99-
let mut old = ctx
97+
let old = ctx
10098
.github
10199
.compare(&issue_repo, &oldbase, oldhead)
102100
.await
103101
.with_context(|| {
104102
format!("failed to retrive the comparison between {oldbase} and {oldhead}")
105103
})?;
106104

107-
// Sort by filename, so it's consistent with GitHub UI
108-
old.files
109-
.sort_unstable_by(|f1, f2| f1.filename.cmp(&f2.filename));
110-
111105
anyhow::Result::<_>::Ok((oldbase, old))
112106
};
113107

@@ -125,24 +119,43 @@ pub async fn gh_range_diff(
125119
.sha;
126120

127121
// Get the comparison between the newbase..newhead
128-
let mut new = ctx
122+
let new = ctx
129123
.github
130124
.compare(&issue_repo, &newbase, newhead)
131125
.await
132126
.with_context(|| {
133127
format!("failed to retrive the comparison between {newbase} and {newhead}")
134128
})?;
135129

136-
// Sort by filename, so it's consistent with GitHub UI
137-
new.files
138-
.sort_unstable_by(|f1, f2| f1.filename.cmp(&f2.filename));
139-
140130
anyhow::Result::<_>::Ok((newbase, new))
141131
};
142132

143133
// Wait for both futures and early exit if there is an error
144134
let ((oldbase, old), (newbase, new)) = futures::try_join!(old, new)?;
145135

136+
process_old_new(
137+
host,
138+
(&owner, &repo),
139+
(&oldbase, oldhead, old),
140+
(&newbase, newhead, new),
141+
)
142+
}
143+
144+
fn process_old_new(
145+
Host(host): Host,
146+
(owner, repo): (&str, &str),
147+
(oldbase, oldhead, mut old): (&str, &str, GithubCompare),
148+
(newbase, newhead, mut new): (&str, &str, GithubCompare),
149+
) -> axum::response::Result<(StatusCode, HeaderMap, String), AppError> {
150+
// Configure unified diff
151+
let config = UnifiedDiffConfig::default();
152+
153+
// Sort by filename, so it's consistent with GitHub UI
154+
old.files
155+
.sort_unstable_by(|f1, f2| f1.filename.cmp(&f2.filename));
156+
new.files
157+
.sort_unstable_by(|f1, f2| f1.filename.cmp(&f2.filename));
158+
146159
// Create the HTML buffer with a very rough approximation for the capacity
147160
let mut html: String = String::with_capacity(800 + old.files.len() * 100);
148161

0 commit comments

Comments
 (0)