-
Notifications
You must be signed in to change notification settings - Fork 124
/
commit_queue.go
151 lines (132 loc) · 4.41 KB
/
commit_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package model
import (
"regexp"
"strings"
"time"
"github.com/evergreen-ci/evergreen/model/commitqueue"
"github.com/evergreen-ci/utility"
)
const commitMessageLineLength = 72
type APICommitQueue struct {
ProjectID *string `json:"queue_id"`
Message *string `json:"message"` // note: this field is not populated by the conversion methods
Owner *string `json:"owner"`
Repo *string `json:"repo"`
Queue []APICommitQueueItem `json:"queue"`
}
type APICommitQueueItem struct {
Issue *string `json:"issue"`
PatchId *string `json:"patch_id"`
Version *string `json:"version"`
EnqueueTime *time.Time `json:"enqueueTime"`
Modules []APIModule `json:"modules"`
Patch *APIPatch `json:"patch"`
MessageOverride *string `json:"message_override"`
Source *string `json:"source"`
QueueLengthAtEnqueue *int `json:"queue_length_at_enqueue"`
}
type APICommitQueuePosition struct {
Position int `json:"position"`
}
type APICommitQueueItemAuthor struct {
Author *string `json:"author"`
}
func (cq *APICommitQueue) BuildFromService(cqService commitqueue.CommitQueue) {
cq.ProjectID = utility.ToStringPtr(cqService.ProjectID)
for _, item := range cqService.Queue {
cqItem := APICommitQueueItem{}
cqItem.BuildFromService(item)
cq.Queue = append(cq.Queue, cqItem)
}
}
func (item *APICommitQueueItem) BuildFromService(cqItemService commitqueue.CommitQueueItem) {
item.Issue = utility.ToStringPtr(cqItemService.Issue)
item.Version = utility.ToStringPtr(cqItemService.Version)
item.EnqueueTime = ToTimePtr(cqItemService.EnqueueTime)
item.MessageOverride = utility.ToStringPtr(cqItemService.MessageOverride)
item.Source = utility.ToStringPtr(cqItemService.Source)
item.PatchId = utility.ToStringPtr(cqItemService.PatchId)
item.QueueLengthAtEnqueue = utility.ToIntPtr(cqItemService.QueueLengthAtEnqueue)
for _, module := range cqItemService.Modules {
item.Modules = append(item.Modules, *APIModuleBuildFromService(module))
}
}
func (item *APICommitQueueItem) ToService() commitqueue.CommitQueueItem {
serviceItem := commitqueue.CommitQueueItem{
Issue: utility.FromStringPtr(item.Issue),
Version: utility.FromStringPtr(item.Version),
MessageOverride: utility.FromStringPtr(item.MessageOverride),
Source: utility.FromStringPtr(item.Source),
PatchId: utility.FromStringPtr(item.PatchId),
}
for _, module := range item.Modules {
serviceItem.Modules = append(serviceItem.Modules, *APIModuleToService(module))
}
return serviceItem
}
type GithubCommentCqData struct {
Modules []APIModule
MessageOverride string
}
func ParseGitHubComment(comment string) GithubCommentCqData {
data := GithubCommentCqData{}
lineRegex := regexp.MustCompile(`(\A.*)(?:\n*)([\S\s]*)`)
lines := lineRegex.FindAllStringSubmatch(comment, -1)
if len(lines) == 0 {
return data
}
for index, line := range lines[0] {
if index == 1 {
data = parseFirstLine(line)
} else if index == 2 {
data.MessageOverride = wrapString(line, commitMessageLineLength)
}
}
return data
}
// wrapString manually splits a string into smaller chunks and appends them to a new string.
func wrapString(str string, limit int) string {
var b strings.Builder
lines := strings.Split(str, "\n")
for i, line := range lines {
var currentLine string
words := strings.Fields(line)
for _, word := range words {
lineLength := len(currentLine + word)
// Only factor in spaces if the current line is not empty.
if len(currentLine) > 0 {
lineLength += 1
}
if lineLength > limit {
if len(currentLine) > 0 {
b.WriteString(currentLine + "\n")
}
currentLine = ""
}
if currentLine == "" {
currentLine += word
} else {
currentLine += " " + word
}
}
b.WriteString(currentLine)
if i < len(lines)-1 {
b.WriteString("\n")
}
}
return b.String()
}
func parseFirstLine(comment string) GithubCommentCqData {
modules := []APIModule{}
moduleRegex := regexp.MustCompile(`(?:--module|-m)\s+(\w+):(\d+)`)
moduleSlices := moduleRegex.FindAllStringSubmatch(comment, -1)
for _, moduleSlice := range moduleSlices {
modules = append(modules, APIModule{
Module: utility.ToStringPtr(moduleSlice[1]),
Issue: utility.ToStringPtr(moduleSlice[2]),
})
}
data := GithubCommentCqData{}
data.Modules = modules
return data
}