-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
205 lines (173 loc) · 5.46 KB
/
util.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
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
package controllers
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/opensourceways/app-cla-server/config"
"github.com/opensourceways/app-cla-server/dbmodels"
"github.com/opensourceways/app-cla-server/email"
"github.com/opensourceways/app-cla-server/models"
"github.com/opensourceways/app-cla-server/util"
"github.com/opensourceways/app-cla-server/worker"
)
const (
apiHeaderToken = "Token"
apiAccessToken = "access_token"
apiAccessController = "access_controller"
contentTypeOfPDF = "application/pdf"
fileNameOfUploadingOrgSignatue = "org_signature_file"
)
func sendEmailToIndividual(linkID, to, subject string, builder email.IEmailMessageBulder) {
sendEmail(linkID, []string{to}, subject, builder)
}
func sendEmail(linkID string, to []string, subject string, builder email.IEmailMessageBulder) {
msg, err := builder.GenEmailMsg()
if err != nil {
beego.Error(err)
return
}
msg.To = to
msg.Subject = subject
worker.GetEmailWorker().SendSimpleMessage(linkID, msg)
}
func notifyCorpAdmin(linkID string, orgInfo *models.OrgInfo, info *dbmodels.CorporationManagerCreateOption) {
notifyCorpManagerWhenAdding(linkID, orgInfo, []dbmodels.CorporationManagerCreateOption{*info})
}
func notifyCorpManagerWhenAdding(linkID string, orgInfo *models.OrgInfo, info []dbmodels.CorporationManagerCreateOption) {
admin := (info[0].Role == dbmodels.RoleAdmin)
subject := fmt.Sprintf("Account on project of \"%s\"", orgInfo.OrgAlias)
for i := range info {
item := &info[i]
d := email.AddingCorpManager{
Admin: admin,
ID: item.ID,
User: item.Name,
Email: item.Email,
Password: item.Password,
Org: orgInfo.OrgAlias,
ProjectURL: orgInfo.ProjectURL(),
URLOfCLAPlatform: config.AppConfig.CLAPlatformURL,
}
sendEmailToIndividual(linkID, item.Email, subject, d)
}
}
func getSingingInfo(info dbmodels.TypeSigningInfo, fields []dbmodels.Field) dbmodels.TypeSigningInfo {
if len(info) == 0 {
return info
}
r := dbmodels.TypeSigningInfo{}
for i := range fields {
fid := fields[i].ID
if v, ok := info[fid]; ok {
r[fid] = v
}
}
return r
}
func parseOrgAndRepo(s string) (string, string) {
v := strings.Split(s, ":")
if len(v) == 2 {
return v[0], v[1]
}
return s, ""
}
func buildOrgRepo(platform, orgID, repoID string) *models.OrgRepo {
return &models.OrgRepo{
Platform: platform,
OrgID: orgID,
RepoID: repoID,
}
}
func genOrgFileLockPath(platform, org, repo string) string {
return util.GenFilePath(
config.AppConfig.PDFOrgSignatureDir,
util.GenFileName("lock", platform, org, repo),
)
}
func genCLAFilePath(linkID, applyTo, language string) string {
return util.GenFilePath(
config.AppConfig.PDFOrgSignatureDir,
util.GenFileName("cla", linkID, applyTo, language, ".txt"))
}
func genOrgSignatureFilePath(linkID, language string) string {
return util.GenFilePath(
config.AppConfig.PDFOrgSignatureDir,
util.GenFileName("signature", linkID, language, ".pdf"))
}
func genLinkID(v *dbmodels.OrgRepo) string {
repo := ""
if v.RepoID != "" {
repo = fmt.Sprintf("_%s", v.RepoID)
}
return fmt.Sprintf("%s_%s%s-%d", v.Platform, v.OrgID, repo, time.Now().UnixNano())
}
func getCLAInfoSigned(linkID, claLang, applyTo string) (*models.CLAInfo, *failedApiResult) {
claInfo, merr := models.GetCLAInfoSigned(linkID, claLang, applyTo)
if merr == nil {
if claInfo == nil {
return nil, newFailedApiResult(500, errSystemError, fmt.Errorf("cla info is empty, impossible"))
}
return claInfo, nil
}
if merr.IsErrorOf(models.ErrNoLinkOrUnsigned) {
return nil, nil
}
return nil, parseModelError(merr)
}
func signHelper(linkID, claLang, applyTo string, doSign func(*models.CLAInfo) *failedApiResult) *failedApiResult {
claInfo, fr := getCLAInfoSigned(linkID, claLang, applyTo)
if fr != nil {
return fr
}
if claInfo == nil {
orgInfo, merr := models.GetOrgOfLink(linkID)
if merr != nil {
return parseModelError(merr)
}
// no contributor signed for this language. lock to avoid the cla to be changed
// before writing to the db.
unlock, fr := lockOnRepo(orgInfo)
if fr != nil {
return fr
}
defer unlock()
claInfo, merr = models.GetCLAInfoToSign(linkID, claLang, applyTo)
if merr != nil {
return parseModelError(merr)
}
if claInfo == nil {
return newFailedApiResult(500, errSystemError, fmt.Errorf("no cla info, impossible"))
}
}
return doSign(claInfo)
}
func fetchInputPayloadData(input *[]byte, info interface{}) *failedApiResult {
if err := json.Unmarshal(*input, info); err != nil {
return newFailedApiResult(
400, errParsingApiBody, fmt.Errorf("invalid input payload: %s", err.Error()),
)
}
return nil
}
func saveCorpCLAAtLocal(cla *models.CLACreateOpt, linkID string) *failedApiResult {
if cla != nil {
path := genCLAFilePath(linkID, dbmodels.ApplyToCorporation, cla.Language)
if err := cla.SaveCLAAtLocal(path); err != nil {
return newFailedApiResult(500, errSystemError, err)
}
path = genOrgSignatureFilePath(linkID, cla.Language)
if err := cla.SaveSignatueAtLocal(path); err != nil {
return newFailedApiResult(500, errSystemError, err)
}
}
return nil
}
func lockOnRepo(orgInfo *dbmodels.OrgInfo) (func(), *failedApiResult) {
unlock, err := util.Lock(genOrgFileLockPath(orgInfo.Platform, orgInfo.OrgID, orgInfo.RepoID))
if err != nil {
return nil, newFailedApiResult(500, errSystemError, err)
}
return unlock, nil
}