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
 31func (nt NoteType) String() string {
 32	switch nt {
 33	case NOTE_COMMENT:
 34		return "note comment"
 35	case NOTE_TITLE_CHANGED:
 36		return "note title changed"
 37	case NOTE_DESCRIPTION_CHANGED:
 38		return "note description changed"
 39	case NOTE_CLOSED:
 40		return "note closed"
 41	case NOTE_REOPENED:
 42		return "note reopened"
 43	case NOTE_LOCKED:
 44		return "note locked"
 45	case NOTE_UNLOCKED:
 46		return "note unlocked"
 47	case NOTE_CHANGED_DUEDATE:
 48		return "note changed duedate"
 49	case NOTE_REMOVED_DUEDATE:
 50		return "note remove duedate"
 51	case NOTE_ASSIGNED:
 52		return "note assigned"
 53	case NOTE_UNASSIGNED:
 54		return "note unassigned"
 55	case NOTE_CHANGED_MILESTONE:
 56		return "note changed milestone"
 57	case NOTE_REMOVED_MILESTONE:
 58		return "note removed in milestone"
 59	case NOTE_MENTIONED_IN_ISSUE:
 60		return "note mentioned in issue"
 61	case NOTE_MENTIONED_IN_MERGE_REQUEST:
 62		return "note mentioned in merge request"
 63	case NOTE_UNKNOWN:
 64		return "note unknown"
 65	default:
 66		panic("unknown note type")
 67	}
 68}
 69
 70// GetNoteType parse a note system and body and return the note type and it content
 71func GetNoteType(n *gitlab.Note) (NoteType, string) {
 72	// when a note is a comment system is set to false
 73	// when a note is a different event system is set to true
 74	// because Gitlab
 75	if !n.System {
 76		return NOTE_COMMENT, n.Body
 77	}
 78
 79	if n.Body == "closed" {
 80		return NOTE_CLOSED, ""
 81	}
 82
 83	if n.Body == "reopened" {
 84		return NOTE_REOPENED, ""
 85	}
 86
 87	if n.Body == "changed the description" {
 88		return NOTE_DESCRIPTION_CHANGED, ""
 89	}
 90
 91	if n.Body == "locked this issue" {
 92		return NOTE_LOCKED, ""
 93	}
 94
 95	if n.Body == "unlocked this issue" {
 96		return NOTE_UNLOCKED, ""
 97	}
 98
 99	if strings.HasPrefix(n.Body, "changed title from") {
100		return NOTE_TITLE_CHANGED, getNewTitle(n.Body)
101	}
102
103	if strings.HasPrefix(n.Body, "changed due date to") {
104		return NOTE_CHANGED_DUEDATE, ""
105	}
106
107	if n.Body == "removed due date" {
108		return NOTE_REMOVED_DUEDATE, ""
109	}
110
111	if strings.HasPrefix(n.Body, "assigned to @") {
112		return NOTE_ASSIGNED, ""
113	}
114
115	if strings.HasPrefix(n.Body, "unassigned @") {
116		return NOTE_UNASSIGNED, ""
117	}
118
119	if strings.HasPrefix(n.Body, "changed milestone to %") {
120		return NOTE_CHANGED_MILESTONE, ""
121	}
122
123	if strings.HasPrefix(n.Body, "removed milestone") {
124		return NOTE_REMOVED_MILESTONE, ""
125	}
126
127	if strings.HasPrefix(n.Body, "mentioned in issue") {
128		return NOTE_MENTIONED_IN_ISSUE, ""
129	}
130
131	if strings.HasPrefix(n.Body, "mentioned in merge request") {
132		return NOTE_MENTIONED_IN_MERGE_REQUEST, ""
133	}
134
135	return NOTE_UNKNOWN, ""
136}
137
138// getNewTitle parses body diff given by gitlab api and return it final form
139// examples: "changed title from **fourth issue** to **fourth issue{+ changed+}**"
140//           "changed title from **fourth issue{- changed-}** to **fourth issue**"
141// because Gitlab
142func getNewTitle(diff string) string {
143	newTitle := strings.Split(diff, "** to **")[1]
144	newTitle = strings.Replace(newTitle, "{+", "", -1)
145	newTitle = strings.Replace(newTitle, "+}", "", -1)
146	return strings.TrimSuffix(newTitle, "**")
147}