Skip to content

Commit cda14c2

Browse files
committed
Move MakeJiraIssueKeyPattern to jira package
- confirm will only extract correct keys in gitView_test
1 parent e9660d2 commit cda14c2

File tree

5 files changed

+126
-25
lines changed

5 files changed

+126
-25
lines changed

cmd/kosli/attestJira.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (o *attestJiraOptions) run(args []string) error {
250250
if err != nil {
251251
return err
252252
}
253-
jiraIssueKeyPattern := makeJiraIssueKeyPattern(o.projectKeys)
253+
jiraIssueKeyPattern := jira.MakeJiraIssueKeyPattern(o.projectKeys)
254254

255255
issueIDs, commitInfo, err := gv.MatchPatternInCommitMessageORBranchName(jiraIssueKeyPattern, o.payload.Commit.Sha1,
256256
o.secondarySource, o.ignoreBranchMatch)
@@ -306,14 +306,3 @@ func (o *attestJiraOptions) run(args []string) error {
306306
}
307307
return wrapAttestationError(err)
308308
}
309-
310-
func makeJiraIssueKeyPattern(projectKeys []string) string {
311-
// Jira issue keys consist of [project-key]-[sequential-number]
312-
// project key must be at least 2 characters long and start with an uppercase letter
313-
// more info: https://support.atlassian.com/jira-software-cloud/docs/what-is-an-issue/#Workingwithissues-Projectandissuekeys
314-
if len(projectKeys) == 0 {
315-
return `[A-Z][A-Z0-9]{1,9}-[0-9]+`
316-
} else {
317-
return `(` + strings.Join(projectKeys, "|") + `)-[0-9]+`
318-
}
319-
}

