Skip to content

Commit e06e1e9

Browse files
committed
Sort by total number of lines changed to improve rendered link guess
1 parent 25b1894 commit e06e1e9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/github.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,9 @@ pub struct PullRequestFile {
11001100
pub sha: String,
11011101
pub filename: String,
11021102
pub blob_url: String,
1103+
pub additions: u64,
1104+
pub deletions: u64,
1105+
pub changes: u64,
11031106
}
11041107

11051108
#[derive(Debug, serde::Deserialize)]

src/handlers/rendered_link.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::bail;
44

55
use crate::{
66
config::RenderedLinkConfig,
7-
github::{Event, IssuesAction, IssuesEvent},
7+
github::{Event, IssuesAction, IssuesEvent, PullRequestFile},
88
handlers::Context,
99
};
1010

@@ -42,7 +42,7 @@ async fn add_rendered_link(
4242

4343
let rendered_link = files
4444
.iter()
45-
.find(|f| {
45+
.filter(|f| {
4646
config
4747
.trigger_files
4848
.iter()
@@ -52,7 +52,10 @@ async fn add_rendered_link(
5252
.iter()
5353
.any(|tf| f.filename.starts_with(tf))
5454
})
55-
.and_then(|file| {
55+
// Sort the relavant files by the total number of lines changed, as to
56+
// improve our guess for the relevant file to show the link to.
57+
.max_by(|f1, f2| total_lines_changed(f1).cmp(&total_lines_changed(f2)))
58+
.map(|file| {
5659
let head = e.issue.head.as_ref()?;
5760
let base = e.issue.base.as_ref()?;
5861

@@ -88,7 +91,8 @@ async fn add_rendered_link(
8891
},
8992
file.filename
9093
))
91-
});
94+
})
95+
.flatten();
9296

9397
let new_body: Cow<'_, str> = if !e.issue.body.contains("[Rendered]") {
9498
if let Some(rendered_link) = rendered_link {
@@ -128,3 +132,7 @@ async fn add_rendered_link(
128132

129133
Ok(())
130134
}
135+
136+
fn total_lines_changed(f: &PullRequestFile) -> u64 {
137+
f.additions + f.deletions + f.changes
138+
}

0 commit comments

Comments
 (0)