-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitlabproject.go
More file actions
213 lines (197 loc) · 4.74 KB
/
gitlabproject.go
File metadata and controls
213 lines (197 loc) · 4.74 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package models
import (
"encoding/json"
"github.com/jinzhu/gorm"
)
const (
GuestAccess = 10
ReporterAccess = 20
DeveloperAccess = 30
MaintainerAccess = 40
OwnerAccess = 50
)
type GitlabProjectAccess struct {
AccessLevel int `json:"access_level"`
NotificationLevel int `json:"notification_level"`
}
type GitlabPermissions struct {
GroupAccess GitlabProjectAccess `json:"group_access"`
ProjectAccess GitlabProjectAccess `json:"project_access"`
}
func (g GitlabPermissions) CheckAccess() bool {
return g.ProjectAccess.AccessLevel >= MaintainerAccess ||
g.GroupAccess.AccessLevel >= MaintainerAccess
}
type ProjectNameSpace struct {
Id int `json:"id"`
Name string `json:"name"`
Kind string `json:"kind"`
}
type GitlabProject struct {
BaseModel
Id int `gorm:"column:id"`
Name string `gorm:"column:name"`
Description string `gorm:"column:description"`
Group string `gorm:"column:group"`
BranchesStr string `gorm:"column:branches" json:"-"`
TagsStr string `gorm:"column:tags" json:"-"`
Branches []string `gorm:"-"`
Tags []string `gorm:"-"`
Permissions GitlabPermissions `gorm:"-" json:"permissions"`
NameSpace ProjectNameSpace `gorm:"-" json:"namespace"`
}
func (m *GitlabProject) TableName() string {
return "gitlab_project"
}
func (m GitlabProject) FindAll() []*GitlabProject {
var retVal []*GitlabProject
if err := DB.Find(&retVal).Error; err != nil {
m.Error(err)
return nil
}
for _, val := range retVal {
val.UnMarshal()
}
return retVal
}
func (m GitlabProject) FindByIds(projectIds []int) []*GitlabProject {
var retVal []*GitlabProject
if err := DB.Where("id IN (?)", projectIds).Find(&retVal).Error; err != nil {
m.Error(err)
return nil
}
for _, val := range retVal {
val.UnMarshal()
}
return retVal
}
func (m *GitlabProject) Find() *GitlabProject {
var retVal GitlabProject
if err := DB.Where("id=?", m.Id).First(&retVal).Error; err != nil {
m.Error(err)
return nil
}
retVal.UnMarshal()
return &retVal
}
func (m *GitlabProject) Update() bool {
if err := DB.Model(m).Omit("id").UpdateColumns(m).Error; err != nil {
m.Error(err)
return false
}
return true
}
func (m *GitlabProject) Marshal() {
if m.Branches != nil && len(m.Branches) != 0 {
bt, _ := json.Marshal(m.Branches)
m.BranchesStr = string(bt)
}
if m.Tags != nil && len(m.Tags) != 0 {
bt, _ := json.Marshal(m.Tags)
m.TagsStr = string(bt)
}
}
func (m *GitlabProject) UnMarshal() {
if m.BranchesStr != "" {
var val []string
json.Unmarshal([]byte(m.BranchesStr), &val)
m.Branches = val
}
if m.TagsStr != "" {
var val []string
json.Unmarshal([]byte(m.TagsStr), &val)
m.Tags = val
}
}
func (m *GitlabProject) Save() (*GitlabProject, bool) {
if m.Name == "" || m.Id == 0 {
return nil, false
}
var retVal GitlabProject
if err := DB.Where("id=?", m.Id).First(&retVal).Error; err != nil &&
err != gorm.ErrRecordNotFound {
m.Error(err)
return nil, false
}
if retVal.Id != 0 {
m.Marshal()
if m.BranchesStr != "" {
retVal.BranchesStr = m.BranchesStr
retVal.TagsStr = m.TagsStr
retVal.Group = m.Group
retVal.Description = m.Description
retVal.Name = m.Name
retVal.Update()
}
retVal.UnMarshal()
return &retVal, true
}
m.Marshal()
if err := DB.Create(m).Error; err != nil {
m.Error(err)
return nil, false
}
return m, true
}
func checkTag(m *GitlabProject, retVal GitlabProject) {
newTag := make([]string, 0)
for _, tag := range m.Tags {
exists := false
for _, oldTag := range retVal.Tags {
if oldTag == tag {
exists = true
break
}
}
if !exists {
newTag = append(newTag, tag)
}
}
if len(newTag) >= 0 {
m.Tags = append(m.Tags, newTag...)
}
}
func checkBranch(m *GitlabProject, retVal GitlabProject) []string {
newBranch := make([]string, 0)
for _, branch := range m.Branches {
exists := false
for _, oldBranch := range retVal.Branches {
if oldBranch == branch {
exists = true
break
}
}
if !exists {
newBranch = append(newBranch, branch)
}
}
if len(newBranch) >= 0 {
m.Branches = append(m.Branches, newBranch...)
}
return newBranch
}
func (m GitlabProject) GetProjectByName() *GitlabProject {
var retVal GitlabProject
if err := DB.Where("name=?", m.Name).First(&retVal).Error; err != nil {
m.Error(err)
return nil
}
retVal.UnMarshal()
return &retVal
}
/**
根据.config.sh传过来的branch寻找对应的分支,主要是处理tag的,如果branch是tag,则返回tag字符串
*/
func (m GitlabProject) GetBranch(branch string) string {
for _, _branch := range m.Branches {
if _branch == branch {
return branch
}
}
for _, _tag := range m.Tags {
if _tag == branch {
return "tag"
}
}
return ""
}