Skip to content

Commit aac07d0

Browse files
yp05327techknowlogicklunny
authored
1 parent 6ff5400 commit aac07d0

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

modules/actions/workflows.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
4444
return ret, nil
4545
}
4646

47+
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
48+
f, err := entry.Blob().DataAsync()
49+
if err != nil {
50+
return nil, err
51+
}
52+
content, err := io.ReadAll(f)
53+
_ = f.Close()
54+
if err != nil {
55+
return nil, err
56+
}
57+
return content, nil
58+
}
59+
60+
func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
61+
workflow, err := model.ReadWorkflow(bytes.NewReader(content))
62+
if err != nil {
63+
return nil, err
64+
}
65+
events, err := jobparser.ParseRawOn(&workflow.RawOn)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
return events, nil
71+
}
72+
4773
func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader) (map[string][]byte, error) {
4874
entries, err := ListWorkflows(commit)
4975
if err != nil {
@@ -52,21 +78,11 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
5278

5379
workflows := make(map[string][]byte, len(entries))
5480
for _, entry := range entries {
55-
f, err := entry.Blob().DataAsync()
56-
if err != nil {
57-
return nil, err
58-
}
59-
content, err := io.ReadAll(f)
60-
_ = f.Close()
81+
content, err := GetContentFromEntry(entry)
6182
if err != nil {
6283
return nil, err
6384
}
64-
workflow, err := model.ReadWorkflow(bytes.NewReader(content))
65-
if err != nil {
66-
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
67-
continue
68-
}
69-
events, err := jobparser.ParseRawOn(&workflow.RawOn)
85+
events, err := GetEventsFromContent(content)
7086
if err != nil {
7187
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
7288
continue

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -3360,5 +3360,7 @@ runs.open_tab = %d Open
33603360
runs.closed_tab = %d Closed
33613361
runs.commit = Commit
33623362
runs.pushed_by = Pushed by
3363+
runs.valid_workflow_helper = Workflow config file is valid.
3364+
runs.invalid_workflow_helper = Workflow config file is invalid. Please check your config file: %s
33633365
33643366
need_approval_desc = Need approval to run workflows for fork pull request.

routers/web/repo/actions/actions.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const (
2323
tplViewActions base.TplName = "repo/actions/view"
2424
)
2525

26+
type Workflow struct {
27+
Entry git.TreeEntry
28+
IsInvalid bool
29+
ErrMsg string
30+
}
31+
2632
// MustEnableActions check if actions are enabled in settings
2733
func MustEnableActions(ctx *context.Context) {
2834
if !setting.Actions.Enabled {
@@ -47,7 +53,7 @@ func List(ctx *context.Context) {
4753
ctx.Data["Title"] = ctx.Tr("actions.actions")
4854
ctx.Data["PageIsActions"] = true
4955

50-
var workflows git.Entries
56+
var workflows []Workflow
5157
if empty, err := ctx.Repo.GitRepo.IsEmpty(); err != nil {
5258
ctx.Error(http.StatusInternalServerError, err.Error())
5359
return
@@ -62,13 +68,27 @@ func List(ctx *context.Context) {
6268
ctx.Error(http.StatusInternalServerError, err.Error())
6369
return
6470
}
65-
workflows, err = actions.ListWorkflows(commit)
71+
entries, err := actions.ListWorkflows(commit)
6672
if err != nil {
6773
ctx.Error(http.StatusInternalServerError, err.Error())
6874
return
6975
}
76+
workflows = make([]Workflow, 0, len(entries))
77+
for _, entry := range entries {
78+
workflow := Workflow{Entry: *entry}
79+
content, err := actions.GetContentFromEntry(entry)
80+
if err != nil {
81+
ctx.Error(http.StatusInternalServerError, err.Error())
82+
return
83+
}
84+
_, err = actions.GetEventsFromContent(content)
85+
if err != nil {
86+
workflow.IsInvalid = true
87+
workflow.ErrMsg = err.Error()
88+
}
89+
workflows = append(workflows, workflow)
90+
}
7091
}
71-
7292
ctx.Data["workflows"] = workflows
7393
ctx.Data["RepoLink"] = ctx.Repo.Repository.Link()
7494

templates/repo/actions/list.tmpl

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@
99
<a class="item{{if not $.CurWorkflow}} active{{end}}" href="{{$.Link}}">{{.locale.Tr "actions.runs.all_workflows"}}</a>
1010
<div class="divider"></div>
1111
{{range .workflows}}
12-
<a class="item{{if eq .Name $.CurWorkflow}} active{{end}}" href="{{$.Link}}?workflow={{.Name}}">{{.Name}}</a>
12+
<a class="item{{if eq .Entry.Name $.CurWorkflow}} active{{end}}" href="{{$.Link}}?workflow={{.Entry.Name}}">{{.Entry.Name}}
13+
{{if .IsInvalid}}
14+
<span class="tooltip" data-content="{{$.locale.Tr "actions.runs.invalid_workflow_helper" (.ErrMsg)}}">
15+
<i class="warning icon red"></i>
16+
</span>
17+
{{else}}
18+
<span class="tooltip" data-content="{{$.locale.Tr "actions.runs.valid_workflow_helper"}}">
19+
<i class="check icon green"></i>
20+
</span>
21+
{{end}}
22+
</a>
1323
{{end}}
1424
</div>
1525
</div>

0 commit comments

Comments
 (0)