grim/youtrack-import

Parents ff8d512f4f72
Children 1e60d209bf11
Convert the custom DebianBug and GnomeBug links to their absolute urls. Fixes YI-47
--- a/trac/tickets.go Tue Aug 11 23:04:40 2020 -0500
+++ b/trac/tickets.go Mon Aug 17 22:44:53 2020 -0500
@@ -117,8 +117,49 @@
// ticket link regex
ticketLinkRegex = regexp.MustCompile(`#(\d+)([^\w])`)
+
+ // issue tracker links
+ debianBugRegex = regexp.MustCompile(`\[DebianBug:(\d+)(?:\s([^]]+))?\]`)
+ gnomeBugRegex = regexp.MustCompile(`\[GnomeBug:(\d+)(?:\s([^]]+))?\]`)
)
+func convertLinks(text string) string {
+ replaceFunc := func(re *regexp.Regexp, urlFormat string) func(string) string {
+ return func(original string) string {
+ parts := re.FindStringSubmatch(original)
+
+ url := fmt.Sprintf(urlFormat, parts[1])
+
+ text := parts[2]
+ if text == "" {
+ return url
+ }
+
+ return fmt.Sprintf("[%s](%s)", text, url)
+ }
+ }
+
+ // replace the custom [DebianBug] links with real markdown links
+ text = debianBugRegex.ReplaceAllStringFunc(
+ text,
+ replaceFunc(
+ debianBugRegex,
+ "https://bugs.debian.org/%s",
+ ),
+ )
+
+ // replace the custom [GnomeBug] links with real markdown links
+ text = gnomeBugRegex.ReplaceAllStringFunc(
+ text,
+ replaceFunc(
+ gnomeBugRegex,
+ "https://bugzilla.gnome.org/show_bug.cgi?id=%s",
+ ),
+ )
+
+ return text
+}
+
func convertComment(projectID, comment string) string {
if strings.Contains(comment, "{{{") {
// inline code uses the same syntax so we need to do code blocks
@@ -140,6 +181,8 @@
// replace any ticket link references
comment = ticketLinkRegex.ReplaceAllString(comment, projectID+"-$1$2")
+ comment = convertLinks(comment)
+
return comment
}