/
check_run.go
154 lines (143 loc) · 4.87 KB
/
check_run.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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"encoding/json"
"strconv"
"time"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/gittar/pkg/gitmodule"
)
type CheckRun struct {
ID int64 `json:"id"`
MrID int64 `json:"mrId"`
Name string `json:"name"` // 名称 golang-lint/test
Type string `json:"type"` // 类型 CI
ExternalID string `json:"externalId"` // 外部系统 ID
PipelineID string `json:"pipelineId"` // 流水线 ID
Commit string `json:"commit"`
Status apistructs.CheckRunStatus `json:"status"` //progress/completed
Result apistructs.CheckRunResult `json:"result"` // success failed cancel
Output string `json:"output"`
CreatedAt time.Time `json:"createdAt"`
CompletedAt *time.Time `json:"completedAt"`
RepoID int64 `json:"repoId"`
}
type CheckRuns struct {
CheckRun []*CheckRun `json:"checkrun"`
Result apistructs.CheckRunResult `json:"result"`
Mergable bool `json:"mergable"`
}
func (svc *Service) CreateOrUpdateCheckRun(repo *gitmodule.Repository, request *apistructs.CheckRun) (*apistructs.CheckRun, error) {
var checkRun CheckRun
err := svc.db.Model(&CheckRun{}).
Where("pipeline_id =?", request.PipelineID).
First(&checkRun).Error
if err == nil {
// 已存在,更新
checkRun.Status = request.Status
checkRun.Result = request.Result
checkRun.Output = request.Output
if request.Status == apistructs.CheckRunStatusCompleted {
now := time.Now()
checkRun.CompletedAt = &now
}
err = svc.db.Save(&checkRun).Error
request.ID = checkRun.ID
return request, err
} else if err == gorm.ErrRecordNotFound {
// 不存在更新,创建新纪录
checkRun = CheckRun{
MrID: request.MrID,
Name: request.Name,
Type: request.Type,
ExternalID: request.ExternalID,
PipelineID: request.PipelineID,
Commit: request.Commit,
Status: request.Status,
Result: request.Result,
Output: request.Output,
CreatedAt: time.Now(),
CompletedAt: nil,
RepoID: repo.ID,
}
err := svc.db.Save(&checkRun).Error
if err != nil {
return nil, err
}
request.ID = checkRun.ID
request.CreatedAt = checkRun.CreatedAt
return request, nil
} else {
return nil, err
}
}
func (svc *Service) QueryCheckRuns(repo *gitmodule.Repository, mrID string) (apistructs.CheckRuns, error) {
var checkRuns []*CheckRun
id, err := strconv.Atoi(mrID)
if err != nil {
return apistructs.CheckRuns{}, err
}
mergeRequestInfo, err := svc.GetMergeRequestDetail(repo, id)
if err != nil {
return apistructs.CheckRuns{}, err
}
query := svc.db.Model(&CheckRun{}).Where("mr_id =? and repo_id =? and commit =?", mrID, repo.ID, mergeRequestInfo.SourceSha)
err = query.Find(&checkRuns).Error
if err != nil {
return apistructs.CheckRuns{}, err
}
res := CheckRuns{
CheckRun: checkRuns,
Result: apistructs.CheckRunResultSuccess,
Mergable: true,
}
for _, each := range res.CheckRun {
if each.Status == apistructs.CheckRunStatusInProgress {
res.Mergable = false
continue
}
if each.Result != apistructs.CheckRunResultSuccess {
res.Result = apistructs.CheckRunResultFailure
}
}
var state apistructs.CheckRuns
cont, err := json.Marshal(res)
if err != nil {
logrus.Errorf("marshal component state failed, content:%v, err:%v", res, err)
return apistructs.CheckRuns{}, err
}
err = json.Unmarshal(cont, &state)
if err != nil {
logrus.Errorf("unmarshal component state failed, content:%v, err:%v", cont, err)
return apistructs.CheckRuns{}, err
}
return state, err
}
func (svc *Service) IsCheckRunsValid(repo *gitmodule.Repository, mrID int64) (bool, error) {
query := svc.db.Model(&CheckRun{}).Where("mr_id =? and (result <> ? or status <> ?)",
mrID, apistructs.CheckRunResultSuccess, apistructs.CheckRunStatusCompleted)
var count int64
err := query.Count(&count).Error
if err != nil {
return false, err
}
return count == 0, nil
}
func (svc *Service) RemoveCheckRuns(mrID int64) error {
svc.db.Where("mr_id =? ", mrID).Delete(&CheckRun{})
return nil
}