Skip to content

Commit 76b673c

Browse files
authored
Merge pull request #1967 from Urgau/check_commits-auto-ident
Automatically indent warnings messages for `check_commits` sub-handler
2 parents 05aa65d + 4894129 commit 76b673c

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

src/handlers/check_commits.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ async fn handle_warnings_and_labels(
204204
fn warning_from_warnings(warnings: &[String]) -> String {
205205
let warnings: Vec<_> = warnings
206206
.iter()
207+
.map(|warning| warning.trim().replace("\n", "\n "))
207208
.map(|warning| format!("* {warning}"))
208209
.collect();
209210
format!(":warning: **Warning** :warning:\n\n{}", warnings.join("\n"))
@@ -241,3 +242,45 @@ fn dummy_commit_from_body(sha: &str, body: &str) -> GithubCommit {
241242
parents: vec![],
242243
}
243244
}
245+
246+
#[test]
247+
#[rustfmt::skip]
248+
fn test_warning_from_warnings() {
249+
assert_eq!(
250+
warning_from_warnings(
251+
&[
252+
r#"This line should NOT be intend with 4 spaces,
253+
but this line should!"#
254+
.to_string()
255+
]
256+
),
257+
r#":warning: **Warning** :warning:
258+
259+
* This line should NOT be intend with 4 spaces,
260+
but this line should!"#
261+
);
262+
263+
assert_eq!(
264+
warning_from_warnings(&[
265+
r#"This is warning 1.
266+
267+
Look at this list:
268+
- 12
269+
- 13"#
270+
.to_string(),
271+
r#"This is warning 2.
272+
- 123456789
273+
"#
274+
.to_string()
275+
]),
276+
r#":warning: **Warning** :warning:
277+
278+
* This is warning 1.
279+
280+
Look at this list:
281+
- 12
282+
- 13
283+
* This is warning 2.
284+
- 123456789"#
285+
);
286+
}

src/handlers/check_commits/behind_upstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(super) async fn behind_upstream(
2929
return Some(format!(
3030
r"This PR is based on an upstream commit that is {} days old.
3131
32-
*It's recommended to update your branch according to the [rustc_dev_guide](https://rustc-dev-guide.rust-lang.org/contributing.html#keeping-your-branch-up-to-date).*",
32+
*It's recommended to update your branch according to the [rustc_dev_guide](https://rustc-dev-guide.rust-lang.org/contributing.html#keeping-your-branch-up-to-date).*",
3333
days_old
3434
));
3535
}

src/handlers/check_commits/issue_links.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ pub(super) fn issue_links_in_commits(
2525
.any(|i| c.commit.message.starts_with(i))
2626
})
2727
.filter(|c| LINKED_RE.is_match(&c.commit.message))
28-
.map(|c| format!(" - {}\n", c.sha))
28+
.map(|c| format!("- {}\n", c.sha))
2929
.collect::<String>();
3030

3131
if issue_links_commits.is_empty() {
3232
None
3333
} else {
3434
Some(format!(
3535
r"There are issue links (such as `#123`) in the commit messages of the following commits.
36-
*Please remove them as they will spam the issue with references to the commit.*
36+
*Please remove them as they will spam the issue with references to the commit.*
3737
{issue_links_commits}",
3838
))
3939
}
@@ -78,8 +78,8 @@ fn test_mentions_in_commits() {
7878
issue_links_in_commits(&config, &commits),
7979
Some(
8080
r"There are issue links (such as `#123`) in the commit messages of the following commits.
81-
*Please remove them as they will spam the issue with references to the commit.*
82-
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
81+
*Please remove them as they will spam the issue with references to the commit.*
82+
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
8383
".to_string()
8484
)
8585
);
@@ -103,9 +103,9 @@ fn test_mentions_in_commits() {
103103
issue_links_in_commits(&config, &commits),
104104
Some(
105105
r"There are issue links (such as `#123`) in the commit messages of the following commits.
106-
*Please remove them as they will spam the issue with references to the commit.*
107-
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
108-
- 891f0916a07c215ae8173f782251422f1fea6acb
106+
*Please remove them as they will spam the issue with references to the commit.*
107+
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
108+
- 891f0916a07c215ae8173f782251422f1fea6acb
109109
".to_string()
110110
)
111111
);

src/handlers/check_commits/no_mentions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub(super) fn mentions_in_commits(
1010
let mentions_commits = commits
1111
.into_iter()
1212
.filter(|c| !parser::get_mentions(&c.commit.message).is_empty())
13-
.map(|c| format!(" - {}\n", c.sha))
13+
.map(|c| format!("- {}\n", c.sha))
1414
.collect::<String>();
1515

1616
if mentions_commits.is_empty() {
1717
None
1818
} else {
1919
Some(format!(
2020
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
21-
*Please remove the mentions to avoid spamming these users.*
21+
*Please remove the mentions to avoid spamming these users.*
2222
{mentions_commits}",
2323
))
2424
}
@@ -53,8 +53,8 @@ Co-authored-by: Baz Qux <[email protected]>",
5353
mentions_in_commits(&NoMentionsConfig {}, &commits),
5454
Some(
5555
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
56-
*Please remove the mentions to avoid spamming these users.*
57-
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
56+
*Please remove the mentions to avoid spamming these users.*
57+
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
5858
".to_string()
5959
)
6060
);

src/handlers/check_commits/no_merges.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ so these commits will need to be removed for this pull request to be merged.
6161
);
6262

6363
for commit in commits {
64-
writeln!(message, " - {commit}").unwrap();
64+
writeln!(message, "- {commit}").unwrap();
6565
}
6666

6767
writeln!(
6868
message,
6969
"
70-
You can start a rebase with the following commands:
71-
```shell-session
72-
$ # rebase
73-
$ git pull --rebase https://github.com/{repository_name}.git {default_branch}
74-
$ git push --force-with-lease
75-
```"
70+
You can start a rebase with the following commands:
71+
```shell-session
72+
$ # rebase
73+
$ git pull --rebase https://github.com/{repository_name}.git {default_branch}
74+
$ git push --force-with-lease
75+
```"
7676
)
7777
.unwrap();
7878

@@ -126,14 +126,14 @@ fn end_to_end() {
126126
unreachable!()
127127
};
128128
assert_eq!(warning, "The following commits have merge commits (commits with multiple parents) in your changes. We have a [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so these commits will need to be removed for this pull request to be merged.
129-
- 9cc6dce67c917fe5937e984f58f5003ccbb5c37e
130-
131-
You can start a rebase with the following commands:
132-
```shell-session
133-
$ # rebase
134-
$ git pull --rebase https://github.com/rust-lang/rust.git master
135-
$ git push --force-with-lease
136-
```
129+
- 9cc6dce67c917fe5937e984f58f5003ccbb5c37e
130+
131+
You can start a rebase with the following commands:
132+
```shell-session
133+
$ # rebase
134+
$ git pull --rebase https://github.com/rust-lang/rust.git master
135+
$ git push --force-with-lease
136+
```
137137
");
138138
assert_eq!(labels, vec!["merge-commits"]);
139139
}

0 commit comments

Comments
 (0)