grim/youtrack-import

Parents fd6245f5c226
Children 1108579b4005
Handle pr keywords better and use some heuristics to figure out when a pr was meant instead of an issue. Fixes YI-20
--- a/bitbucket/converter.go Thu Jan 16 01:16:43 2020 -0600
+++ b/bitbucket/converter.go Thu Jan 16 02:32:47 2020 -0600
@@ -35,7 +35,7 @@
}
)
-func (a *Archive) convertIssue(projectID, repository string, bb Issue, userMap map[string]*youtrack.User) *youtrack.Issue {
+func (a *Archive) convertIssue(nIssues int, projectID, repository string, bb Issue, userMap map[string]*youtrack.User) *youtrack.Issue {
reporter := "admin"
if user, found := userMap[bb.Reporter.AccountID]; found {
reporter = user.Login
@@ -49,7 +49,7 @@
yt := &youtrack.Issue{
Number: bb.ID,
Summary: bb.Title,
- Description: replaceKeywords(userMap, projectID, repository, bb.Content),
+ Description: replaceKeywords(userMap, nIssues, projectID, repository, bb.Content),
Created: bb.CreatedOn,
Updated: bb.UpdatedOn,
Reporter: reporter,
@@ -81,13 +81,13 @@
}
// add the comments and attachments
- yt.Comments = a.convertComments(projectID, repository, userMap, bb.ID)
+ yt.Comments = a.convertComments(nIssues, projectID, repository, userMap, bb.ID)
yt.Attachments = a.convertAttachments(userMap, bb)
return yt
}
-func (a *Archive) convertComments(projectID, repository string, userMap map[string]*youtrack.User, id int) []*youtrack.Comment {
+func (a *Archive) convertComments(nIssues int, projectID, repository string, userMap map[string]*youtrack.User, id int) []*youtrack.Comment {
comments := []*youtrack.Comment{}
for _, comment := range a.Comments {
@@ -124,7 +124,7 @@
ytComment := &youtrack.Comment{
Author: author,
- Text: replaceKeywords(userMap, projectID, repository, comment.Content),
+ Text: replaceKeywords(userMap, nIssues, projectID, repository, comment.Content),
Created: comment.CreatedOn,
Updated: comment.UpdatedOn,
Markdown: true,
@@ -191,9 +191,10 @@
}
// convert all of the issues
- issues := make([]*youtrack.Issue, len(a.Issues))
- for i := 0; i < len(a.Issues); i++ {
- issues[i] = a.convertIssue(projectID, repository, a.Issues[i], users)
+ nIssues := len(a.Issues)
+ issues := make([]*youtrack.Issue, nIssues)
+ for i := 0; i < nIssues; i++ {
+ issues[i] = a.convertIssue(nIssues, projectID, repository, a.Issues[i], users)
}
project := &youtrack.Project{
--- a/bitbucket/keywords.go Thu Jan 16 01:16:43 2020 -0600
+++ b/bitbucket/keywords.go Thu Jan 16 02:32:47 2020 -0600
@@ -3,6 +3,7 @@
import (
"fmt"
"regexp"
+ "strconv"
"strings"
"hg.sr.ht/~grim/youtrack-import/youtrack"
@@ -10,7 +11,7 @@
var (
mentionRegex = regexp.MustCompile(`@\{(.+?)\}`)
- prIssueRegex = regexp.MustCompile(`((PR)\s+)?#(\d+)`)
+ prIssueRegex = regexp.MustCompile(`(([Pp][Rr])\s+)?(\\)?#(\d+)`)
csetRegex = regexp.MustCompile(`→ <<cset (.+?)>>`)
)
@@ -30,20 +31,22 @@
return output
}
-func replacePullRequestsIssues(repository, projectID, message string) string {
+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[3]
+ id := match[4]
var replacement string
- if pr != "" {
+ nId, _ := strconv.Atoi(id)
+
+ if pr != "" || nId > nIssues {
replacement = fmt.Sprintf(
- "[%s](https://bitbucket.org/%s/pull-requests/%s)",
- old,
+ "[PR #%s](https://bitbucket.org/%s/pull-requests/%s)",
+ id,
repository,
id,
)
@@ -76,11 +79,11 @@
return output
}
-func replaceKeywords(m map[string]*youtrack.User, projectID, repository, message string) string {
+func replaceKeywords(m map[string]*youtrack.User, nIssues int, projectID, repository, message string) string {
output := message
output = replaceMentions(m, output)
- output = replacePullRequestsIssues(repository, projectID, output)
+ output = replacePullRequestsIssues(nIssues, repository, projectID, output)
output = replaceChangeSets(repository, output)
return output