Skip to content

Commit

Permalink
Merge pull request #21163 from hongkailiu/ignore_code
Browse files Browse the repository at this point in the history
jira plugin: do not replace code section
  • Loading branch information
k8s-ci-robot committed Mar 4, 2021
2 parents bd73e8f + 122151f commit a61492b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
39 changes: 37 additions & 2 deletions prow/plugins/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,47 @@ func updateComment(e *github.GenericCommentEvent, validIssues []string, jiraBase
return nil
}

type line struct {
content string
replacing bool
}

func getLines(text string) []line {
var lines []line
rawLines := strings.Split(text, "\n")
var prefixCount int
for _, rawLine := range rawLines {
if strings.HasPrefix(rawLine, "```") {
prefixCount++
}
l := line{content: rawLine, replacing: true}
if prefixCount%2 == 1 {
l.replacing = false
}
lines = append(lines, l)
}
return lines
}

func insertLinksIntoComment(body string, issueNames []string, jiraBaseURL string) string {
var linesWithLinks []string
lines := getLines(body)
for _, line := range lines {
if line.replacing {
linesWithLinks = append(linesWithLinks, insertLinksIntoLine(line.content, issueNames, jiraBaseURL))
continue
}
linesWithLinks = append(linesWithLinks, line.content)
}
return strings.Join(linesWithLinks, "\n")
}

func insertLinksIntoLine(line string, issueNames []string, jiraBaseURL string) string {
for _, issue := range issueNames {
replacement := fmt.Sprintf("[%s](%s/browse/%s)", issue, jiraBaseURL, issue)
body = replaceStringIfHasntSquareBracketOrSlashPrefix(body, issue, replacement)
line = replaceStringIfHasntSquareBracketOrSlashPrefix(line, issue, replacement)
}
return body
return line
}

// replaceStringIfHasntSquareBracketOrSlashPrefix replaces a string if it is not prefixed by
Expand Down
13 changes: 13 additions & 0 deletions prow/plugins/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ because of [ABC-123](https://my-jira.com/browse/ABC-123)`,
body: "https://my-jira.com/browse/ABC-123",
expected: "https://my-jira.com/browse/ABC-123",
},
{
name: "code section is not replaced",
body: `This change:
is very important` + "\n```bash\n" +
`ABC-123` +
"\n```\n" + `ABC-123
`,
expected: `This change:
is very important` + "\n```bash\n" +
`ABC-123` +
"\n```\n" + `[ABC-123](https://my-jira.com/browse/ABC-123)
`,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit a61492b

Please sign in to comment.