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_UNKNOWN
27)
28
29// GetNoteType parses note body a give it type
30// Since gitlab api return all these NoteType event as the same object
31// and doesn't provide a field to specify the note type. We must parse the
32// note body to detect it type.
33func GetNoteType(n *gitlab.Note) (NoteType, string) {
34	if !n.System {
35		return NOTE_COMMENT, n.Body
36	}
37
38	if n.Body == "closed" {
39		return NOTE_CLOSED, ""
40	}
41
42	if n.Body == "reopened" {
43		return NOTE_REOPENED, ""
44	}
45
46	if n.Body == "changed the description" {
47		return NOTE_DESCRIPTION_CHANGED, ""
48	}
49
50	if n.Body == "locked this issue" {
51		return NOTE_LOCKED, ""
52	}
53
54	if n.Body == "unlocked this issue" {
55		return NOTE_UNLOCKED, ""
56	}
57
58	if strings.HasPrefix(n.Body, "changed title from") {
59		return NOTE_TITLE_CHANGED, getNewTitle(n.Body)
60	}
61
62	if strings.HasPrefix(n.Body, "changed due date to") {
63		return NOTE_CHANGED_DUEDATE, ""
64	}
65
66	if n.Body == "removed due date" {
67		return NOTE_REMOVED_DUEDATE, ""
68	}
69
70	if strings.HasPrefix(n.Body, "assigned to @") {
71		return NOTE_ASSIGNED, ""
72	}
73
74	if strings.HasPrefix(n.Body, "unassigned @") {
75		return NOTE_UNASSIGNED, ""
76	}
77
78	if strings.HasPrefix(n.Body, "changed milestone to %") {
79		return NOTE_CHANGED_MILESTONE, ""
80	}
81
82	if strings.HasPrefix(n.Body, "removed milestone") {
83		return NOTE_REMOVED_MILESTONE, ""
84	}
85
86	return NOTE_UNKNOWN, ""
87}
88
89// getNewTitle parses body diff given by gitlab api and return it final form
90// examples: "changed title from **fourth issue** to **fourth issue{+ changed+}**"
91//           "changed title from **fourth issue{- changed-}** to **fourth issue**"
92func getNewTitle(diff string) string {
93	newTitle := strings.Split(diff, "** to **")[1]
94	newTitle = strings.Replace(newTitle, "{+", "", -1)
95	newTitle = strings.Replace(newTitle, "+}", "", -1)
96	return strings.TrimSuffix(newTitle, "**")
97}