Skip to content

Commit a6247d4

Browse files
committed
workflow_dispatch use workflow from trigger branch
* htmx updates the input form on branch switch
1 parent 3d544a3 commit a6247d4

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

routers/web/repo/actions/actions.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import (
3232
)
3333

3434
const (
35-
tplListActions templates.TplName = "repo/actions/list"
36-
tplViewActions templates.TplName = "repo/actions/view"
35+
tplListActions templates.TplName = "repo/actions/list"
36+
tplDispatchInputsActions templates.TplName = "repo/actions/workflow_dispatch_inputs"
37+
tplViewActions templates.TplName = "repo/actions/view"
3738
)
3839

3940
type Workflow struct {
@@ -62,6 +63,10 @@ func MustEnableActions(ctx *context.Context) {
6263
}
6364

6465
func List(ctx *context.Context) {
66+
renderListAndWorkflowDispatchTemplate(ctx, tplListActions, false)
67+
}
68+
69+
func renderListAndWorkflowDispatchTemplate(ctx *context.Context, tplName templates.TplName, useRef bool) {
6570
ctx.Data["Title"] = ctx.Tr("actions.actions")
6671
ctx.Data["PageIsActions"] = true
6772
workflowID := ctx.FormString("workflow")
@@ -75,10 +80,34 @@ func List(ctx *context.Context) {
7580
ctx.ServerError("IsEmpty", err)
7681
return
7782
} else if !empty {
78-
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
79-
if err != nil {
80-
ctx.ServerError("GetBranchCommit", err)
81-
return
83+
var commit *git.Commit
84+
if useRef {
85+
ref := ctx.FormString("ref")
86+
if len(ref) == 0 {
87+
ctx.ServerError("ref", nil)
88+
return
89+
}
90+
// get target commit of run from specified ref
91+
refName := git.RefName(ref)
92+
var err error
93+
if refName.IsTag() {
94+
commit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
95+
} else if refName.IsBranch() {
96+
commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
97+
} else {
98+
ctx.ServerError("git_ref_name_error", nil)
99+
return
100+
}
101+
if err != nil {
102+
ctx.ServerError("target_ref_not_exist", err)
103+
return
104+
}
105+
} else {
106+
commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
107+
if err != nil {
108+
ctx.ServerError("GetBranchCommit", err)
109+
return
110+
}
82111
}
83112
entries, err := actions.ListWorkflows(commit)
84113
if err != nil {
@@ -265,7 +294,7 @@ func List(ctx *context.Context) {
265294
ctx.Data["Page"] = pager
266295
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
267296

268-
ctx.HTML(http.StatusOK, tplListActions)
297+
ctx.HTML(http.StatusOK, tplName)
269298
}
270299

271300
// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.

routers/web/repo/actions/view.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,8 @@ func Run(ctx *context_module.Context) {
812812
return
813813
}
814814

815-
// get workflow entry from default branch commit
816-
defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
817-
if err != nil {
818-
ctx.Error(http.StatusInternalServerError, err.Error())
819-
return
820-
}
821-
entries, err := actions.ListWorkflows(defaultBranchCommit)
815+
// get workflow entry from runTargetCommit
816+
entries, err := actions.ListWorkflows(runTargetCommit)
822817
if err != nil {
823818
ctx.Error(http.StatusInternalServerError, err.Error())
824819
return
@@ -928,3 +923,7 @@ func Run(ctx *context_module.Context) {
928923
ctx.Flash.Success(ctx.Tr("actions.workflow.run_success", workflowID))
929924
ctx.Redirect(redirectURL)
930925
}
926+
927+
func DispatchInputs(ctx *context_module.Context) {
928+
renderListAndWorkflowDispatchTemplate(ctx, tplDispatchInputsActions, true)
929+
}

routers/web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ func registerRoutes(m *web.Router) {
14121412
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
14131413
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
14141414
m.Post("/run", reqRepoActionsWriter, actions.Run)
1415+
m.Get("/dispatch-inputs", reqRepoActionsWriter, actions.DispatchInputs)
14151416

14161417
m.Group("/runs/{run}", func() {
14171418
m.Combo("").

templates/repo/actions/workflow_dispatch.tmpl

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<label>{{ctx.Locale.Tr "actions.workflow.from_ref"}}:</label>
1212
</span>
1313
<div class="ui inline field dropdown button select-branch branch-selector-dropdown ellipsis-items-nowrap">
14-
<input type="hidden" name="ref" value="refs/heads/{{index .Branches 0}}">
14+
<input type="hidden" name="ref" hx-sync="this:replace" hx-target="#runWorkflowDispatchModalInputs" hx-swap="innerHTML" hx-get="{{$.Link}}/dispatch-inputs?workflow={{$.CurWorkflow}}&actor={{$.CurActor}}&status={{.Status}}" hx-trigger="change" value="refs/heads/{{index .Branches 0}}">
1515
{{svg "octicon-git-branch" 14}}
1616
<div class="default text">{{index .Branches 0}}</div>
1717
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
@@ -49,29 +49,10 @@
4949

5050
<div class="divider"></div>
5151

52-
{{range $item := .WorkflowDispatchConfig.Inputs}}
53-
<div class="ui field {{if .Required}}required{{end}}">
54-
{{if eq .Type "choice"}}
55-
<label>{{.Description}}:</label>
56-
<select class="ui selection type dropdown" name="{{.Name}}">
57-
{{range .Options}}
58-
<option value="{{.}}" {{if eq $item.Default .}}selected{{end}} >{{.}}</option>
59-
{{end}}
60-
</select>
61-
{{else if eq .Type "boolean"}}
62-
<div class="ui inline checkbox">
63-
<label>{{.Description}}</label>
64-
<input type="checkbox" name="{{.Name}}" {{if eq .Default "true"}}checked{{end}}>
65-
</div>
66-
{{else if eq .Type "number"}}
67-
<label>{{.Description}}:</label>
68-
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
69-
{{else}}
70-
<label>{{.Description}}:</label>
71-
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
72-
{{end}}
52+
<div id="runWorkflowDispatchModalInputs">
53+
{{template "repo/actions/workflow_dispatch_inputs" .}}
7354
</div>
74-
{{end}}
55+
7556
<button class="ui tiny primary button" type="submit">Submit</button>
7657
</form>
7758
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{range $item := .WorkflowDispatchConfig.Inputs}}
2+
<div class="ui field {{if .Required}}required{{end}}">
3+
{{if eq .Type "choice"}}
4+
<label>{{.Description}}:</label>
5+
<select class="ui selection type dropdown" name="{{.Name}}">
6+
{{range .Options}}
7+
<option value="{{.}}" {{if eq $item.Default .}}selected{{end}} >{{.}}</option>
8+
{{end}}
9+
</select>
10+
{{else if eq .Type "boolean"}}
11+
<div class="ui inline checkbox">
12+
<label>{{.Description}}</label>
13+
<input type="checkbox" name="{{.Name}}" {{if eq .Default "true"}}checked{{end}}>
14+
</div>
15+
{{else if eq .Type "number"}}
16+
<label>{{.Description}}:</label>
17+
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
18+
{{else}}
19+
<label>{{.Description}}:</label>
20+
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
21+
{{end}}
22+
</div>
23+
{{end}}

0 commit comments

Comments
 (0)