Actions support workflow dispatch event

This commit is contained in:
pangliang 2023-11-22 10:38:22 +08:00
parent b8a598e6a4
commit fbadf6f79e
4 changed files with 130 additions and 9 deletions

View File

@ -5,6 +5,8 @@ package actions
import (
"bytes"
"code.gitea.io/gitea/modules/log"
webhook_module "code.gitea.io/gitea/modules/webhook"
"fmt"
"net/http"
"strings"
@ -58,6 +60,10 @@ func MustEnableActions(ctx *context.Context) {
func List(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("actions.actions")
ctx.Data["PageIsActions"] = true
workflowID := ctx.FormString("workflow")
actorID := ctx.FormInt64("actor")
status := ctx.FormInt("status")
ctx.Data["CurWorkflow"] = workflowID
var workflows []Workflow
if empty, err := ctx.Repo.GitRepo.IsEmpty(); err != nil {
@ -124,6 +130,21 @@ func List(ctx *context.Context) {
}
}
workflows = append(workflows, workflow)
if len(workflowID) > 0 && ctx.Repo.IsAdmin() && workflow.Entry.Name() == workflowID {
events, err := actions.GetEventsFromContent(content)
if err != nil {
log.Warn("ignore check invalid workflow events %q: %v", workflow.Entry.Name(), err)
} else {
for _, event := range events {
if event.Name == webhook_module.HookEventWorkflowDispatch.Event() {
ctx.Data["AllowTriggerWorkflowDispatchEvent"] = true
break
}
}
}
}
}
}
ctx.Data["workflows"] = workflows
@ -134,17 +155,12 @@ func List(ctx *context.Context) {
page = 1
}
workflow := ctx.FormString("workflow")
actorID := ctx.FormInt64("actor")
status := ctx.FormInt("status")
ctx.Data["CurWorkflow"] = workflow
actionsConfig := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
ctx.Data["ActionsConfig"] = actionsConfig
if len(workflow) > 0 && ctx.Repo.IsAdmin() {
if len(workflowID) > 0 && ctx.Repo.IsAdmin() {
ctx.Data["AllowDisableOrEnableWorkflow"] = true
ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflow)
ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflowID)
}
// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
@ -161,7 +177,7 @@ func List(ctx *context.Context) {
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
},
RepoID: ctx.Repo.Repository.ID,
WorkflowID: workflow,
WorkflowID: workflowID,
TriggerUserID: actorID,
}
@ -198,7 +214,7 @@ func List(ctx *context.Context) {
pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx)
pager.AddParamString("workflow", workflow)
pager.AddParamString("workflow", workflowID)
pager.AddParamString("actor", fmt.Sprint(actorID))
pager.AddParamString("status", fmt.Sprint(status))
ctx.Data["Page"] = pager

View File

@ -5,10 +5,12 @@ package actions
import (
"archive/zip"
"code.gitea.io/gitea/modules/log"
"compress/gzip"
"context"
"errors"
"fmt"
"github.com/nektos/act/pkg/jobparser"
"io"
"net/http"
"net/url"
@ -26,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
webhook_module "code.gitea.io/gitea/modules/webhook"
actions_service "code.gitea.io/gitea/services/actions"
context_module "code.gitea.io/gitea/services/context"
@ -675,3 +678,100 @@ func disableOrEnableWorkflowFile(ctx *context_module.Context, isEnable bool) {
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
ctx.JSONRedirect(redirectURL)
}
func Run(ctx *context_module.Context) {
workflow := ctx.FormString("workflow")
if len(workflow) == 0 {
ctx.ServerError("workflow", nil)
return
}
// can not rerun job when workflow is disabled
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
cfg := cfgUnit.ActionsConfig()
if cfg.IsWorkflowDisabled(workflow) {
ctx.JSONError(ctx.Locale.Tr("actions.workflow.disabled"))
return
}
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
entries, err := actions.ListWorkflows(commit)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
var dwf *actions.DetectedWorkflow = nil
for _, entry := range entries {
if entry.Name() == workflow {
content, err := actions.GetContentFromEntry(entry)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
dwf = &actions.DetectedWorkflow{
EntryName: entry.Name(),
TriggerEvent: webhook_module.HookEventWorkflowDispatch.Event(),
Content: content,
}
break
}
}
if dwf == nil {
ctx.JSONError(ctx.Locale.Tr("actions.workflow.not_found"))
return
}
run := &actions_model.ActionRun{
Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
RepoID: ctx.Repo.Repository.ID,
OwnerID: ctx.Repo.Repository.OwnerID,
WorkflowID: workflow,
TriggerUserID: ctx.Doer.ID,
Ref: ctx.Repo.Repository.DefaultBranch,
CommitSHA: commit.ID.String(),
IsForkPullRequest: false,
Event: webhook_module.HookEventWorkflowDispatch,
TriggerEvent: webhook_module.HookEventWorkflowDispatch.Event(),
Status: actions_model.StatusWaiting,
}
// cancel running jobs of the same workflow
if err := actions_model.CancelRunningJobs(
ctx,
run.RepoID,
run.Ref,
run.WorkflowID,
); err != nil {
log.Error("CancelRunningJobs: %v", err)
}
// Parse the workflow specification from the cron schedule
workflows, err := jobparser.Parse(dwf.Content)
if err != nil {
ctx.ServerError("workflow", err)
return
}
// Insert the action run and its associated jobs into the database
if err := actions_model.InsertRun(ctx, run, workflows); err != nil {
ctx.ServerError("workflow", err)
return
}
alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID})
if err != nil {
log.Error("FindRunJobs: %v", err)
}
actions_service.CreateCommitStatus(ctx, alljobs...)
redirectURL := fmt.Sprintf("%s/actions?workflow=%s&actor=%s&status=%s", ctx.Repo.RepoLink, url.QueryEscape(workflow),
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
ctx.JSONRedirect(redirectURL)
}

View File

@ -1352,6 +1352,7 @@ func registerRoutes(m *web.Route) {
m.Get("", actions.List)
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
m.Post("/run", reqRepoActionsWriter, actions.Run)
m.Group("/runs/{run}", func() {
m.Combo("").

View File

@ -75,6 +75,10 @@
</div>
</button>
{{end}}
{{if .AllowTriggerWorkflowDispatchEvent}}
<button class="ui basic small compact button gt-mr-0 link-action" data-url="{{$.Link}}/run?workflow={{$.CurWorkflow}}&actor={{.CurActor}}&status={{$.CurStatus}}">{{ctx.Locale.Tr "Run workflow"}}</button>
{{end}}
</div>
{{template "repo/actions/runs_list" .}}
</div>