0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-24 20:32:48 +01:00

Feature: Support workflow event dispatch via API

This commit is contained in:
Bence Santha 2024-09-17 15:10:34 +02:00
parent f528df944b
commit 8814e9f14c
6 changed files with 594 additions and 0 deletions

View file

@ -32,3 +32,36 @@ type ActionTaskResponse struct {
Entries []*ActionTask `json:"workflow_runs"`
TotalCount int64 `json:"total_count"`
}
// CreateActionWorkflowDispatch represents the data structure for dispatching a workflow action.
//
// swagger:model CreateActionWorkflowDispatch
type CreateActionWorkflowDispatch struct {
// required: true
Ref string `json:"ref"`
Inputs map[string]interface{} `json:"inputs"`
}
// ActionWorkflow represents a ActionWorkflow
type ActionWorkflow struct {
ID int64 `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Path string `json:"path"`
State string `json:"state"`
// swagger:strfmt date-time
CreatedAt time.Time `json:"created_at"`
// swagger:strfmt date-time
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
BadgeURL string `json:"badge_url"`
// swagger:strfmt date-time
DeletedAt time.Time `json:"deleted_at"`
}
// ActionWorkflowResponse returns a ActionWorkflow
type ActionWorkflowResponse struct {
Workflows []*ActionWorkflow `json:"workflows"`
TotalCount int64 `json:"total_count"`
}

View file

@ -864,6 +864,20 @@ func Routes() *web.Router {
})
}
addActionsWorkflowRoutes := func(
m *web.Router,
reqChecker func(ctx *context.APIContext),
actw actions.WorkflowAPI,
) {
m.Group("/actions", func() {
m.Group("/workflows", func() {
m.Get("", reqToken(), reqChecker, actw.ListRepositoryWorkflows)
m.Get("/{workflow_id}", reqToken(), reqChecker, actw.GetWorkflow)
m.Post("/{workflow_id}/dispatches", reqToken(), reqChecker, bind(api.CreateActionWorkflowDispatch{}), actw.DispatchWorkflow)
}, context.ReferencesGitRepo(), reqRepoWriter(unit.TypeCode))
})
}
m.Group("", func() {
// Miscellaneous (no scope required)
if setting.API.EnableSwagger {
@ -1107,6 +1121,11 @@ func Routes() *web.Router {
reqOwner(),
repo.NewAction(),
)
addActionsWorkflowRoutes(
m,
reqRepoWriter(unit.TypeCode),
repo.NewActionWorkflow(),
)
m.Group("/hooks/git", func() {
m.Combo("").Get(repo.ListGitHooks)
m.Group("/{id}", func() {

View file

@ -4,8 +4,17 @@
package repo
import (
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/git"
"errors"
"github.com/nektos/act/pkg/jobparser"
"github.com/nektos/act/pkg/model"
"net/http"
"strconv"
"strings"
actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
@ -581,3 +590,271 @@ func ListActionTasks(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, &res)
}
// ActionWorkflow implements actions_service.WorkflowAPI
type ActionWorkflow struct{}
// NewActionWorkflow creates a new ActionWorkflow service
func NewActionWorkflow() actions_service.WorkflowAPI {
return ActionWorkflow{}
}
func (a ActionWorkflow) ListRepositoryWorkflows(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/actions/workflows repository ListRepositoryWorkflows
// ---
// summary: List repository workflows
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActionWorkflowList"
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/conflict"
// "422":
// "$ref": "#/responses/validationError"
panic("implement me")
}
func (a ActionWorkflow) GetWorkflow(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} repository GetWorkflow
// ---
// summary: Get a workflow
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: workflow_id
// in: path
// description: id of the workflow
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActionWorkflow"
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/conflict"
// "422":
// "$ref": "#/responses/validationError"
panic("implement me")
}
func (a ActionWorkflow) DispatchWorkflow(ctx *context.APIContext) {
// swagger:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches repository DispatchWorkflow
// ---
// summary: Create a workflow dispatch event
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: workflow_id
// in: path
// description: id of the workflow
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/CreateActionWorkflowDispatch"
// responses:
// "204":
// description: No Content
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/conflict"
// "422":
// "$ref": "#/responses/validationError"
opt := web.GetForm(ctx).(*api.CreateActionWorkflowDispatch)
workflowID := ctx.PathParam("workflow_id")
if len(workflowID) == 0 {
ctx.Error(http.StatusUnprocessableEntity, "MissingWorkflowParameter", util.NewInvalidArgumentErrorf("workflow_id is required parameter"))
return
}
ref := opt.Ref
if len(ref) == 0 {
ctx.Error(http.StatusUnprocessableEntity, "MissingWorkflowParameter", util.NewInvalidArgumentErrorf("ref is required parameter"))
return
}
// can not rerun job when workflow is disabled
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
cfg := cfgUnit.ActionsConfig()
if cfg.IsWorkflowDisabled(workflowID) {
ctx.Error(http.StatusInternalServerError, "WorkflowDisabled", ctx.Tr("actions.workflow.disabled"))
return
}
// get target commit of run from specified ref
refName := git.RefName(ref)
var runTargetCommit *git.Commit
var err error
if refName.IsTag() {
runTargetCommit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
} else if refName.IsBranch() {
// [E] PANIC: runtime error: invalid memory address or nil pointer dereference
runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
} else {
ctx.Error(http.StatusInternalServerError, "WorkflowRefNameError", ctx.Tr("form.git_ref_name_error", ref))
return
}
if err != nil {
ctx.Error(http.StatusNotFound, "WorkflowRefNotFound", ctx.Tr("form.target_ref_not_exist", ref))
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, "WorkflowDefaultBranchError", err.Error())
return
}
entries, err := actions.ListWorkflows(defaultBranchCommit)
if err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowListError", err.Error())
}
// find workflow from commit
var workflows []*jobparser.SingleWorkflow
for _, entry := range entries {
if entry.Name() == workflowID {
content, err := actions.GetContentFromEntry(entry)
if err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowGetContentError", err.Error())
return
}
workflows, err = jobparser.Parse(content)
if err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowParseError", err.Error())
return
}
break
}
}
if len(workflows) == 0 {
ctx.Error(http.StatusNotFound, "WorkflowNotFound", ctx.Tr("actions.workflow.not_found", workflowID))
return
}
workflow := &model.Workflow{
RawOn: workflows[0].RawOn,
}
inputs := make(map[string]any)
if workflowDispatch := workflow.WorkflowDispatchConfig(); workflowDispatch != nil {
for name, config := range workflowDispatch.Inputs {
value, exists := opt.Inputs[name]
if !exists {
continue
}
if config.Type == "boolean" {
inputs[name] = strconv.FormatBool(value == "on")
} else if value != "" {
inputs[name] = value.(string)
} else {
inputs[name] = config.Default
}
}
}
workflowDispatchPayload := &api.WorkflowDispatchPayload{
Workflow: workflowID,
Ref: ref,
Repository: convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeNone}),
Inputs: inputs,
Sender: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone),
}
var eventPayload []byte
if eventPayload, err = workflowDispatchPayload.JSONPayload(); err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowDispatchJSONParseError", err.Error())
return
}
run := &actions_model.ActionRun{
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
RepoID: ctx.Repo.Repository.ID,
OwnerID: ctx.Repo.Repository.Owner.ID,
WorkflowID: workflowID,
TriggerUserID: ctx.Doer.ID,
Ref: ref,
CommitSHA: runTargetCommit.ID.String(),
IsForkPullRequest: false,
Event: "workflow_dispatch",
TriggerEvent: "workflow_dispatch",
EventPayload: string(eventPayload),
Status: actions_model.StatusWaiting,
}
// cancel running jobs of the same workflow
if err := actions_model.CancelPreviousJobs(
ctx,
run.RepoID,
run.Ref,
run.WorkflowID,
run.Event,
); err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowCancelPreviousJobsError", err.Error())
return
}
if err := actions_model.InsertRun(ctx, run, workflows); err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowInsertRunError", err.Error())
return
}
alljobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
if err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowFindRunJobError", err.Error())
return
}
actions_service.CreateCommitStatus(ctx, alljobs...)
ctx.Status(http.StatusNoContent)
}

View file

@ -32,3 +32,17 @@ type swaggerResponseVariableList struct {
// in:body
Body []api.ActionVariable `json:"body"`
}
// ActionWorkflow
// swagger:response ActionWorkflow
type swaggerResponseActionWorkflow struct {
// in:body
Body api.ActionWorkflow `json:"body"`
}
// ActionWorkflowList
// swagger:response ActionWorkflowList
type swaggerResponseActionWorkflowList struct {
// in:body
Body []api.ActionWorkflow `json:"body"`
}

View file

@ -0,0 +1,16 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import "code.gitea.io/gitea/services/context"
// WorkflowAPI for action workflow of a repository
type WorkflowAPI interface {
// ListRepositoryWorkflows list repository workflows
ListRepositoryWorkflows(*context.APIContext)
// GetWorkflow get a workflow
GetWorkflow(*context.APIContext)
// DispatchWorkflow create a workflow dispatch event
DispatchWorkflow(*context.APIContext)
}

View file

@ -4345,6 +4345,171 @@
}
}
},
"/repos/{owner}/{repo}/actions/workflows": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "List repository workflows",
"operationId": "ListRepositoryWorkflows",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/ActionWorkflowList"
},
"400": {
"$ref": "#/responses/error"
},
"403": {
"$ref": "#/responses/forbidden"
},
"404": {
"$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/conflict"
},
"422": {
"$ref": "#/responses/validationError"
}
}
}
},
"/repos/{owner}/{repo}/actions/workflows/{workflow_id}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a workflow",
"operationId": "GetWorkflow",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "id of the workflow",
"name": "workflow_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/ActionWorkflow"
},
"400": {
"$ref": "#/responses/error"
},
"403": {
"$ref": "#/responses/forbidden"
},
"404": {
"$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/conflict"
},
"422": {
"$ref": "#/responses/validationError"
}
}
}
},
"/repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches": {
"post": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Create a workflow dispatch event",
"operationId": "DispatchWorkflow",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "id of the workflow",
"name": "workflow_id",
"in": "path",
"required": true
},
{
"name": "body",
"in": "body",
"schema": {
"$ref": "#/definitions/CreateActionWorkflowDispatch"
}
}
],
"responses": {
"204": {
"description": "No Content"
},
"400": {
"$ref": "#/responses/error"
},
"403": {
"$ref": "#/responses/forbidden"
},
"404": {
"$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/conflict"
},
"422": {
"$ref": "#/responses/validationError"
}
}
}
},
"/repos/{owner}/{repo}/activities/feeds": {
"get": {
"produces": [
@ -18376,6 +18541,61 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ActionWorkflow": {
"description": "ActionWorkflow represents a ActionWorkflow",
"type": "object",
"properties": {
"badge_url": {
"type": "string",
"x-go-name": "BadgeURL"
},
"created_at": {
"type": "string",
"format": "date-time",
"x-go-name": "CreatedAt"
},
"deleted_at": {
"type": "string",
"format": "date-time",
"x-go-name": "DeletedAt"
},
"html_url": {
"type": "string",
"x-go-name": "HTMLURL"
},
"id": {
"type": "integer",
"format": "int64",
"x-go-name": "ID"
},
"name": {
"type": "string",
"x-go-name": "Name"
},
"node_id": {
"type": "string",
"x-go-name": "NodeID"
},
"path": {
"type": "string",
"x-go-name": "Path"
},
"state": {
"type": "string",
"x-go-name": "State"
},
"updated_at": {
"type": "string",
"format": "date-time",
"x-go-name": "UpdatedAt"
},
"url": {
"type": "string",
"x-go-name": "URL"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Activity": {
"type": "object",
"properties": {
@ -25268,6 +25488,21 @@
"$ref": "#/definitions/ActionVariable"
}
},
"ActionWorkflow": {
"description": "ActionWorkflow",
"schema": {
"$ref": "#/definitions/ActionWorkflow"
}
},
"ActionWorkflowList": {
"description": "ActionWorkflowList",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/ActionWorkflow"
}
}
},
"ActivityFeedsList": {
"description": "ActivityFeedsList",
"schema": {