Skip to content

Commit 4fc2d9c

Browse files
bencuriolunny
authored andcommitted
refactor
Signed-off-by: Bence Santha <[email protected]>
1 parent 5afb8ae commit 4fc2d9c

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

routers/api/v1/repo/action.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,6 @@ func (a ActionWorkflow) ListRepositoryWorkflows(ctx *context.APIContext) {
616616
// "$ref": "#/responses/forbidden"
617617
// "404":
618618
// "$ref": "#/responses/notFound"
619-
// "409":
620-
// "$ref": "#/responses/conflict"
621619
// "422":
622620
// "$ref": "#/responses/validationError"
623621
// "500":
@@ -630,6 +628,7 @@ func (a ActionWorkflow) ListRepositoryWorkflows(ctx *context.APIContext) {
630628

631629
if len(workflows) == 0 {
632630
ctx.JSON(http.StatusNotFound, nil)
631+
return
633632
}
634633

635634
ctx.SetTotalCountHeader(int64(len(workflows)))
@@ -667,8 +666,6 @@ func (a ActionWorkflow) GetWorkflow(ctx *context.APIContext) {
667666
// "$ref": "#/responses/forbidden"
668667
// "404":
669668
// "$ref": "#/responses/notFound"
670-
// "409":
671-
// "$ref": "#/responses/conflict"
672669
// "422":
673670
// "$ref": "#/responses/validationError"
674671
// "500":
@@ -687,6 +684,7 @@ func (a ActionWorkflow) GetWorkflow(ctx *context.APIContext) {
687684

688685
if workflow == nil {
689686
ctx.JSON(http.StatusNotFound, nil)
687+
return
690688
}
691689

692690
ctx.JSON(http.StatusOK, workflow)
@@ -723,8 +721,6 @@ func (a ActionWorkflow) DisableWorkflow(ctx *context.APIContext) {
723721
// "$ref": "#/responses/forbidden"
724722
// "404":
725723
// "$ref": "#/responses/notFound"
726-
// "409":
727-
// "$ref": "#/responses/conflict"
728724
// "422":
729725
// "$ref": "#/responses/validationError"
730726

@@ -737,6 +733,7 @@ func (a ActionWorkflow) DisableWorkflow(ctx *context.APIContext) {
737733
err := actions_service.DisableActionWorkflow(ctx, workflowID)
738734
if err != nil {
739735
ctx.Error(http.StatusInternalServerError, "DisableActionWorkflow", err)
736+
return
740737
}
741738

742739
ctx.Status(http.StatusNoContent)
@@ -777,8 +774,6 @@ func (a ActionWorkflow) DispatchWorkflow(ctx *context.APIContext) {
777774
// "$ref": "#/responses/forbidden"
778775
// "404":
779776
// "$ref": "#/responses/notFound"
780-
// "409":
781-
// "$ref": "#/responses/conflict"
782777
// "422":
783778
// "$ref": "#/responses/validationError"
784779

@@ -846,6 +841,7 @@ func (a ActionWorkflow) EnableWorkflow(ctx *context.APIContext) {
846841
err := actions_service.EnableActionWorkflow(ctx, workflowID)
847842
if err != nil {
848843
ctx.Error(http.StatusInternalServerError, "EnableActionWorkflow", err)
844+
return
849845
}
850846

851847
ctx.Status(http.StatusNoContent)

services/actions/workflow.go

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,16 @@ import (
2727
)
2828

2929
func getActionWorkflowPath(commit *git.Commit) string {
30-
_, err := commit.SubTree(".gitea/workflows")
31-
if err == nil {
32-
return ".gitea/workflows"
33-
}
34-
35-
if _, ok := err.(git.ErrNotExist); ok {
36-
_, err = commit.SubTree(".github/workflows")
37-
return ".github/workflows"
30+
paths := []string{".gitea/workflows", ".github/workflows"}
31+
for _, path := range paths {
32+
if _, err := commit.SubTree(path); err == nil {
33+
return path
34+
}
3835
}
39-
4036
return ""
4137
}
4238

43-
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *git.TreeEntry) (*api.ActionWorkflow, error) {
39+
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *git.TreeEntry) *api.ActionWorkflow {
4440
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
4541
cfg := cfgUnit.ActionsConfig()
4642

@@ -88,17 +84,22 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *
8884
URL: URL,
8985
HTMLURL: HTMLURL,
9086
BadgeURL: badgeURL,
91-
}, nil
87+
}
9288
}
9389

9490
func disableOrEnableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
91+
workflow, err := GetActionWorkflow(ctx, workflowID)
92+
if err != nil {
93+
return err
94+
}
95+
9596
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
9697
cfg := cfgUnit.ActionsConfig()
9798

9899
if isEnable {
99-
cfg.EnableWorkflow(workflowID)
100+
cfg.EnableWorkflow(workflow.ID)
100101
} else {
101-
cfg.DisableWorkflow(workflowID)
102+
cfg.DisableWorkflow(workflow.ID)
102103
}
103104

104105
return repo_model.UpdateRepoUnit(ctx, cfgUnit)
@@ -119,11 +120,7 @@ func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error)
119120

120121
workflows := make([]*api.ActionWorkflow, len(entries))
121122
for i, entry := range entries {
122-
workflows[i], err = getActionWorkflowEntry(ctx, defaultBranchCommit, entry)
123-
if err != nil {
124-
ctx.Error(http.StatusInternalServerError, "WorkflowGetError", err.Error())
125-
return nil, err
126-
}
123+
workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, entry)
127124
}
128125

129126
return workflows, nil
@@ -135,100 +132,86 @@ func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionW
135132
return nil, err
136133
}
137134

138-
workflows := make([]*api.ActionWorkflow, len(entries))
139-
for i, entry := range entries {
135+
for _, entry := range entries {
140136
if entry.Name == workflowID {
141-
workflows[i] = entry
142-
break
137+
return entry, nil
143138
}
144139
}
145140

146-
return workflows[len(workflows)-1], nil
141+
return nil, fmt.Errorf("workflow not found")
147142
}
148143

149144
func DisableActionWorkflow(ctx *context.APIContext, workflowID string) error {
150145
return disableOrEnableWorkflow(ctx, workflowID, false)
151146
}
152147

153148
func DispatchActionWorkflow(ctx *context.APIContext, workflowID string, opt *api.CreateActionWorkflowDispatch) {
154-
// can not run job when workflow is disabled
155149
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
156150
cfg := cfgUnit.ActionsConfig()
151+
157152
if cfg.IsWorkflowDisabled(workflowID) {
158153
ctx.Error(http.StatusInternalServerError, "WorkflowDisabled", ctx.Tr("actions.workflow.disabled"))
159154
return
160155
}
161156

162-
// get target commit of run from specified ref
163157
refName := git.RefName(opt.Ref)
164158
var runTargetCommit *git.Commit
165159
var err error
166-
if refName.IsTag() {
160+
161+
switch {
162+
case refName.IsTag():
167163
runTargetCommit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
168-
} else if refName.IsBranch() {
164+
case refName.IsBranch():
169165
runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
170-
} else {
166+
default:
171167
ctx.Error(http.StatusInternalServerError, "WorkflowRefNameError", ctx.Tr("form.git_ref_name_error", opt.Ref))
172168
return
173169
}
170+
174171
if err != nil {
175172
ctx.Error(http.StatusNotFound, "WorkflowRefNotFound", ctx.Tr("form.target_ref_not_exist", opt.Ref))
176173
return
177174
}
178175

179-
// get workflow entry from default branch commit
180176
defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
181177
if err != nil {
182178
ctx.Error(http.StatusInternalServerError, "WorkflowDefaultBranchError", err.Error())
183179
return
184180
}
181+
185182
entries, err := actions.ListWorkflows(defaultBranchCommit)
186183
if err != nil {
187184
ctx.Error(http.StatusNotFound, "WorkflowListNotFound", err.Error())
185+
return
188186
}
189187

190-
// find workflow from commit
191-
var workflows []*jobparser.SingleWorkflow
188+
var workflow *jobparser.SingleWorkflow
192189
for _, entry := range entries {
193190
if entry.Name() == workflowID {
194191
content, err := actions.GetContentFromEntry(entry)
195192
if err != nil {
196193
ctx.Error(http.StatusInternalServerError, "WorkflowGetContentError", err.Error())
197194
return
198195
}
199-
workflows, err = jobparser.Parse(content)
200-
if err != nil {
196+
workflows, err := jobparser.Parse(content)
197+
if err != nil || len(workflows) == 0 {
201198
ctx.Error(http.StatusInternalServerError, "WorkflowParseError", err.Error())
202199
return
203200
}
201+
workflow = workflows[0]
204202
break
205203
}
206204
}
207205

208-
if len(workflows) == 0 {
206+
if workflow == nil {
209207
ctx.Error(http.StatusNotFound, "WorkflowNotFound", ctx.Tr("actions.workflow.not_found", workflowID))
210208
return
211209
}
212210

213-
workflow := &model.Workflow{
214-
RawOn: workflows[0].RawOn,
215-
}
216-
inputs := make(map[string]any)
217-
if workflowDispatch := workflow.WorkflowDispatchConfig(); workflowDispatch != nil {
218-
for name, config := range workflowDispatch.Inputs {
219-
value, exists := opt.Inputs[name]
220-
if !exists {
221-
continue
222-
}
223-
if config.Type == "boolean" {
224-
inputs[name] = strconv.FormatBool(value == "on")
225-
} else if value != "" {
226-
inputs[name] = value
227-
} else {
228-
inputs[name] = config.Default
229-
}
230-
}
231-
}
211+
// Process workflow inputs
212+
inputs := processWorkflowInputs(opt, &model.Workflow{
213+
RawOn: workflow.RawOn,
214+
})
232215

233216
workflowDispatchPayload := &api.WorkflowDispatchPayload{
234217
Workflow: workflowID,
@@ -237,8 +220,9 @@ func DispatchActionWorkflow(ctx *context.APIContext, workflowID string, opt *api
237220
Inputs: inputs,
238221
Sender: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone),
239222
}
240-
var eventPayload []byte
241-
if eventPayload, err = workflowDispatchPayload.JSONPayload(); err != nil {
223+
224+
eventPayload, err := workflowDispatchPayload.JSONPayload()
225+
if err != nil {
242226
ctx.Error(http.StatusInternalServerError, "WorkflowDispatchJSONParseError", err.Error())
243227
return
244228
}
@@ -258,7 +242,7 @@ func DispatchActionWorkflow(ctx *context.APIContext, workflowID string, opt *api
258242
Status: actions_model.StatusWaiting,
259243
}
260244

261-
if err := actions_model.InsertRun(ctx, run, workflows); err != nil {
245+
if err := actions_model.InsertRun(ctx, run, []*jobparser.SingleWorkflow{workflow}); err != nil {
262246
ctx.Error(http.StatusInternalServerError, "WorkflowInsertRunError", err.Error())
263247
return
264248
}
@@ -268,9 +252,32 @@ func DispatchActionWorkflow(ctx *context.APIContext, workflowID string, opt *api
268252
ctx.Error(http.StatusInternalServerError, "WorkflowFindRunJobError", err.Error())
269253
return
270254
}
255+
271256
CreateCommitStatus(ctx, alljobs...)
272257
}
273258

259+
func processWorkflowInputs(opt *api.CreateActionWorkflowDispatch, workflow *model.Workflow) map[string]any {
260+
inputs := make(map[string]any)
261+
if workflowDispatch := workflow.WorkflowDispatchConfig(); workflowDispatch != nil {
262+
for name, config := range workflowDispatch.Inputs {
263+
value, exists := opt.Inputs[name]
264+
if !exists {
265+
continue
266+
}
267+
if value == "" {
268+
value = config.Default
269+
}
270+
switch config.Type {
271+
case "boolean":
272+
inputs[name] = strconv.FormatBool(value == "on")
273+
default:
274+
inputs[name] = value
275+
}
276+
}
277+
}
278+
return inputs
279+
}
280+
274281
func EnableActionWorkflow(ctx *context.APIContext, workflowID string) error {
275282
return disableOrEnableWorkflow(ctx, workflowID, true)
276283
}

0 commit comments

Comments
 (0)