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