mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-10-30 17:34:13 +01:00
51735c415b
Closes #2797 I'm aware of https://github.com/go-gitea/gitea/pull/28163 exists, but since I had it laying around on my drive and collecting dust, I might as well open a PR for it if anyone wants the feature a bit sooner than waiting for upstream to release it or to be a forgejo "native" implementation. This PR Contains: - Support for the `workflow_dispatch` trigger - Inputs: boolean, string, number, choice Things still to be done: - [x] API Endpoint `/api/v1/<org>/<repo>/actions/workflows/<workflow id>/dispatches` - ~~Fixing some UI bugs I had no time figuring out, like why dropdown/choice inputs's menu's behave weirdly~~ Unrelated visual bug with dropdowns inside dropdowns - [x] Fix bug where opening the branch selection submits the form - [x] Limit on inputs to render/process Things not in this PR: - Inputs: environment (First need support for environments in forgejo) Things needed to test this: - A patch for https://code.forgejo.org/forgejo/runner to actually consider the inputs inside the workflow. ~~One possible patch can be seen here: https://code.forgejo.org/Mai-Lapyst/runner/src/branch/support-workflow-inputs~~ [PR](https://code.forgejo.org/forgejo/runner/pulls/199) ![image](/attachments/2db50c9e-898f-41cb-b698-43edeefd2573) ## Testing - Checkout PR - Setup new development runner with [this PR](https://code.forgejo.org/forgejo/runner/pulls/199) - Create a repo with a workflow (see below) - Go to the actions tab, select the workflow and see the notice as in the screenshot above - Use the button + dropdown to run the workflow - Try also running it via the api using the `` endpoint - ... - Profit! <details> <summary>Example workflow</summary> ```yaml on: workflow_dispatch: inputs: logLevel: description: 'Log Level' required: true default: 'warning' type: choice options: - info - warning - debug tags: description: 'Test scenario tags' required: false type: boolean boolean_default_true: description: 'Test scenario tags' required: true type: boolean default: true boolean_default_false: description: 'Test scenario tags' required: false type: boolean default: false number1_default: description: 'Number w. default' default: '100' type: number number2: description: 'Number w/o. default' type: number string1_default: description: 'String w. default' default: 'Hello world' type: string string2: description: 'String w/o. default' required: true type: string jobs: test: runs-on: docker steps: - uses: actions/checkout@v3 - run: whoami - run: cat /etc/issue - run: uname -a - run: date - run: echo ${{ inputs.logLevel }} - run: echo ${{ inputs.tags }} - env: GITHUB_CONTEXT: ${{ toJson(github) }} run: echo "$GITHUB_CONTEXT" - run: echo "abc" ``` </details> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3334 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org> Co-committed-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
171 lines
3.9 KiB
Go
171 lines
3.9 KiB
Go
// Copyright The Forgejo Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
"code.gitea.io/gitea/models/perm"
|
|
"code.gitea.io/gitea/models/perm/access"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/actions"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/modules/webhook"
|
|
"code.gitea.io/gitea/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) error {
|
|
content, err := actions.GetContentFromEntry(entry.GitEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
wf, err := act_model.ReadWorkflow(bytes.NewReader(content))
|
|
if err != nil {
|
|
return 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 InputRequiredErr{Name: name}
|
|
}
|
|
continue
|
|
}
|
|
} else {
|
|
switch input.Type {
|
|
case "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 errors.New("to many inputs")
|
|
}
|
|
|
|
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 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 err
|
|
}
|
|
|
|
jobs, err := jobparser.Parse(content, jobparser.WithVars(vars))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return actions_model.InsertRun(ctx, run, jobs)
|
|
}
|
|
|
|
func GetWorkflowFromCommit(gitRepo *git.Repository, ref, workflowID string) (*Workflow, error) {
|
|
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
|
|
}
|