Skip to content

Commit 245e5be

Browse files
authored
[issuegenerator] Trim module name when creating a new issue (#864)
* [issuegenerator] Trim module name when creating a new issue * add changelog * Use central method for trimming prefix, add tests * Fix lint failure * Remove unnecessary prefix check * Changes requested in code review - expected -> want - Only use module name in test, don't include https:// prefix - Whitespace - require -> assert - Print all input args for failing tests
1 parent f036375 commit 245e5be

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

.chloggen/trim_issue_title.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
5+
component: issuegenerator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Trim repository prefix from new issue titles
9+
10+
# One or more tracking issues related to the change
11+
issues: [864]
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.
16+
subtext:

issuegenerator/main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func newReportGenerator() *reportGenerator {
9393
}
9494
}
9595

96+
func trimModule(owner, repo, module string) string {
97+
return strings.TrimPrefix(module, fmt.Sprintf("github.com/%s/%s/", owner, repo))
98+
}
99+
96100
func main() {
97101
pathToArtifacts := flag.String("path", "", "Path to the directory with test results")
98102
flag.Parse()
@@ -255,7 +259,7 @@ func (rg *reportGenerator) getExistingIssue(module string) *github.Issue {
255259
rg.handleBadResponses(response)
256260
}
257261

258-
module = strings.TrimPrefix(module, fmt.Sprintf("github.com/%s/%s/", rg.envVariables[githubOwner], rg.envVariables[githubRepository]))
262+
module = trimModule(rg.envVariables[githubOwner], rg.envVariables[githubRepository], module)
259263
requiredTitle := strings.Replace(issueTitleTemplate, "${module}", module, 1)
260264
for _, issue := range issues {
261265
if *issue.Title == requiredTitle {
@@ -294,7 +298,8 @@ func (rg *reportGenerator) commentOnIssue(issue *github.Issue) *github.IssueComm
294298

295299
// createIssue creates a new GitHub Issue corresponding to a build failure.
296300
func (rg *reportGenerator) createIssue(r report) *github.Issue {
297-
title := strings.Replace(issueTitleTemplate, "${module}", r.module, 1)
301+
trimmedModule := trimModule(rg.envVariables[githubOwner], rg.envVariables[githubRepository], r.module)
302+
title := strings.Replace(issueTitleTemplate, "${module}", trimmedModule, 1)
298303
body := os.Expand(issueBodyTemplate, rg.templateHelper)
299304

300305
issue, response, err := rg.client.Issues.Create(

issuegenerator/main_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/joshdk/go-junit"
21+
"github.com/stretchr/testify/assert"
2122
"github.com/stretchr/testify/require"
2223
)
2324

@@ -108,3 +109,34 @@ func TestIngestArtifacts(t *testing.T) {
108109
require.Equal(t, expectedTestResults, rg.testSuites)
109110

110111
}
112+
113+
func TestTrimPath(t *testing.T) {
114+
tests := []struct {
115+
name string
116+
owner string
117+
repo string
118+
module string
119+
wantModule string
120+
}{
121+
{
122+
name: "Test contrib host metrics integration path",
123+
owner: "open-telemetry",
124+
repo: "opentelemetry-collector-contrib",
125+
module: "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/integration_test.go",
126+
wantModule: "receiver/hostmetricsreceiver/integration_test.go",
127+
},
128+
{
129+
name: "Test core otlphttp exporter test path",
130+
owner: "open-telemetry",
131+
repo: "opentelemetry-collector",
132+
module: "github.com/open-telemetry/opentelemetry-collector/exporter/otlphttpexporter/otlp_test.go",
133+
wantModule: "exporter/otlphttpexporter/otlp_test.go",
134+
},
135+
}
136+
137+
for _, tt := range tests {
138+
t.Run(tt.name, func(t *testing.T) {
139+
assert.Equal(t, tt.wantModule, trimModule(tt.owner, tt.repo, tt.module), "owner: %s, repo: %s, module: %s, wantModule: %s", tt.owner, tt.repo, tt.module, tt.wantModule)
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)