Skip to content

Commit f46599b

Browse files
committed
feat(issuegenerator): filter job reports and expand templates
Refine failure detection to exclude timed-out jobs and support multiple failed job links in reports. Added tests for multi-job expansion and conclusion filtering.
1 parent 62c1da9 commit f46599b

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
21
change_type: enhancement
3-
4-
# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
52
component: issuegenerator
6-
7-
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
83
note: "Link to all failing jobs in GitHub Actions instead of general workflow run."
9-
10-
# One or more tracking issues related to the change
114
issues: [1183]
12-
13-
# (Optional) One or more lines of additional information to render under the primary note.
14-
# These lines will be padded with 2 spaces and then inserted directly into the document.
15-
# Use pipe (|) for multiline entries.
165
subtext:

issuegenerator/internal/github/client.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ func (c *Client) GetExistingIssue(ctx context.Context, module string) *github.Is
211211
// information about the latest failure. This method is expected to be
212212
// called only if there's an existing open Issue for the current job.
213213
func (c *Client) CommentOnIssue(ctx context.Context, r report.Report, issue *github.Issue) *github.IssueComment {
214-
// Get commit message and extract PR number
215214
commitMessage := c.getCommitMessage(ctx)
216215
prNumber := c.extractPRNumberFromCommitMessage(commitMessage)
217216

@@ -234,7 +233,6 @@ func (c *Client) CommentOnIssue(ctx context.Context, r report.Report, issue *git
234233
c.handleBadResponses(response)
235234
}
236235

237-
// Also comment on the PR with a link to this comment
238236
if prNumber > 0 && issueComment != nil && issueComment.HTMLURL != nil {
239237
if prAuthor := c.GetPRAuthor(ctx, prNumber); prAuthor != "" {
240238
_ = c.CommentOnPR(ctx, prNumber, prAuthor, *issueComment.HTMLURL)
@@ -472,10 +470,9 @@ func (c *Client) CommentOnPR(ctx context.Context, prNumber int, prAuthor string,
472470
}
473471

474472
func (c *Client) extractPRNumberFromCommitMessage(commitMsg string) int {
475-
// Only consider the first line of the commit message.
476473
firstLine := strings.SplitN(commitMsg, "\n", 2)[0]
477474

478-
// cases matched :
475+
// Cases matched:
479476
// - (#123)
480477
// - Merge pull request #123
481478
// - (#123): some description
@@ -506,7 +503,6 @@ func (c *Client) CreateIssue(ctx context.Context, r report.Report) *github.Issue
506503
trimmedModule := trimModule(c.envVariables[githubOwner], c.envVariables[githubRepository], r.Module)
507504
title := strings.Replace(issueTitleTemplate, "${module}", trimmedModule, 1)
508505

509-
// Get commit message and extract PR number
510506
commitMessage := c.getCommitMessage(ctx)
511507
prNumber := c.extractPRNumberFromCommitMessage(commitMessage)
512508

@@ -533,7 +529,6 @@ func (c *Client) CreateIssue(ctx context.Context, r report.Report) *github.Issue
533529
c.handleBadResponses(response)
534530
}
535531

536-
// After creating the issue, also comment on the PR with a link to the created issue
537532
if prNumber > 0 && issue != nil && issue.HTMLURL != nil {
538533
if prAuthor := c.GetPRAuthor(ctx, prNumber); prAuthor != "" {
539534
_ = c.CommentOnPR(ctx, prNumber, prAuthor, *issue.HTMLURL)

issuegenerator/internal/github/client_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ func TestGetFailedJobURLs(t *testing.T) {
438438
]}`,
439439
expected: map[string]string{},
440440
},
441+
{
442+
name: "timed_out jobs are excluded",
443+
runID: 123,
444+
mockResponse: `{"jobs": [
445+
{"id": 1, "name": "Failed Job", "conclusion": "failure", "html_url": "http://job/1"},
446+
{"id": 2, "name": "Timed out Job", "conclusion": "timed_out", "html_url": "http://job/2"}
447+
]}`,
448+
expected: map[string]string{"Failed Job": "http://job/1"},
449+
},
441450
}
442451

443452
for _, tt := range tests {
@@ -458,7 +467,6 @@ func TestGetFailedJobURLs(t *testing.T) {
458467
},
459468
}
460469

461-
// Override client base URL to point to mock server
462470
c, _ := github.NewClient(nil).WithEnterpriseURLs(server.URL, server.URL)
463471
client.client = c
464472

@@ -472,3 +480,45 @@ func TestGetFailedJobURLs(t *testing.T) {
472480
})
473481
}
474482
}
483+
484+
func TestMultiJobTemplateExpansion(t *testing.T) {
485+
envVariables := map[string]string{
486+
githubWorkflow: "test-ci",
487+
githubServerURL: "https://github.com",
488+
githubOwner: "test-org",
489+
githubRepository: "test-repo",
490+
githubRunID: "555555",
491+
githubSHAKey: "abcde12345",
492+
}
493+
494+
report := report.Report{
495+
Module: "test-module",
496+
}
497+
498+
t.Run("single failed job link", func(t *testing.T) {
499+
mockResponse := &github.Jobs{Jobs: []*github.WorkflowJob{
500+
{Name: github.Ptr("Job A"), Conclusion: github.Ptr("failure"), HTMLURL: github.Ptr("http://job/a")},
501+
}}
502+
mockedHTTPClient := newMockHTTPClient(t, mock.GetReposActionsRunsJobsByOwnerByRepoByRunId, mockResponse, 0)
503+
client := newTestClient(t, mockedHTTPClient)
504+
client.envVariables = envVariables
505+
506+
expand := templateHelper(context.Background(), client, envVariables, report, 0)
507+
result := expand("linkToBuild")
508+
509+
assert.Equal(t, "http://job/a", result)
510+
})
511+
512+
t.Run("multiple failed jobs list", func(t *testing.T) {
513+
mockResponse := &github.Jobs{Jobs: []*github.WorkflowJob{
514+
{Name: github.Ptr("Job A"), Conclusion: github.Ptr("failure"), HTMLURL: github.Ptr("http://job/a")},
515+
{Name: github.Ptr("Job B"), Conclusion: github.Ptr("failure"), HTMLURL: github.Ptr("http://job/b")},
516+
}}
517+
mockedHTTPClient := newMockHTTPClient(t, mock.GetReposActionsRunsJobsByOwnerByRepoByRunId, mockResponse, 0)
518+
client := newTestClient(t, mockedHTTPClient)
519+
client.envVariables = envVariables
520+
521+
expand := templateHelper(context.Background(), client, envVariables, report, 0)
522+
assert.Equal(t, "\n- [`Job A`](http://job/a)\n- [`Job B`](http://job/b)", expand("linkToBuild"))
523+
})
524+
}

0 commit comments

Comments
 (0)