import_notes.go

  1package gitlab
  2
  3import (
  4	"strings"
  5
  6	"github.com/xanzy/go-gitlab"
  7)
  8
  9type NoteType int
 10
 11const (
 12	_ NoteType = iota
 13	NOTE_COMMENT
 14	NOTE_TITLE_CHANGED
 15	NOTE_DESCRIPTION_CHANGED
 16	NOTE_CLOSED
 17	NOTE_REOPENED
 18	NOTE_LOCKED
 19	NOTE_UNLOCKED
 20	NOTE_CHANGED_DUEDATE
 21	NOTE_REMOVED_DUEDATE
 22	NOTE_ASSIGNED
 23	NOTE_UNASSIGNED
 24	NOTE_CHANGED_MILESTONE
 25	NOTE_REMOVED_MILESTONE
 26	NOTE_MENTIONED_IN_ISSUE
 27	NOTE_MENTIONED_IN_MERGE_REQUEST
 28	NOTE_UNKNOWN
 29)
 30
 31// GetNoteType parse a note system and body and return the note type and it content
 32func GetNoteType(n *gitlab.Note) (NoteType, string) {
 33	// when a note is a comment system is set to false
 34	// when a note is a different event system is set to true
 35	// because Gitlab
 36	if !n.System {
 37		return NOTE_COMMENT, n.Body
 38	}
 39
 40	if n.Body == "closed" {
 41		return NOTE_CLOSED, ""
 42	}
 43
 44	if n.Body == "reopened" {
 45		return NOTE_REOPENED, ""
 46	}
 47
 48	if n.Body == "changed the description" {
 49		return NOTE_DESCRIPTION_CHANGED, ""
 50	}
 51
 52	if n.Body == "locked this issue" {
 53		return NOTE_LOCKED, ""
 54	}
 55
 56	if n.Body == "unlocked this issue" {
 57		return NOTE_UNLOCKED, ""
 58	}
 59
 60	if strings.HasPrefix(n.Body, "changed title from") {
 61		return NOTE_TITLE_CHANGED, getNewTitle(n.Body)
 62	}
 63
 64	if strings.HasPrefix(n.Body, "changed due date to") {
 65		return NOTE_CHANGED_DUEDATE, ""
 66	}
 67
 68	if n.Body == "removed due date" {
 69		return NOTE_REMOVED_DUEDATE, ""
 70	}
 71
 72	if strings.HasPrefix(n.Body, "assigned to @") {
 73		return NOTE_ASSIGNED, ""
 74	}
 75
 76	if strings.HasPrefix(n.Body, "unassigned @") {
 77		return NOTE_UNASSIGNED, ""
 78	}
 79
 80	if strings.HasPrefix(n.Body, "changed milestone to %") {
 81		return NOTE_CHANGED_MILESTONE, ""
 82	}
 83
 84	if strings.HasPrefix(n.Body, "removed milestone") {
 85		return NOTE_REMOVED_MILESTONE, ""
 86	}
 87
 88	if strings.HasPrefix(n.Body, "mentioned in issue") {
 89		return NOTE_MENTIONED_IN_ISSUE, ""
 90	}
 91
 92	if strings.HasPrefix(n.Body, "mentioned in merge request") {
 93		return NOTE_MENTIONED_IN_MERGE_REQUEST, ""
 94	}
 95
 96	return NOTE_UNKNOWN, ""
 97}
 98
 99// getNewTitle parses body diff given by gitlab api and return it final form
100// examples: "changed title from **fourth issue** to **fourth issue{+ changed+}**"
101//           "changed title from **fourth issue{- changed-}** to **fourth issue**"
102// because Gitlab
103func getNewTitle(diff string) string {
104	newTitle := strings.Split(diff, "** to **")[1]
105	newTitle = strings.Replace(newTitle, "{+", "", -1)
106	newTitle = strings.Replace(newTitle, "+}", "", -1)
107	return strings.TrimSuffix(newTitle, "**")
108}