cmd/kosli/attestJira_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/kosli-dev/cli/internal/jira"
56
"os"
67
"regexp"
78
"testing"
@@ -316,7 +317,7 @@ func TestMakeJiraIssueKey(t *testing.T) {
316317

317318
for _, tt := range tests {
318319
t.Run(tt.name, func(t *testing.T) {
319-
got := makeJiraIssueKeyPattern(tt.projectKeys)
320+
got := jira.MakeJiraIssueKeyPattern(tt.projectKeys)
320321
if got != tt.want {
321322
t.Errorf("makeJiraIssueKeyPattern() = %v, want %v", got, tt.want)
322323
}

internal/gitview/gitView_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitview
22

33
import (
44
"fmt"
5+
"github.com/kosli-dev/cli/internal/jira"
56
"os"
67
"path/filepath"
78
"testing"
@@ -360,6 +361,7 @@ func (suite *GitViewTestSuite) TestMatchPatternInCommitMessageORBranchName() {
360361
_, workTree, fs, err := testHelpers.InitializeGitRepo(suite.tmpDir)
361362
require.NoError(suite.Suite.T(), err)
362363

364+
defaultJiraPattern := "[A-Z][A-Z0-9]{1,9}-[0-9]+"
363365
for _, t := range []struct {
364366
name string
365367
pattern string
@@ -373,36 +375,36 @@ func (suite *GitViewTestSuite) TestMatchPatternInCommitMessageORBranchName() {
373375
}{
374376
{
375377
name: "One Jira reference found",
376-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
378+
pattern: jira.MakeJiraIssueKeyPattern([]string{}),
377379
commitMessage: "EX-1 test commit",
378380
want: []string{"EX-1"},
379381
wantError: false,
380382
},
381383
{
382384
name: "Two Jira references found",
383-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
385+
pattern: defaultJiraPattern,
384386
commitMessage: "EX-1 ABC-22 test commit",
385387
want: []string{"EX-1", "ABC-22"},
386388
wantError: false,
387389
},
388390
{
389391
name: "No Jira references found",
390-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
392+
pattern: defaultJiraPattern,
391393
commitMessage: "test commit",
392394
want: []string{},
393395
wantError: false,
394396
},
395397
{
396398
name: "Jira references found in branch name",
397-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
399+
pattern: defaultJiraPattern,
398400
commitMessage: "some test commit",
399401
branchName: "EX-5-cool-branch",
400402
want: []string{"EX-5"},
401403
wantError: false,
402404
},
403405
{
404406
name: "Jira references found in branch name but ignoreBranchMatch set",
405-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
407+
pattern: defaultJiraPattern,
406408
commitMessage: "some test commit",
407409
branchName: "EX-6-cool-branch",
408410
ignoreBranchMatch: true,
@@ -411,39 +413,39 @@ func (suite *GitViewTestSuite) TestMatchPatternInCommitMessageORBranchName() {
411413
},
412414
{
413415
name: "Jira references found in secondary source",
414-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
416+
pattern: defaultJiraPattern,
415417
commitMessage: "some test commit",
416418
secondarySource: "EX-1-test-commit",
417419
want: []string{"EX-1"},
418420
wantError: false,
419421
},
420422
{
421423
name: "Jira references found in commit and secondary source",
422-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
424+
pattern: defaultJiraPattern,
423425
commitMessage: "EX-1 some test commit",
424426
secondarySource: "EX-2-test-commit",
425427
want: []string{"EX-1", "EX-2"},
426428
wantError: false,
427429
},
428430
{
429431
name: "Jira references found in commit and branch name",
430-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
432+
pattern: defaultJiraPattern,
431433
commitMessage: "EX-1 some test commit",
432434
branchName: "EX-2-test-commit",
433435
want: []string{"EX-1", "EX-2"},
434436
wantError: false,
435437
},
436438
{
437439
name: "Same Jira references found in commit and branch name is not duplicated",
438-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
440+
pattern: defaultJiraPattern,
439441
commitMessage: "DUP-1 some test commit",
440442
branchName: "DUP-1-test-commit",
441443
want: []string{"DUP-1"},
442444
wantError: false,
443445
},
444446
{
445447
name: "Jira references found in commit, branch name and secondary source",
446-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
448+
pattern: defaultJiraPattern,
447449
commitMessage: "ALL-1 some test commit",
448450
branchName: "ALL-2-test-commit",
449451
secondarySource: "ALL-3-some-things",
@@ -452,14 +454,28 @@ func (suite *GitViewTestSuite) TestMatchPatternInCommitMessageORBranchName() {
452454
},
453455
{
454456
name: "No Jira references found, despite something that looks similar to Jira reference",
455-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
457+
pattern: defaultJiraPattern,
456458
commitMessage: "Ea-1 test commit",
457459
want: []string{},
458460
wantError: false,
459461
},
462+
{
463+
name: "One Jira reference found with specific pattern",
464+
pattern: jira.MakeJiraIssueKeyPattern([]string{"EX"}),
465+
commitMessage: "EX-1 ABC-22 test commit",
466+
want: []string{"EX-1"},
467+
wantError: false,
468+
},
469+
{
470+
name: "Two Jira references found with specific pattern",
471+
pattern: jira.MakeJiraIssueKeyPattern([]string{"EX", "ABC"}),
472+
commitMessage: "EX-1 ABC-22 test commit",
473+
want: []string{"EX-1", "ABC-22"},
474+
wantError: false,
475+
},
460476
{
461477
name: "Commit not found, expect an error",
462-
pattern: "[A-Z][A-Z0-9]{1,9}-[0-9]+",
478+
pattern: defaultJiraPattern,
463479
commitSha: "3b7420d0392114794591aaefcd84d7b100b8d095",
464480
wantError: true,
465481
},

internal/jira/jira.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jira
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
jira "github.com/andygrunwald/go-jira"
89
)
@@ -96,3 +97,14 @@ func (jc *JiraConfig) GetJiraIssueInfo(issueID string, issueFields string) (*Jir
9697
}
9798
return result, nil
9899
}
100+
101+
func MakeJiraIssueKeyPattern(projectKeys []string) string {
102+
// Jira issue keys consist of [project-key]-[sequential-number]
103+
// project key must be at least 2 characters long and start with an uppercase letter
104+
// more info: https://support.atlassian.com/jira-software-cloud/docs/what-is-an-issue/#Workingwithissues-Projectandissuekeys
105+
if len(projectKeys) == 0 {
106+
return `[A-Z][A-Z0-9]{1,9}-[0-9]+`
107+
} else {
108+
return `(` + strings.Join(projectKeys, "|") + `)-[0-9]+`
109+
}
110+
}

internal/jira/jira_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package jira
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
)
7+
8+
func TestMakeJiraIssueKey(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
projectKeys []string
12+
want string
13+
matches []string // strings that should match the pattern
14+
nonMatches []string // strings that should not match the pattern
15+
}{
16+
{
17+
name: "Empty project keys",
18+
projectKeys: []string{},
19+
want: `[A-Z][A-Z0-9]{1,9}-[0-9]+`,
20+
matches: []string{
21+
"ABC-123",
22+
"A1-456",
23+
"XY-789",
24+
},
25+
nonMatches: []string{
26+
"abc-123", // project key should start with uppercase
27+
"A-123", // project key too short
28+
"1A-123", // project key starts with a number
29+
"ABC_123", // wrong separator
30+
"ABC-", // missing number
31+
"-123", // missing project key
32+
},
33+
},
34+
{
35+
name: "With project keys",
36+
projectKeys: []string{"ABC", "XYZ"},
37+
want: `(ABC|XYZ)-[0-9]+`, // Currently empty in the function implementation
38+
matches: []string{
39+
"ABC-123",
40+
"XYZ-789",
41+
},
42+
nonMatches: []string{
43+
"xyz-123", // project key should start with uppercase
44+
"ABC_123", // wrong separator
45+
"ABC-", // missing number
46+
"-123", // missing project key
47+
"DEF-123", // wrong project key
48+
},
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
got := MakeJiraIssueKeyPattern(tt.projectKeys)
55+
if got != tt.want {
56+
t.Errorf("makeJiraIssueKeyPattern() = %v, want %v", got, tt.want)
57+
}
58+
59+
// Only test pattern matching if a pattern is returned
60+
if got != "" {
61+
re, err := regexp.Compile(got)
62+
if err != nil {
63+
t.Errorf("Invalid regex pattern returned: %v", err)
64+
return
65+
}
66+
67+
// Test matches
68+
for _, s := range tt.matches {
69+
if !re.MatchString(s) {
70+
t.Errorf("Pattern %q should match %q but doesn't", got, s)
71+
}
72+
}
73+
74+
// Test non-matches
75+
for _, s := range tt.nonMatches {
76+
if re.MatchString(s) {
77+
t.Errorf("Pattern %q should NOT match %q but does", got, s)
78+
}
79+
}
80+
}
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)