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.Body == "closed" {
35 return NOTE_CLOSED, ""
36 }
37
38 if n.Body == "reopened" {
39 return NOTE_REOPENED, ""
40 }
41
42 if n.Body == "changed the description" {
43 return NOTE_DESCRIPTION_CHANGED, ""
44 }
45
46 if n.Body == "locked this issue" {
47 return NOTE_LOCKED, ""
48 }
49
50 if n.Body == "unlocked this issue" {
51 return NOTE_UNLOCKED, ""
52 }
53
54 if strings.HasPrefix(n.Body, "changed title from") {
55 return NOTE_TITLE_CHANGED, getNewTitle(n.Body)
56 }
57
58 if strings.HasPrefix(n.Body, "changed due date to") {
59 return NOTE_CHANGED_DUEDATE, ""
60 }
61
62 if n.Body == "removed due date" {
63 return NOTE_REMOVED_DUEDATE, ""
64 }
65
66 if strings.HasPrefix(n.Body, "assigned to @") {
67 return NOTE_ASSIGNED, ""
68 }
69
70 if strings.HasPrefix(n.Body, "unassigned @") {
71 return NOTE_UNASSIGNED, ""
72 }
73
74 if strings.HasPrefix(n.Body, "changed milestone to %") {
75 return NOTE_CHANGED_MILESTONE, ""
76 }
77
78 if strings.HasPrefix(n.Body, "removed milestone") {
79 return NOTE_REMOVED_MILESTONE, ""
80 }
81
82 // comment don't have a specific format
83 return NOTE_COMMENT, n.Body
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}