Skip to content

Commit 60fffe1

Browse files
committed
workflow_dispatch use workflow from trigger branch
* htmx updates the input form on branch switch
1 parent 188e0ee commit 60fffe1

File tree

5 files changed

+104
-32
lines changed

5 files changed

+104
-32
lines changed

routers/web/repo/actions/actions.go

Lines changed: 3 additions & 2 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 {

routers/web/repo/actions/view.go

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"archive/zip"
8+
"bytes"
89
"compress/gzip"
910
"context"
1011
"errors"
@@ -812,13 +813,8 @@ func Run(ctx *context_module.Context) {
812813
return
813814
}
814815

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)
816+
// get workflow entry from runTargetCommit
817+
entries, err := actions.ListWorkflows(runTargetCommit)
822818
if err != nil {
823819
ctx.Error(http.StatusInternalServerError, err.Error())
824820
return
@@ -928,3 +924,73 @@ func Run(ctx *context_module.Context) {
928924
ctx.Flash.Success(ctx.Tr("actions.workflow.run_success", workflowID))
929925
ctx.Redirect(redirectURL)
930926
}
927+
928+
func DispatchInputs(ctx *context_module.Context) {
929+
workflowID := ctx.FormString("workflow")
930+
if len(workflowID) == 0 {
931+
ctx.ServerError("workflow", nil)
932+
return
933+
}
934+
935+
ref := ctx.FormString("ref")
936+
if len(ref) == 0 {
937+
ctx.ServerError("ref", nil)
938+
return
939+
}
940+
941+
// get target commit of run from specified ref
942+
refName := git.RefName(ref)
943+
var runTargetCommit *git.Commit
944+
var err error
945+
if refName.IsTag() {
946+
runTargetCommit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
947+
} else if refName.IsBranch() {
948+
runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
949+
} else {
950+
ctx.Flash.Error(ctx.Tr("form.git_ref_name_error", ref))
951+
return
952+
}
953+
if err != nil {
954+
ctx.Flash.Error(ctx.Tr("form.target_ref_not_exist", ref))
955+
return
956+
}
957+
958+
// get workflow entry from runTargetCommit
959+
entries, err := actions.ListWorkflows(runTargetCommit)
960+
if err != nil {
961+
ctx.Error(http.StatusInternalServerError, err.Error())
962+
return
963+
}
964+
965+
var curWorkflow *model.Workflow
966+
967+
// find workflow from commit
968+
for _, entry := range entries {
969+
if entry.Name() == workflowID {
970+
content, err := actions.GetContentFromEntry(entry)
971+
if err != nil {
972+
ctx.Error(http.StatusInternalServerError, err.Error())
973+
return
974+
}
975+
curWorkflow, err = model.ReadWorkflow(bytes.NewReader(content))
976+
if err != nil {
977+
ctx.ServerError("workflow", err)
978+
return
979+
}
980+
break
981+
}
982+
}
983+
984+
if curWorkflow == nil {
985+
ctx.Flash.Error(ctx.Tr("actions.workflow.not_found", workflowID))
986+
return
987+
}
988+
989+
workflowDispatchConfig := workflowDispatchConfig(curWorkflow)
990+
if workflowDispatchConfig == nil {
991+
return
992+
}
993+
ctx.Data["WorkflowDispatchConfig"] = workflowDispatchConfig
994+
995+
ctx.HTML(http.StatusOK, tplDispatchInputsActions)
996+
}

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)