grim/youtrack-import

Parents 4865c397f3ac
Children bfb598e00f0d
Add an argument for the repository so we can properly link pull requests. Fixes YI-11
--- a/bitbucket/cmd.go Wed Jan 15 06:35:33 2020 -0600
+++ b/bitbucket/cmd.go Wed Jan 15 06:53:17 2020 -0600
@@ -7,6 +7,7 @@
type Cmd struct {
Archive string `kong:"arg,name='archive',help='The zip file containing the archive'"`
+ Repository string `kong:"arg,name='repository',help='The repostiory name on bitbucket.'"`
UsersMapFile string `kong:"arg,name="users-map",help='A key=value file mapping emails to display names'"`
}
@@ -22,7 +23,7 @@
return err
}
- project, err := archive.convert(g.ProjectID, usersMap)
+ project, err := archive.convert(g.ProjectID, c.Repository, usersMap)
if err != nil {
return err
}
--- a/bitbucket/converter.go Wed Jan 15 06:35:33 2020 -0600
+++ b/bitbucket/converter.go Wed Jan 15 06:53:17 2020 -0600
@@ -34,7 +34,7 @@
}
)
-func (a *Archive) convertIssue(projectID string, bb Issue, userMap map[string]*youtrack.User) *youtrack.Issue {
+func (a *Archive) convertIssue(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
@@ -48,7 +48,7 @@
yt := &youtrack.Issue{
Number: bb.ID,
Summary: bb.Title,
- Description: replaceKeywords(userMap, projectID, bb.Content),
+ Description: replaceKeywords(userMap, projectID, repository, bb.Content),
Created: bb.CreatedOn,
Updated: bb.UpdatedOn,
Reporter: reporter,
@@ -80,13 +80,13 @@
}
// add the comments and attachments
- yt.Comments = a.convertComments(projectID, userMap, bb.ID)
+ yt.Comments = a.convertComments(projectID, repository, userMap, bb.ID)
yt.Attachments = a.convertAttachments(userMap, bb)
return yt
}
-func (a *Archive) convertComments(projectID string, userMap map[string]*youtrack.User, id int) []*youtrack.Comment {
+func (a *Archive) convertComments(projectID, repository string, userMap map[string]*youtrack.User, id int) []*youtrack.Comment {
comments := []*youtrack.Comment{}
for _, comment := range a.Comments {
@@ -134,7 +134,7 @@
ytComment = &youtrack.Comment{
Author: author,
- Text: replaceKeywords(userMap, projectID, comment.Content),
+ Text: replaceKeywords(userMap, projectID, repository, comment.Content),
Created: comment.CreatedOn,
Updated: comment.UpdatedOn,
Markdown: true,
@@ -175,7 +175,7 @@
return attachments
}
-func (a *Archive) convert(projectID string, usersMap *UsersMap) (*youtrack.Project, error) {
+func (a *Archive) convert(projectID, repository string, usersMap *UsersMap) (*youtrack.Project, error) {
users, err := a.ExtractUsers(usersMap)
if err != nil {
return nil, err
@@ -204,7 +204,7 @@
// 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, a.Issues[i], users)
+ issues[i] = a.convertIssue(projectID, repository, a.Issues[i], users)
}
project := &youtrack.Project{
--- a/bitbucket/keywords.go Wed Jan 15 06:35:33 2020 -0600
+++ b/bitbucket/keywords.go Wed Jan 15 06:53:17 2020 -0600
@@ -1,6 +1,7 @@
package bitbucket
import (
+ "fmt"
"regexp"
"strings"
@@ -9,7 +10,7 @@
var (
mentionRegex = regexp.MustCompile(`@\{(.+?)\}`)
- issueRegex = regexp.MustCompile(`#(\d+)`)
+ prIssueRegex = regexp.MustCompile(`((PR)\s+)?#(\d+)`)
)
func replaceMentions(m map[string]*youtrack.User, message string) string {
@@ -28,25 +29,38 @@
return output
}
-func replaceIssues(projectID, message string) string {
+func replacePullRequestsIssues(repository, projectID, message string) string {
output := message
-
- matches := issueRegex.FindAllStringSubmatch(message, -1)
+ matches := prIssueRegex.FindAllStringSubmatch(message, -1)
for _, match := range matches {
old := match[0]
- id := match[1]
+ pr := match[2]
+ id := match[3]
+
+ var replacement string
- output = strings.Replace(output, old, projectID+"-"+id, 1)
+ if pr != "" {
+ replacement = fmt.Sprintf(
+ "[%s](https://bitbucket.org/%s/pull-requests/%s)",
+ old,
+ repository,
+ id,
+ )
+ } else {
+ replacement = projectID + "-" + id
+ }
+
+ output = strings.Replace(output, old, replacement, 1)
}
return output
}
-func replaceKeywords(m map[string]*youtrack.User, projectID string, message string) string {
+func replaceKeywords(m map[string]*youtrack.User, projectID, repository, message string) string {
output := message
output = replaceMentions(m, output)
- output = replaceIssues(projectID, output)
+ output = replacePullRequestsIssues(repository, projectID, output)
return output
}
--- a/build-and-run Wed Jan 15 06:35:33 2020 -0600
+++ b/build-and-run Wed Jan 15 06:53:17 2020 -0600
@@ -4,5 +4,5 @@
echo -n "deleting project \"${1}\" ... "
curl -X DELETE -H "Authorization: Bearer ${YOUTRACK_TOKEN}" "${YOUTRACK_URL}rest/admin/project/${1}"
echo "done."
-./youtrack-import --project-id="${1}" --project-name="${2}" bitbucket "${3}" "${4}"
+./youtrack-import --project-id="${1}" --project-name="${2}" bitbucket "${3}" "${4}" "${5}"