aboutsummaryrefslogtreecommitdiffstats
path: root/services/actions/workflow.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/actions/workflow.go')
-rw-r--r--services/actions/workflow.go180
1 files changed, 57 insertions, 123 deletions
diff --git a/services/actions/workflow.go b/services/actions/workflow.go
index dc8a1dd349..233e22b5dd 100644
--- a/services/actions/workflow.go
+++ b/services/actions/workflow.go
@@ -5,9 +5,6 @@ package actions
import (
"fmt"
- "net/http"
- "net/url"
- "path"
"strings"
actions_model "code.gitea.io/gitea/models/actions"
@@ -31,61 +28,8 @@ import (
"github.com/nektos/act/pkg/model"
)
-func getActionWorkflowPath(commit *git.Commit) string {
- paths := []string{".gitea/workflows", ".github/workflows"}
- for _, treePath := range paths {
- if _, err := commit.SubTree(treePath); err == nil {
- return treePath
- }
- }
- return ""
-}
-
-func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
- cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
- cfg := cfgUnit.ActionsConfig()
-
- defaultBranch, _ := commit.GetBranchName()
-
- workflowURL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), url.PathEscape(entry.Name()))
- workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), util.PathEscapeSegments(defaultBranch), util.PathEscapeSegments(folder), url.PathEscape(entry.Name()))
- badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), url.PathEscape(entry.Name()), url.QueryEscape(ctx.Repo.Repository.DefaultBranch))
-
- // See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
- // State types:
- // - active
- // - deleted
- // - disabled_fork
- // - disabled_inactivity
- // - disabled_manually
- state := "active"
- if cfg.IsWorkflowDisabled(entry.Name()) {
- state = "disabled_manually"
- }
-
- // The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
- // by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
- // while the last commit would represent the modification date. The DeletedAt could be determined by identifying
- // the last commit where the file existed. However, this implementation has not been done here yet, as it would likely
- // cause a significant performance degradation.
- createdAt := commit.Author.When
- updatedAt := commit.Author.When
-
- return &api.ActionWorkflow{
- ID: entry.Name(),
- Name: entry.Name(),
- Path: path.Join(folder, entry.Name()),
- State: state,
- CreatedAt: createdAt,
- UpdatedAt: updatedAt,
- URL: workflowURL,
- HTMLURL: workflowRepoURL,
- BadgeURL: badgeURL,
- }
-}
-
func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
- workflow, err := GetActionWorkflow(ctx, workflowID)
+ workflow, err := convert.GetActionWorkflow(ctx, ctx.Repo.GitRepo, ctx.Repo.Repository, workflowID)
if err != nil {
return err
}
@@ -102,44 +46,6 @@ func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnabl
return repo_model.UpdateRepoUnit(ctx, cfgUnit)
}
-func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error) {
- defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
- if err != nil {
- ctx.APIErrorInternal(err)
- return nil, err
- }
-
- entries, err := actions.ListWorkflows(defaultBranchCommit)
- if err != nil {
- ctx.APIError(http.StatusNotFound, err.Error())
- return nil, err
- }
-
- folder := getActionWorkflowPath(defaultBranchCommit)
-
- workflows := make([]*api.ActionWorkflow, len(entries))
- for i, entry := range entries {
- workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, folder, entry)
- }
-
- return workflows, nil
-}
-
-func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionWorkflow, error) {
- entries, err := ListActionWorkflows(ctx)
- if err != nil {
- return nil, err
- }
-
- for _, entry := range entries {
- if entry.Name == workflowID {
- return entry, nil
- }
- }
-
- return nil, util.NewNotExistErrorf("workflow %q not found", workflowID)
-}
-
func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
if workflowID == "" {
return util.ErrorWrapLocale(
@@ -185,29 +91,62 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
}
// get workflow entry from runTargetCommit
- entries, err := actions.ListWorkflows(runTargetCommit)
+ _, entries, err := actions.ListWorkflows(runTargetCommit)
if err != nil {
return err
}
// find workflow from commit
var workflows []*jobparser.SingleWorkflow
- for _, entry := range entries {
- if entry.Name() != workflowID {
- continue
- }
+ var entry *git.TreeEntry
- content, err := actions.GetContentFromEntry(entry)
- if err != nil {
- return err
- }
- workflows, err = jobparser.Parse(content)
- if err != nil {
- return err
+ run := &actions_model.ActionRun{
+ Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
+ RepoID: repo.ID,
+ Repo: repo,
+ OwnerID: repo.OwnerID,
+ WorkflowID: workflowID,
+ TriggerUserID: doer.ID,
+ TriggerUser: doer,
+ Ref: string(refName),
+ CommitSHA: runTargetCommit.ID.String(),
+ IsForkPullRequest: false,
+ Event: "workflow_dispatch",
+ TriggerEvent: "workflow_dispatch",
+ Status: actions_model.StatusWaiting,
+ }
+
+ for _, e := range entries {
+ if e.Name() != workflowID {
+ continue
}
+ entry = e
break
}
+ if entry == nil {
+ return util.ErrorWrapLocale(
+ util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
+ "actions.workflow.not_found", workflowID,
+ )
+ }
+
+ content, err := actions.GetContentFromEntry(entry)
+ if err != nil {
+ return err
+ }
+
+ giteaCtx := GenerateGiteaContext(run, nil)
+
+ workflows, err = jobparser.Parse(content, jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
+ if err != nil {
+ return err
+ }
+
+ if len(workflows) > 0 && workflows[0].RunName != "" {
+ run.Title = workflows[0].RunName
+ }
+
if len(workflows) == 0 {
return util.ErrorWrapLocale(
util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
@@ -236,25 +175,12 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
Inputs: inputsWithDefaults,
Sender: convert.ToUserWithAccessMode(ctx, doer, perm.AccessModeNone),
}
+
var eventPayload []byte
if eventPayload, err = workflowDispatchPayload.JSONPayload(); err != nil {
return fmt.Errorf("JSONPayload: %w", err)
}
-
- run := &actions_model.ActionRun{
- Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
- RepoID: repo.ID,
- OwnerID: repo.OwnerID,
- WorkflowID: workflowID,
- TriggerUserID: doer.ID,
- Ref: string(refName),
- CommitSHA: runTargetCommit.ID.String(),
- IsForkPullRequest: false,
- Event: "workflow_dispatch",
- TriggerEvent: "workflow_dispatch",
- EventPayload: string(eventPayload),
- Status: actions_model.StatusWaiting,
- }
+ run.EventPayload = string(eventPayload)
// cancel running jobs of the same workflow
if err := CancelPreviousJobs(
@@ -277,9 +203,17 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
log.Error("FindRunJobs: %v", err)
}
CreateCommitStatus(ctx, allJobs...)
+ if len(allJobs) > 0 {
+ job := allJobs[0]
+ err := job.LoadRun(ctx)
+ if err != nil {
+ log.Error("LoadRun: %v", err)
+ } else {
+ notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
+ }
+ }
for _, job := range allJobs {
notify_service.WorkflowJobStatusUpdate(ctx, repo, doer, job, nil)
}
-
return nil
}