Skip to content

Commit 113a2e2

Browse files
committed
feat: outputting in GitHub Actions error format
1 parent 932945f commit 113a2e2

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::fmt::Write;
2+
3+
use super::{CommitError, CommitsError, LintingResults};
4+
5+
pub(crate) fn print_all(results: &LintingResults) -> String {
6+
let mut output = String::new();
7+
8+
if let Some(commit_errors) = &results.commit_errors {
9+
for commit in &commit_errors.order {
10+
if let Some(errors) = commit_errors.errors.get(commit) {
11+
let short_hash = &commit.hash[..7.min(commit.hash.len())];
12+
let message = commit.message.lines().next().unwrap_or_default();
13+
14+
let _ = writeln!(output, "::group::{short_hash} - {message}");
15+
16+
for error in errors {
17+
match error {
18+
CommitError::MergeCommit => {
19+
let _ = writeln!(
20+
output,
21+
"::error title=Merge Commit::Commit {short_hash} is a merge commit."
22+
);
23+
}
24+
}
25+
}
26+
27+
let _ = writeln!(output, "::endgroup::");
28+
}
29+
}
30+
}
31+
32+
if let Some(commits_errors) = &results.commits_errors {
33+
for commits_error in &commits_errors.errors {
34+
match commits_error {
35+
CommitsError::MaxCommitsExceeded {
36+
max_commits,
37+
actual_commits,
38+
} => {
39+
let _ = writeln!(
40+
output,
41+
"::error title=Max Commits Exceeded::Maximum commits exceeded: found {actual_commits} commits, but maximum allowed is {max_commits}."
42+
);
43+
}
44+
}
45+
}
46+
}
47+
48+
output
49+
}

src/linting_results/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ use std::collections::{HashMap, VecDeque};
22

33
use crate::commits::commit::Commit;
44

5+
mod github_actions;
56
mod pretty;
67

78
/// The representation of an error that an individual commit can have.
89
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
910
pub enum CommitError {
10-
/// Commit is a merge commit (has multiple parents).
1111
MergeCommit,
1212
}
1313

1414
/// The representation of an error for the collection of commits as a whole.
1515
#[derive(Debug, Clone, PartialEq, Eq)]
1616
pub enum CommitsError {
17-
/// The number of commits exceeds the maximum allowed.
1817
MaxCommitsExceeded {
1918
max_commits: usize,
2019
actual_commits: usize,
@@ -51,8 +50,11 @@ pub struct LintingResults {
5150
}
5251

5352
impl LintingResults {
54-
/// Get a pretty representation of the linting results as a string, suitable as output for a user.
5553
pub fn pretty(&self) -> String {
5654
pretty::print_all(self)
5755
}
56+
57+
pub fn github_actions(&self) -> String {
58+
github_actions::print_all(self)
59+
}
5860
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) struct Arguments {
3333
#[arg(
3434
long,
3535
default_value = "default",
36-
help = "Specifies the format for outputting results, acceptable values are quiet, default, and pretty."
36+
help = "Specifies the format for outputting results, acceptable values are quiet, default, pretty, and github."
3737
)]
3838
pub(crate) output: Output,
3939

@@ -79,6 +79,9 @@ fn run(arguments: Arguments) -> Result<i32> {
7979
Output::Pretty => {
8080
println!("{}", linting_results.pretty());
8181
}
82+
Output::GitHub => {
83+
println!("{}", linting_results.github_actions());
84+
}
8285
}
8386

8487
// As we don't want an error printed but linting failed so want to exit unsuccessfully.

src/output.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ pub enum Output {
55
Quiet,
66
Default,
77
Pretty,
8+
#[value(name = "github")]
9+
GitHub,
810
}

0 commit comments

Comments
 (0)