@@ -24,7 +24,6 @@ import (
2424 "os"
2525 "path"
2626 "strings"
27- "time"
2827
2928 "github.com/google/go-github/github"
3029 "github.com/joshdk/go-junit"
@@ -45,6 +44,8 @@ const (
4544 githubServerURL = "GITHUB_SERVER_URL"
4645 githubRunID = "GITHUB_RUN_ID"
4746
47+ autoGeneratedLabel = "generated-by-issuegenerator"
48+
4849 issueTitleTemplate = `[${module}]: Report for failed tests on main`
4950 issueBodyTemplate = `
5051Auto-generated report for ${jobName} job build.
@@ -74,6 +75,7 @@ type reportGenerator struct {
7475
7576 reports []report
7677 reportIterator int
78+ issueLabels []string
7779}
7880
7981type report struct {
@@ -89,9 +91,10 @@ func newReportGenerator() *reportGenerator {
8991 }
9092
9193 return & reportGenerator {
92- logger : logger ,
93- testSuites : make (map [string ]junit.Suite ),
94- reports : make ([]report , 0 ),
94+ logger : logger ,
95+ testSuites : make (map [string ]junit.Suite ),
96+ reports : make ([]report , 0 ),
97+ issueLabels : []string {autoGeneratedLabel },
9598 }
9699}
97100
@@ -109,6 +112,7 @@ func getComponent(module string) string {
109112
110113func main () {
111114 pathToArtifacts := flag .String ("path" , "" , "Path to the directory with test results" )
115+ labelFlag := flag .String ("labels" , "" , "Comma-separated list of labels to add to the created issues" )
112116 flag .Parse ()
113117 if * pathToArtifacts == "" {
114118 fmt .Println ("Path to the directory with test results is required" )
@@ -121,7 +125,13 @@ func main() {
121125 rg .ingestArtifacts (* pathToArtifacts )
122126 rg .processTestResults ()
123127 rg .initializeGHClient (ctx )
124-
128+ if * labelFlag != "" {
129+ labels := strings .Split (* labelFlag , "," )
130+ for i := range labels {
131+ labels [i ] = strings .TrimSpace (labels [i ])
132+ }
133+ rg .issueLabels = append (rg .issueLabels , labels ... )
134+ }
125135 // Look for existing open GitHub Issue that resulted from previous
126136 // failures of this job.
127137 rg .logger .Info ("Searching GitHub for existing Issues" )
@@ -262,13 +272,15 @@ func (rg *reportGenerator) templateHelper(param string) string {
262272// getExistingIssues gathers an existing GitHub Issue related to previous failures
263273// of the same module.
264274func (rg * reportGenerator ) getExistingIssue (ctx context.Context , module string ) * github.Issue {
275+
276+ componentName := getComponent (trimModule (rg .envVariables [githubOwner ], rg .envVariables [githubRepository ], module ))
265277 issues , response , err := rg .client .Issues .ListByRepo (
266278 ctx ,
267279 rg .envVariables [githubOwner ],
268280 rg .envVariables [githubRepository ],
269281 & github.IssueListByRepoOptions {
270- State : "open" ,
271- Since : time . Now (). Add ( - time . Hour * 24 * 30 * 6 ), // Search for at least 6 months of issues
282+ State : "open" ,
283+ Labels : [] string { autoGeneratedLabel , componentName },
272284 },
273285 )
274286 if err != nil {
@@ -279,14 +291,21 @@ func (rg *reportGenerator) getExistingIssue(ctx context.Context, module string)
279291 rg .handleBadResponses (response )
280292 }
281293
282- module = trimModule (rg .envVariables [githubOwner ], rg .envVariables [githubRepository ], module )
283- requiredTitle := strings .Replace (issueTitleTemplate , "${module}" , module , 1 )
284- for _ , issue := range issues {
285- if * issue .Title == requiredTitle {
286- return issue
294+ if len (issues ) > 0 {
295+ if len (issues ) > 1 {
296+ issueLinks := make ([]string , len (issues ))
297+ for i , issue := range issues {
298+ if issue .HTMLURL != nil {
299+ issueLinks [i ] = * issue .HTMLURL
300+ }
301+ rg .logger .Warn (
302+ "Multiple existing Issues found for the same component" ,
303+ zap .Strings ("issue_links" , issueLinks ),
304+ )
305+ }
287306 }
307+ return issues [0 ]
288308 }
289-
290309 return nil
291310}
292311
@@ -321,15 +340,18 @@ func (rg *reportGenerator) createIssue(ctx context.Context, r report) *github.Is
321340 trimmedModule := trimModule (rg .envVariables [githubOwner ], rg .envVariables [githubRepository ], r .module )
322341 title := strings .Replace (issueTitleTemplate , "${module}" , trimmedModule , 1 )
323342 body := os .Expand (issueBodyTemplate , rg .templateHelper )
343+ componentName := getComponent (trimmedModule )
344+
345+ rg .issueLabels = append (rg .issueLabels , componentName )
324346
325347 issue , response , err := rg .client .Issues .Create (
326348 ctx ,
327349 rg .envVariables [githubOwner ],
328350 rg .envVariables [githubRepository ],
329351 & github.IssueRequest {
330- Title : & title ,
331- Body : & body ,
332- // TODO: Set Assignees and labels
352+ Title : & title ,
353+ Body : & body ,
354+ Labels : & rg . issueLabels ,
333355 })
334356 if err != nil {
335357 rg .logger .Fatal ("Failed to create GitHub Issue" , zap .Error (err ))
0 commit comments