Skip to content
143 changes: 89 additions & 54 deletions routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import (
)

const (
tplListActions templates.TplName = "repo/actions/list"
tplViewActions templates.TplName = "repo/actions/view"
tplListActions templates.TplName = "repo/actions/list"
tplDispatchInputsActions templates.TplName = "repo/actions/workflow_dispatch_inputs"
tplViewActions templates.TplName = "repo/actions/view"
)

type Workflow struct {
Expand Down Expand Up @@ -62,6 +63,14 @@ func MustEnableActions(ctx *context.Context) {
}

func List(ctx *context.Context) {
renderListAndWorkflowDispatchTemplate(ctx, tplListActions, false)
}

func WorkflowDispatchInputs(ctx *context.Context) {
renderListAndWorkflowDispatchTemplate(ctx, tplDispatchInputsActions, true)
}

func renderListAndWorkflowDispatchTemplate(ctx *context.Context, tplName templates.TplName, inputsOnly bool) {
ctx.Data["Title"] = ctx.Tr("actions.actions")
ctx.Data["PageIsActions"] = true
workflowID := ctx.FormString("workflow")
Expand All @@ -75,10 +84,34 @@ func List(ctx *context.Context) {
ctx.ServerError("IsEmpty", err)
return
} else if !empty {
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.ServerError("GetBranchCommit", err)
return
var commit *git.Commit
if inputsOnly {
ref := ctx.FormString("ref")
if len(ref) == 0 {
ctx.ServerError("ref", nil)
return
}
// get target commit of run from specified ref
refName := git.RefName(ref)
var err error
if refName.IsTag() {
commit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
} else if refName.IsBranch() {
commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
} else {
ctx.ServerError("git_ref_name_error", nil)
return
}
if err != nil {
ctx.ServerError("target_ref_not_exist", err)
return
}
} else {
commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.ServerError("GetBranchCommit", err)
return
}
}
entries, err := actions.ListWorkflows(commit)
if err != nil {
Expand Down Expand Up @@ -207,65 +240,67 @@ func List(ctx *context.Context) {
}
}

// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
// they will be 0 by default, which indicates get all status or actors
ctx.Data["CurActor"] = actorID
ctx.Data["CurStatus"] = status
if actorID > 0 || status > int(actions_model.StatusUnknown) {
ctx.Data["IsFiltered"] = true
}
if !inputsOnly {
// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
// they will be 0 by default, which indicates get all status or actors
ctx.Data["CurActor"] = actorID
ctx.Data["CurStatus"] = status
if actorID > 0 || status > int(actions_model.StatusUnknown) {
ctx.Data["IsFiltered"] = true
}

opts := actions_model.FindRunOptions{
ListOptions: db.ListOptions{
Page: page,
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
},
RepoID: ctx.Repo.Repository.ID,
WorkflowID: workflowID,
TriggerUserID: actorID,
}
opts := actions_model.FindRunOptions{
ListOptions: db.ListOptions{
Page: page,
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
},
RepoID: ctx.Repo.Repository.ID,
WorkflowID: workflowID,
TriggerUserID: actorID,
}

// if status is not StatusUnknown, it means user has selected a status filter
if actions_model.Status(status) != actions_model.StatusUnknown {
opts.Status = []actions_model.Status{actions_model.Status(status)}
}
// if status is not StatusUnknown, it means user has selected a status filter
if actions_model.Status(status) != actions_model.StatusUnknown {
opts.Status = []actions_model.Status{actions_model.Status(status)}
}

runs, total, err := db.FindAndCount[actions_model.ActionRun](ctx, opts)
if err != nil {
ctx.ServerError("FindAndCount", err)
return
}
runs, total, err := db.FindAndCount[actions_model.ActionRun](ctx, opts)
if err != nil {
ctx.ServerError("FindAndCount", err)
return
}

for _, run := range runs {
run.Repo = ctx.Repo.Repository
}
for _, run := range runs {
run.Repo = ctx.Repo.Repository
}

if err := actions_model.RunList(runs).LoadTriggerUser(ctx); err != nil {
ctx.ServerError("LoadTriggerUser", err)
return
}
if err := actions_model.RunList(runs).LoadTriggerUser(ctx); err != nil {
ctx.ServerError("LoadTriggerUser", err)
return
}

if err := loadIsRefDeleted(ctx, ctx.Repo.Repository.ID, runs); err != nil {
log.Error("LoadIsRefDeleted", err)
}
if err := loadIsRefDeleted(ctx, ctx.Repo.Repository.ID, runs); err != nil {
log.Error("LoadIsRefDeleted", err)
}

ctx.Data["Runs"] = runs
ctx.Data["Runs"] = runs

actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID)
if err != nil {
ctx.ServerError("GetActors", err)
return
}
ctx.Data["Actors"] = shared_user.MakeSelfOnTop(ctx.Doer, actors)
actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID)
if err != nil {
ctx.ServerError("GetActors", err)
return
}
ctx.Data["Actors"] = shared_user.MakeSelfOnTop(ctx.Doer, actors)

ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx)
ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx)

pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
}

ctx.HTML(http.StatusOK, tplListActions)
ctx.HTML(http.StatusOK, tplName)
}

// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.
Expand Down
9 changes: 2 additions & 7 deletions routers/web/repo/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,8 @@ func Run(ctx *context_module.Context) {
return
}

// get workflow entry from default branch commit
defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
entries, err := actions.ListWorkflows(defaultBranchCommit)
// get workflow entry from runTargetCommit
entries, err := actions.ListWorkflows(runTargetCommit)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ func registerRoutes(m *web.Router) {
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
m.Post("/run", reqRepoActionsWriter, actions.Run)
m.Get("/workflow-dispatch-inputs", reqRepoActionsWriter, actions.WorkflowDispatchInputs)

m.Group("/runs/{run}", func() {
m.Combo("").
Expand Down
27 changes: 3 additions & 24 deletions templates/repo/actions/workflow_dispatch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<label>{{ctx.Locale.Tr "actions.workflow.from_ref"}}:</label>
</span>
<div class="ui inline field dropdown button select-branch branch-selector-dropdown ellipsis-items-nowrap">
<input type="hidden" name="ref" value="refs/heads/{{index .Branches 0}}">
<input type="hidden" name="ref" hx-sync="this:replace" hx-target="#runWorkflowDispatchModalInputs" hx-swap="innerHTML" hx-get="{{$.Link}}/workflow-dispatch-inputs?workflow={{$.CurWorkflow}}&actor={{$.CurActor}}&status={{.Status}}" hx-trigger="change" value="refs/heads/{{index .Branches 0}}">
{{svg "octicon-git-branch" 14}}
<div class="default text">{{index .Branches 0}}</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
Expand Down Expand Up @@ -49,30 +49,9 @@

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

{{range $item := .WorkflowDispatchConfig.Inputs}}
<div class="ui field {{if .Required}}required{{end}}">
{{if eq .Type "choice"}}
<label>{{.Description}}:</label>
<select class="ui selection type dropdown" name="{{.Name}}">
{{range .Options}}
<option value="{{.}}" {{if eq $item.Default .}}selected{{end}} >{{.}}</option>
{{end}}
</select>
{{else if eq .Type "boolean"}}
<div class="ui inline checkbox">
<label>{{.Description}}</label>
<input type="checkbox" name="{{.Name}}" {{if eq .Default "true"}}checked{{end}}>
</div>
{{else if eq .Type "number"}}
<label>{{.Description}}:</label>
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
{{else}}
<label>{{.Description}}:</label>
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
{{end}}
<div id="runWorkflowDispatchModalInputs">
{{template "repo/actions/workflow_dispatch_inputs" .}}
</div>
{{end}}
<button class="ui tiny primary button" type="submit">Submit</button>
</form>
</div>
</div>
37 changes: 37 additions & 0 deletions templates/repo/actions/workflow_dispatch_inputs.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{if not .WorkflowDispatchConfig}}
<div class="ui red info message tw-flex tw-justify-between tw-items-center">
<span class="ui text middle">{{ctx.Locale.Tr "actions.workflow.not_found" $.CurWorkflow}}</span>
</div>
{{else}}
{{range $item := .WorkflowDispatchConfig.Inputs}}
<div class="ui field {{if .Required}}required{{end}}">
{{if eq .Type "choice"}}
<label>{{Iif .Description .Description .Name}}:</label>
<select class="ui selection type dropdown" name="{{.Name}}">
{{range .Options}}
<option value="{{.}}" {{if eq $item.Default .}}selected{{end}} >{{.}}</option>
{{end}}
</select>
{{else if eq .Type "boolean"}}
<div class="ui inline checkbox">
<label>{{Iif .Description .Description .Name}}</label>
<input type="checkbox" name="{{.Name}}" {{if eq .Default "true"}}checked{{end}}>
</div>
{{else if eq .Type "number"}}
<label>{{Iif .Description .Description .Name}}:</label>
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
{{else}}
<label>{{Iif .Description .Description .Name}}:</label>
<input name="{{.Name}}" value="{{.Default}}" {{if .Required}}required{{end}}>
{{end}}
</div>
{{end}}
<button class="ui tiny primary button" type="submit">Submit</button>
{{end}}
{{range .workflows}}
{{if and .ErrMsg (eq .Entry.Name $.CurWorkflow)}}
<span data-tooltip-content="{{.ErrMsg}}">
{{svg "octicon-alert" 16 "text red"}}
</span>
{{end}}
{{end}}
Loading