Skip to content

Commit d43f435

Browse files
authored
Merge branch 'main' into refactor-branch-selector
2 parents 3c698d2 + aac07d0 commit d43f435

File tree

7 files changed

+79
-33
lines changed

7 files changed

+79
-33
lines changed

modules/actions/workflows.go

Lines changed: 28 additions & 12 deletions
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

modules/context/pagination.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ type Pagination struct {
1818
urlParams []string
1919
}
2020

21-
// NewPagination creates a new instance of the Pagination struct
22-
func NewPagination(total, page, issueNum, numPages int) *Pagination {
21+
// NewPagination creates a new instance of the Pagination struct.
22+
// "pagingNum" is "page size" or "limit", "current" is "page"
23+
func NewPagination(total, pagingNum, current, numPages int) *Pagination {
2324
p := &Pagination{}
24-
p.Paginater = paginator.New(total, page, issueNum, numPages)
25+
p.Paginater = paginator.New(total, pagingNum, current, numPages)
2526
return p
2627
}
2728

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
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

Lines changed: 23 additions & 3 deletions
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

routers/web/repo/branch.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"net/http"
11+
"net/url"
1112
"strings"
1213

1314
"code.gitea.io/gitea/models"
@@ -65,21 +66,17 @@ func Branches(ctx *context.Context) {
6566
if page <= 1 {
6667
page = 1
6768
}
69+
pageSize := setting.Git.BranchesRangeSize
6870

69-
limit := ctx.FormInt("limit")
70-
if limit <= 0 || limit > setting.Git.BranchesRangeSize {
71-
limit = setting.Git.BranchesRangeSize
72-
}
73-
74-
skip := (page - 1) * limit
75-
log.Debug("Branches: skip: %d limit: %d", skip, limit)
76-
defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit)
71+
skip := (page - 1) * pageSize
72+
log.Debug("Branches: skip: %d limit: %d", skip, pageSize)
73+
defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, pageSize)
7774
if ctx.Written() {
7875
return
7976
}
8077
ctx.Data["Branches"] = branches
8178
ctx.Data["DefaultBranchBranch"] = defaultBranchBranch
82-
pager := context.NewPagination(branchesCount, setting.Git.BranchesRangeSize, page, 5)
79+
pager := context.NewPagination(branchesCount, pageSize, page, 5)
8380
pager.SetDefaultParams(ctx)
8481
ctx.Data["Page"] = pager
8582

@@ -165,7 +162,7 @@ func RestoreBranchPost(ctx *context.Context) {
165162

166163
func redirect(ctx *context.Context) {
167164
ctx.JSON(http.StatusOK, map[string]interface{}{
168-
"redirect": ctx.Repo.RepoLink + "/branches",
165+
"redirect": ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")),
169166
})
170167
}
171168

templates/repo/actions/list.tmpl

Lines changed: 11 additions & 1 deletion
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>

templates/repo/branch/list.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@
8181
<td class="three wide right aligned">
8282
{{if not .LatestPullRequest}}
8383
{{if .IsIncluded}}
84-
<a class="ui tooltip orange large label" data-content="{{$.locale.Tr "repo.branch.included_desc"}}" data-position="top right">
84+
<span class="ui tooltip orange large label" data-content="{{$.locale.Tr "repo.branch.included_desc"}}" data-position="top right">
8585
{{svg "octicon-git-pull-request"}} {{$.locale.Tr "repo.branch.included"}}
86-
</a>
86+
</span>
8787
{{else if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}}
8888
<a href="{{$.RepoLink}}/compare/{{PathEscapeSegments $.DefaultBranch}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{PathEscape $.Owner.Name}}:{{end}}{{PathEscapeSegments .Name}}">
8989
<button id="new-pull-request" class="ui compact basic button gt-mr-0">{{if $.CanPull}}{{$.locale.Tr "repo.pulls.compare_changes"}}{{else}}{{$.locale.Tr "action.compare_branch"}}{{end}}</button>
@@ -123,13 +123,13 @@
123123
{{end}}
124124
{{if and $.IsWriter (not $.IsMirror) (not $.Repository.IsArchived) (not .IsProtected)}}
125125
{{if .IsDeleted}}
126-
<button class="ui basic jump button icon tooltip undo-button" data-url="{{$.Link}}/restore?branch_id={{.DeletedBranch.ID}}&name={{.DeletedBranch.Name}}" data-content="{{$.locale.Tr "repo.branch.restore" (.Name)}}" data-position="top right">
126+
<button class="ui basic jump button icon tooltip undo-button" data-url="{{$.Link}}/restore?branch_id={{.DeletedBranch.ID}}&name={{.DeletedBranch.Name}}&page={{$.Page.Paginater.Current}}" data-content="{{$.locale.Tr "repo.branch.restore" (.Name)}}" data-position="top right">
127127
<span class="text blue">
128128
{{svg "octicon-reply"}}
129129
</span>
130130
</button>
131131
{{else}}
132-
<button class="ui basic jump button icon tooltip delete-button delete-branch-button" data-url="{{$.Link}}/delete?name={{.Name}}" data-content="{{$.locale.Tr "repo.branch.delete" (.Name)}}" data-position="top right" data-name="{{.Name}}">
132+
<button class="ui basic jump button icon tooltip delete-button delete-branch-button" data-url="{{$.Link}}/delete?name={{.Name}}&page={{$.Page.Paginater.Current}}" data-content="{{$.locale.Tr "repo.branch.delete" (.Name)}}" data-position="top right" data-name="{{.Name}}">
133133
{{svg "octicon-trash"}}
134134
</button>
135135
{{end}}

0 commit comments

Comments
 (0)