mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-19 17:01:12 +00:00
Some checks failed
testing / frontend-checks (push) Has been skipped
testing / backend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / security-check (push) Has been skipped
/ release (push) Has been cancelled
**Backport: https://codeberg.org/forgejo/forgejo/pulls/8864**
The status of two jobs by the same name shadow each other, they need to be distinct. If two jobs by the same name are found, they are made distinct by adding a -<occurence number> suffix.
Resolves forgejo/forgejo#8648
(cherry picked from commit 6bc1803c70
)
```
Conflicts:
services/actions/notifier_helper.go
services/actions/schedule_tasks.go
services/actions/workflows.go
trivial context conflicts
services/actions/job_parser.go
use "github.com/nektos/act/pkg/jobparser"
```
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8884
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
176 lines
4.1 KiB
Go
176 lines
4.1 KiB
Go
// Copyright The Forgejo Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/models/perm"
|
|
"forgejo.org/models/perm/access"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/user"
|
|
"forgejo.org/modules/actions"
|
|
"forgejo.org/modules/git"
|
|
"forgejo.org/modules/json"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/structs"
|
|
"forgejo.org/modules/util"
|
|
"forgejo.org/modules/webhook"
|
|
"forgejo.org/services/convert"
|
|
|
|
"github.com/nektos/act/pkg/jobparser"
|
|
act_model "github.com/nektos/act/pkg/model"
|
|
)
|
|
|
|
type InputRequiredErr struct {
|
|
Name string
|
|
}
|
|
|
|
func (err InputRequiredErr) Error() string {
|
|
return fmt.Sprintf("input required for '%s'", err.Name)
|
|
}
|
|
|
|
func IsInputRequiredErr(err error) bool {
|
|
_, ok := err.(InputRequiredErr)
|
|
return ok
|
|
}
|
|
|
|
type Workflow struct {
|
|
WorkflowID string
|
|
Ref string
|
|
Commit *git.Commit
|
|
GitEntry *git.TreeEntry
|
|
}
|
|
|
|
type InputValueGetter func(key string) string
|
|
|
|
func (entry *Workflow) Dispatch(ctx context.Context, inputGetter InputValueGetter, repo *repo_model.Repository, doer *user.User) (r *actions_model.ActionRun, j []string, err error) {
|
|
content, err := actions.GetContentFromEntry(entry.GitEntry)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
wf, err := act_model.ReadWorkflow(bytes.NewReader(content))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
fullWorkflowID := ".forgejo/workflows/" + entry.WorkflowID
|
|
|
|
title := wf.Name
|
|
if len(title) < 1 {
|
|
title = fullWorkflowID
|
|
}
|
|
|
|
inputs := make(map[string]string)
|
|
if workflowDispatch := wf.WorkflowDispatchConfig(); workflowDispatch != nil {
|
|
for key, input := range workflowDispatch.Inputs {
|
|
val := inputGetter(key)
|
|
if len(val) == 0 {
|
|
val = input.Default
|
|
if len(val) == 0 {
|
|
if input.Required {
|
|
name := input.Description
|
|
if len(name) == 0 {
|
|
name = key
|
|
}
|
|
return nil, nil, InputRequiredErr{Name: name}
|
|
}
|
|
continue
|
|
}
|
|
} else if input.Type == "boolean" {
|
|
// Since "boolean" inputs are rendered as a checkbox in html, the value inside the form is "on"
|
|
val = strconv.FormatBool(val == "on")
|
|
}
|
|
inputs[key] = val
|
|
}
|
|
}
|
|
|
|
if int64(len(inputs)) > setting.Actions.LimitDispatchInputs {
|
|
return nil, nil, errors.New("to many inputs")
|
|
}
|
|
|
|
jobNames := util.KeysOfMap(wf.Jobs)
|
|
|
|
payload := &structs.WorkflowDispatchPayload{
|
|
Inputs: inputs,
|
|
Ref: entry.Ref,
|
|
Repository: convert.ToRepo(ctx, repo, access.Permission{AccessMode: perm.AccessModeNone}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Workflow: fullWorkflowID,
|
|
}
|
|
|
|
p, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
run := &actions_model.ActionRun{
|
|
Title: title,
|
|
RepoID: repo.ID,
|
|
Repo: repo,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: entry.WorkflowID,
|
|
TriggerUserID: doer.ID,
|
|
TriggerUser: doer,
|
|
Ref: entry.Ref,
|
|
CommitSHA: entry.Commit.ID.String(),
|
|
Event: webhook.HookEventWorkflowDispatch,
|
|
EventPayload: string(p),
|
|
TriggerEvent: string(webhook.HookEventWorkflowDispatch),
|
|
Status: actions_model.StatusWaiting,
|
|
}
|
|
|
|
vars, err := actions_model.GetVariablesOfRun(ctx, run)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
jobs, err := jobParser(content, jobparser.WithVars(vars))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return run, jobNames, actions_model.InsertRun(ctx, run, jobs)
|
|
}
|
|
|
|
func GetWorkflowFromCommit(gitRepo *git.Repository, ref, workflowID string) (*Workflow, error) {
|
|
ref, err := gitRepo.ExpandRef(ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commit, err := gitRepo.GetCommit(ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
entries, err := actions.ListWorkflows(commit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var workflowEntry *git.TreeEntry
|
|
for _, entry := range entries {
|
|
if entry.Name() == workflowID {
|
|
workflowEntry = entry
|
|
break
|
|
}
|
|
}
|
|
if workflowEntry == nil {
|
|
return nil, errors.New("workflow not found")
|
|
}
|
|
|
|
return &Workflow{
|
|
WorkflowID: workflowID,
|
|
Ref: ref,
|
|
Commit: commit,
|
|
GitEntry: workflowEntry,
|
|
}, nil
|
|
}
|