grim/youtrack-import

cdadae07e61c
Convert bold, italic, and bold italic to the markdown version
package bitbucket
import (
"fmt"
"regexp"
"strconv"
"strings"
"keep.imfreedom.org/grim/youtrack-import/youtrack"
)
var (
mentionRegex = regexp.MustCompile(`@\{(.+?)\}`)
prIssueRegex = regexp.MustCompile(`(([Pp][Rr])\s+)?(\\)?#(\d+)`)
csetRegex = regexp.MustCompile(`→ <<cset (.+?)>>`)
hashRegex = regexp.MustCompile(`\b([a-fA-F0-9]{40})\b`)
)
func replaceMentions(m map[string]*youtrack.User, message string) string {
output := message
matches := mentionRegex.FindAllStringSubmatch(message, -1)
for _, match := range matches {
old := match[0]
id := match[1]
if user, found := m[id]; found {
output = strings.Replace(output, old, "@"+user.Login, 1)
}
}
return output
}
func replacePullRequestsIssues(nIssues int, repository, projectID, message string) string {
output := message
matches := prIssueRegex.FindAllStringSubmatch(message, -1)
for _, match := range matches {
old := match[0]
pr := match[2]
id := match[4]
var replacement string
nId, _ := strconv.Atoi(id)
if pr != "" || nId > nIssues {
replacement = fmt.Sprintf(
"[PR #%s](https://bitbucket.org/%s/pull-requests/%s)",
id,
repository,
id,
)
} else {
replacement = projectID + "-" + id
}
output = strings.Replace(output, old, replacement, 1)
}
return output
}
func replaceChangeSets(repository, message string) string {
output := message
matches := csetRegex.FindAllStringSubmatch(message, -1)
for _, match := range matches {
old := match[0]
id := match[1]
replacement := fmt.Sprintf(
"* **commit**: %s@%s\n",
repository,
id,
)
output = strings.Replace(output, old, replacement, 1)
}
return output
}
func replaceCommitHashes(repository, message string) string {
output := message
matches := hashRegex.FindAllStringSubmatch(message, -1)
for _, match := range matches {
old := match[0]
hash := match[1]
replacement := fmt.Sprintf(
"%s@%s",
repository,
hash,
)
output = strings.Replace(output, old, replacement, 1)
}
return output
}
func replaceKeywords(m map[string]*youtrack.User, nIssues int, projectID, repository, message string) string {
output := message
output = replaceMentions(m, output)
output = replacePullRequestsIssues(nIssues, repository, projectID, output)
output = replaceChangeSets(repository, output)
output = replaceCommitHashes(repository, output)
return output
}