mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-30 19:15:23 +01:00 
			
		
		
		
	Feature: Support workflow event dispatch via API (#33545)
Fix: https://github.com/go-gitea/gitea/issues/31765 (Re-open #32059) --------- Co-authored-by: Bence Santha <git@santha.eu> Co-authored-by: Bence Sántha <7604637+bencurio@users.noreply.github.com> Co-authored-by: Christopher Homberger <christopher.homberger@web.de>
This commit is contained in:
		
							parent
							
								
									085f273d19
								
							
						
					
					
						commit
						30993e9508
					
				| @ -32,3 +32,36 @@ type ActionTaskResponse struct { | ||||
| 	Entries    []*ActionTask `json:"workflow_runs"` | ||||
| 	TotalCount int64         `json:"total_count"` | ||||
| } | ||||
| 
 | ||||
| // CreateActionWorkflowDispatch represents the payload for triggering a workflow dispatch event | ||||
| // swagger:model | ||||
| type CreateActionWorkflowDispatch struct { | ||||
| 	// required: true | ||||
| 	// example: refs/heads/main | ||||
| 	Ref string `json:"ref" binding:"Required"` | ||||
| 	// required: false | ||||
| 	Inputs map[string]string `json:"inputs,omitempty"` | ||||
| } | ||||
| 
 | ||||
| // ActionWorkflow represents a ActionWorkflow | ||||
| type ActionWorkflow struct { | ||||
| 	ID    string `json:"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,omitempty"` | ||||
| } | ||||
| 
 | ||||
| // ActionWorkflowResponse returns a ActionWorkflow | ||||
| type ActionWorkflowResponse struct { | ||||
| 	Workflows  []*ActionWorkflow `json:"workflows"` | ||||
| 	TotalCount int64             `json:"total_count"` | ||||
| } | ||||
|  | ||||
| @ -36,6 +36,22 @@ func (w SilentWrap) Unwrap() error { | ||||
| 	return w.Err | ||||
| } | ||||
| 
 | ||||
| type LocaleWrap struct { | ||||
| 	err    error | ||||
| 	TrKey  string | ||||
| 	TrArgs []any | ||||
| } | ||||
| 
 | ||||
| // Error returns the message | ||||
| func (w LocaleWrap) Error() string { | ||||
| 	return w.err.Error() | ||||
| } | ||||
| 
 | ||||
| // Unwrap returns the underlying error | ||||
| func (w LocaleWrap) Unwrap() error { | ||||
| 	return w.err | ||||
| } | ||||
| 
 | ||||
| // NewSilentWrapErrorf returns an error that formats as the given text but unwraps as the provided error | ||||
| func NewSilentWrapErrorf(unwrap error, message string, args ...any) error { | ||||
| 	if len(args) == 0 { | ||||
| @ -63,3 +79,16 @@ func NewAlreadyExistErrorf(message string, args ...any) error { | ||||
| func NewNotExistErrorf(message string, args ...any) error { | ||||
| 	return NewSilentWrapErrorf(ErrNotExist, message, args...) | ||||
| } | ||||
| 
 | ||||
| // ErrWrapLocale wraps an err with a translation key and arguments | ||||
| func ErrWrapLocale(err error, trKey string, trArgs ...any) error { | ||||
| 	return LocaleWrap{err: err, TrKey: trKey, TrArgs: trArgs} | ||||
| } | ||||
| 
 | ||||
| func ErrAsLocale(err error) *LocaleWrap { | ||||
| 	var e LocaleWrap | ||||
| 	if errors.As(err, &e) { | ||||
| 		return &e | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @ -1155,11 +1155,17 @@ func Routes() *web.Router { | ||||
| 					m.Post("/accept", repo.AcceptTransfer) | ||||
| 					m.Post("/reject", repo.RejectTransfer) | ||||
| 				}, reqToken()) | ||||
| 				addActionsRoutes( | ||||
| 					m, | ||||
| 					reqOwner(), | ||||
| 					repo.NewAction(), | ||||
| 				) | ||||
| 
 | ||||
| 				addActionsRoutes(m, reqOwner(), repo.NewAction()) // it adds the routes for secrets/variables and runner management | ||||
| 
 | ||||
| 				m.Group("/actions/workflows", func() { | ||||
| 					m.Get("", repo.ActionsListRepositoryWorkflows) | ||||
| 					m.Get("/{workflow_id}", repo.ActionsGetWorkflow) | ||||
| 					m.Put("/{workflow_id}/disable", reqRepoWriter(unit.TypeActions), repo.ActionsDisableWorkflow) | ||||
| 					m.Put("/{workflow_id}/enable", reqRepoWriter(unit.TypeActions), repo.ActionsEnableWorkflow) | ||||
| 					m.Post("/{workflow_id}/dispatches", reqRepoWriter(unit.TypeActions), bind(api.CreateActionWorkflowDispatch{}), repo.ActionsDispatchWorkflow) | ||||
| 				}, context.ReferencesGitRepo(), reqToken(), reqRepoReader(unit.TypeActions)) | ||||
| 
 | ||||
| 				m.Group("/hooks/git", func() { | ||||
| 					m.Combo("").Get(repo.ListGitHooks) | ||||
| 					m.Group("/{id}", func() { | ||||
|  | ||||
| @ -6,6 +6,7 @@ package repo | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	actions_model "code.gitea.io/gitea/models/actions" | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| @ -19,6 +20,8 @@ import ( | ||||
| 	"code.gitea.io/gitea/services/context" | ||||
| 	"code.gitea.io/gitea/services/convert" | ||||
| 	secret_service "code.gitea.io/gitea/services/secrets" | ||||
| 
 | ||||
| 	"github.com/nektos/act/pkg/model" | ||||
| ) | ||||
| 
 | ||||
| // ListActionsSecrets list an repo's actions secrets | ||||
| @ -581,3 +584,270 @@ func ListActionTasks(ctx *context.APIContext) { | ||||
| 
 | ||||
| 	ctx.JSON(http.StatusOK, &res) | ||||
| } | ||||
| 
 | ||||
| func ActionsListRepositoryWorkflows(ctx *context.APIContext) { | ||||
| 	// swagger:operation GET /repos/{owner}/{repo}/actions/workflows repository ActionsListRepositoryWorkflows | ||||
| 	// --- | ||||
| 	// 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" | ||||
| 	//   "422": | ||||
| 	//     "$ref": "#/responses/validationError" | ||||
| 	//   "500": | ||||
| 	//     "$ref": "#/responses/error" | ||||
| 
 | ||||
| 	workflows, err := actions_service.ListActionWorkflows(ctx) | ||||
| 	if err != nil { | ||||
| 		ctx.Error(http.StatusInternalServerError, "ListActionWorkflows", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	ctx.JSON(http.StatusOK, &api.ActionWorkflowResponse{Workflows: workflows, TotalCount: int64(len(workflows))}) | ||||
| } | ||||
| 
 | ||||
| func ActionsGetWorkflow(ctx *context.APIContext) { | ||||
| 	// swagger:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} repository ActionsGetWorkflow | ||||
| 	// --- | ||||
| 	// 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" | ||||
| 	//   "422": | ||||
| 	//     "$ref": "#/responses/validationError" | ||||
| 	//   "500": | ||||
| 	//     "$ref": "#/responses/error" | ||||
| 
 | ||||
| 	workflowID := ctx.PathParam("workflow_id") | ||||
| 	workflow, err := actions_service.GetActionWorkflow(ctx, workflowID) | ||||
| 	if err != nil { | ||||
| 		if errors.Is(err, util.ErrNotExist) { | ||||
| 			ctx.Error(http.StatusNotFound, "GetActionWorkflow", err) | ||||
| 		} else { | ||||
| 			ctx.Error(http.StatusInternalServerError, "GetActionWorkflow", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	ctx.JSON(http.StatusOK, workflow) | ||||
| } | ||||
| 
 | ||||
| func ActionsDisableWorkflow(ctx *context.APIContext) { | ||||
| 	// swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable repository ActionsDisableWorkflow | ||||
| 	// --- | ||||
| 	// summary: Disable 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: | ||||
| 	//   "204": | ||||
| 	//     description: No Content | ||||
| 	//   "400": | ||||
| 	//     "$ref": "#/responses/error" | ||||
| 	//   "403": | ||||
| 	//     "$ref": "#/responses/forbidden" | ||||
| 	//   "404": | ||||
| 	//     "$ref": "#/responses/notFound" | ||||
| 	//   "422": | ||||
| 	//     "$ref": "#/responses/validationError" | ||||
| 
 | ||||
| 	workflowID := ctx.PathParam("workflow_id") | ||||
| 	err := actions_service.EnableOrDisableWorkflow(ctx, workflowID, false) | ||||
| 	if err != nil { | ||||
| 		if errors.Is(err, util.ErrNotExist) { | ||||
| 			ctx.Error(http.StatusNotFound, "DisableActionWorkflow", err) | ||||
| 		} else { | ||||
| 			ctx.Error(http.StatusInternalServerError, "DisableActionWorkflow", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	ctx.Status(http.StatusNoContent) | ||||
| } | ||||
| 
 | ||||
| func ActionsDispatchWorkflow(ctx *context.APIContext) { | ||||
| 	// swagger:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches repository ActionsDispatchWorkflow | ||||
| 	// --- | ||||
| 	// 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" | ||||
| 	//   "422": | ||||
| 	//     "$ref": "#/responses/validationError" | ||||
| 
 | ||||
| 	workflowID := ctx.PathParam("workflow_id") | ||||
| 	opt := web.GetForm(ctx).(*api.CreateActionWorkflowDispatch) | ||||
| 	if opt.Ref == "" { | ||||
| 		ctx.Error(http.StatusUnprocessableEntity, "MissingWorkflowParameter", util.NewInvalidArgumentErrorf("ref is required parameter")) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	err := actions_service.DispatchActionWorkflow(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, workflowID, opt.Ref, func(workflowDispatch *model.WorkflowDispatch, inputs map[string]any) error { | ||||
| 		if strings.Contains(ctx.Req.Header.Get("Content-Type"), "form-urlencoded") { | ||||
| 			// The chi framework's "Binding" doesn't support to bind the form map values into a map[string]string | ||||
| 			// So we have to manually read the `inputs[key]` from the form | ||||
| 			for name, config := range workflowDispatch.Inputs { | ||||
| 				value := ctx.FormString("inputs["+name+"]", config.Default) | ||||
| 				inputs[name] = value | ||||
| 			} | ||||
| 		} else { | ||||
| 			for name, config := range workflowDispatch.Inputs { | ||||
| 				value, ok := opt.Inputs[name] | ||||
| 				if ok { | ||||
| 					inputs[name] = value | ||||
| 				} else { | ||||
| 					inputs[name] = config.Default | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		if errors.Is(err, util.ErrNotExist) { | ||||
| 			ctx.Error(http.StatusNotFound, "DispatchActionWorkflow", err) | ||||
| 		} else if errors.Is(err, util.ErrPermissionDenied) { | ||||
| 			ctx.Error(http.StatusForbidden, "DispatchActionWorkflow", err) | ||||
| 		} else { | ||||
| 			ctx.Error(http.StatusInternalServerError, "DispatchActionWorkflow", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	ctx.Status(http.StatusNoContent) | ||||
| } | ||||
| 
 | ||||
| func ActionsEnableWorkflow(ctx *context.APIContext) { | ||||
| 	// swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable repository ActionsEnableWorkflow | ||||
| 	// --- | ||||
| 	// summary: Enable 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: | ||||
| 	//   "204": | ||||
| 	//     description: No Content | ||||
| 	//   "400": | ||||
| 	//     "$ref": "#/responses/error" | ||||
| 	//   "403": | ||||
| 	//     "$ref": "#/responses/forbidden" | ||||
| 	//   "404": | ||||
| 	//     "$ref": "#/responses/notFound" | ||||
| 	//   "409": | ||||
| 	//     "$ref": "#/responses/conflict" | ||||
| 	//   "422": | ||||
| 	//     "$ref": "#/responses/validationError" | ||||
| 
 | ||||
| 	workflowID := ctx.PathParam("workflow_id") | ||||
| 	err := actions_service.EnableOrDisableWorkflow(ctx, workflowID, true) | ||||
| 	if err != nil { | ||||
| 		if errors.Is(err, util.ErrNotExist) { | ||||
| 			ctx.Error(http.StatusNotFound, "EnableActionWorkflow", err) | ||||
| 		} else { | ||||
| 			ctx.Error(http.StatusInternalServerError, "EnableActionWorkflow", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	ctx.Status(http.StatusNoContent) | ||||
| } | ||||
|  | ||||
| @ -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"` | ||||
| } | ||||
|  | ||||
| @ -211,6 +211,9 @@ type swaggerParameterBodies struct { | ||||
| 	// in:body | ||||
| 	RenameOrgOption api.RenameOrgOption | ||||
| 
 | ||||
| 	// in:body | ||||
| 	CreateActionWorkflowDispatch api.CreateActionWorkflowDispatch | ||||
| 
 | ||||
| 	// in:body | ||||
| 	UpdateVariableOption api.UpdateVariableOption | ||||
| } | ||||
|  | ||||
| @ -20,8 +20,6 @@ import ( | ||||
| 	actions_model "code.gitea.io/gitea/models/actions" | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	git_model "code.gitea.io/gitea/models/git" | ||||
| 	"code.gitea.io/gitea/models/perm" | ||||
| 	access_model "code.gitea.io/gitea/models/perm/access" | ||||
| 	repo_model "code.gitea.io/gitea/models/repo" | ||||
| 	"code.gitea.io/gitea/models/unit" | ||||
| 	"code.gitea.io/gitea/modules/actions" | ||||
| @ -30,16 +28,13 @@ import ( | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| 	"code.gitea.io/gitea/modules/setting" | ||||
| 	"code.gitea.io/gitea/modules/storage" | ||||
| 	api "code.gitea.io/gitea/modules/structs" | ||||
| 	"code.gitea.io/gitea/modules/templates" | ||||
| 	"code.gitea.io/gitea/modules/timeutil" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| 	"code.gitea.io/gitea/modules/web" | ||||
| 	actions_service "code.gitea.io/gitea/services/actions" | ||||
| 	context_module "code.gitea.io/gitea/services/context" | ||||
| 	"code.gitea.io/gitea/services/convert" | ||||
| 
 | ||||
| 	"github.com/nektos/act/pkg/jobparser" | ||||
| 	"github.com/nektos/act/pkg/model" | ||||
| 	"xorm.io/builder" | ||||
| ) | ||||
| @ -792,142 +787,28 @@ func Run(ctx *context_module.Context) { | ||||
| 		ctx.ServerError("ref", nil) | ||||
| 		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.Flash.Error(ctx.Tr("actions.workflow.disabled")) | ||||
| 		ctx.Redirect(redirectURL) | ||||
| 		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() { | ||||
| 		runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName()) | ||||
| 	} else { | ||||
| 		ctx.Flash.Error(ctx.Tr("form.git_ref_name_error", ref)) | ||||
| 		ctx.Redirect(redirectURL) | ||||
| 		return | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		ctx.Flash.Error(ctx.Tr("form.target_ref_not_exist", ref)) | ||||
| 		ctx.Redirect(redirectURL) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	// get workflow entry from runTargetCommit | ||||
| 	entries, err := actions.ListWorkflows(runTargetCommit) | ||||
| 	if err != nil { | ||||
| 		ctx.Error(http.StatusInternalServerError, err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	// 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, err.Error()) | ||||
| 				return | ||||
| 			} | ||||
| 			workflows, err = jobparser.Parse(content) | ||||
| 			if err != nil { | ||||
| 				ctx.ServerError("workflow", err) | ||||
| 				return | ||||
| 			} | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if len(workflows) == 0 { | ||||
| 		ctx.Flash.Error(ctx.Tr("actions.workflow.not_found", workflowID)) | ||||
| 		ctx.Redirect(redirectURL) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	// get inputs from post | ||||
| 	workflow := &model.Workflow{ | ||||
| 		RawOn: workflows[0].RawOn, | ||||
| 	} | ||||
| 	inputs := make(map[string]any) | ||||
| 	if workflowDispatch := workflow.WorkflowDispatchConfig(); workflowDispatch != nil { | ||||
| 	err := actions_service.DispatchActionWorkflow(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, workflowID, ref, func(workflowDispatch *model.WorkflowDispatch, inputs map[string]any) error { | ||||
| 		for name, config := range workflowDispatch.Inputs { | ||||
| 			value := ctx.Req.PostFormValue(name) | ||||
| 			if config.Type == "boolean" { | ||||
| 				// https://www.w3.org/TR/html401/interact/forms.html | ||||
| 				// https://stackoverflow.com/questions/11424037/do-checkbox-inputs-only-post-data-if-theyre-checked | ||||
| 				// Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. | ||||
| 				// A switch is "on" when the control element's checked attribute is set. | ||||
| 				// When a form is submitted, only "on" checkbox controls can become successful. | ||||
| 				inputs[name] = strconv.FormatBool(value == "on") | ||||
| 				inputs[name] = strconv.FormatBool(ctx.FormBool(name)) | ||||
| 			} else if value != "" { | ||||
| 				inputs[name] = value | ||||
| 			} else { | ||||
| 				inputs[name] = config.Default | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// ctx.Req.PostForm -> WorkflowDispatchPayload.Inputs -> ActionRun.EventPayload -> runner: ghc.Event | ||||
| 	// https://docs.github.com/en/actions/learn-github-actions/contexts#github-context | ||||
| 	// https://docs.github.com/en/webhooks/webhook-events-and-payloads#workflow_dispatch | ||||
| 	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.ServerError("JSONPayload", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	run := &actions_model.ActionRun{ | ||||
| 		Title:             strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0], | ||||
| 		RepoID:            ctx.Repo.Repository.ID, | ||||
| 		OwnerID:           ctx.Repo.Repository.OwnerID, | ||||
| 		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 { | ||||
| 		log.Error("CancelRunningJobs: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	// 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 := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID}) | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Error("FindRunJobs: %v", err) | ||||
| 		if errLocale := util.ErrAsLocale(err); errLocale != nil { | ||||
| 			ctx.Flash.Error(ctx.Tr(errLocale.TrKey, errLocale.TrArgs...)) | ||||
| 			ctx.Redirect(redirectURL) | ||||
| 		} else { | ||||
| 			ctx.ServerError("DispatchActionWorkflow", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 	actions_service.CreateCommitStatus(ctx, alljobs...) | ||||
| 
 | ||||
| 	ctx.Flash.Success(ctx.Tr("actions.workflow.run_success", workflowID)) | ||||
| 	ctx.Redirect(redirectURL) | ||||
|  | ||||
							
								
								
									
										281
									
								
								services/actions/workflow.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										281
									
								
								services/actions/workflow.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,281 @@ | ||||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||||
| // SPDX-License-Identifier: MIT | ||||
| 
 | ||||
| package actions | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	actions_model "code.gitea.io/gitea/models/actions" | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	"code.gitea.io/gitea/models/perm" | ||||
| 	access_model "code.gitea.io/gitea/models/perm/access" | ||||
| 	repo_model "code.gitea.io/gitea/models/repo" | ||||
| 	"code.gitea.io/gitea/models/unit" | ||||
| 	user_model "code.gitea.io/gitea/models/user" | ||||
| 	"code.gitea.io/gitea/modules/actions" | ||||
| 	"code.gitea.io/gitea/modules/git" | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| 	"code.gitea.io/gitea/modules/reqctx" | ||||
| 	api "code.gitea.io/gitea/modules/structs" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| 	"code.gitea.io/gitea/services/context" | ||||
| 	"code.gitea.io/gitea/services/convert" | ||||
| 
 | ||||
| 	"github.com/nektos/act/pkg/jobparser" | ||||
| 	"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) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions) | ||||
| 	cfg := cfgUnit.ActionsConfig() | ||||
| 
 | ||||
| 	if isEnable { | ||||
| 		cfg.EnableWorkflow(workflow.ID) | ||||
| 	} else { | ||||
| 		cfg.DisableWorkflow(workflow.ID) | ||||
| 	} | ||||
| 
 | ||||
| 	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.Error(http.StatusInternalServerError, "WorkflowDefaultBranchError", err.Error()) | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	entries, err := actions.ListWorkflows(defaultBranchCommit) | ||||
| 	if err != nil { | ||||
| 		ctx.Error(http.StatusNotFound, "WorkflowListNotFound", 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.ErrWrapLocale( | ||||
| 			util.NewNotExistErrorf("workflowID is empty"), | ||||
| 			"actions.workflow.not_found", workflowID, | ||||
| 		) | ||||
| 	} | ||||
| 
 | ||||
| 	if ref == "" { | ||||
| 		return util.ErrWrapLocale( | ||||
| 			util.NewNotExistErrorf("ref is empty"), | ||||
| 			"form.target_ref_not_exist", ref, | ||||
| 		) | ||||
| 	} | ||||
| 
 | ||||
| 	// can not rerun job when workflow is disabled | ||||
| 	cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions) | ||||
| 	cfg := cfgUnit.ActionsConfig() | ||||
| 	if cfg.IsWorkflowDisabled(workflowID) { | ||||
| 		return util.ErrWrapLocale( | ||||
| 			util.NewPermissionDeniedErrorf("workflow is disabled"), | ||||
| 			"actions.workflow.disabled", | ||||
| 		) | ||||
| 	} | ||||
| 
 | ||||
| 	// get target commit of run from specified ref | ||||
| 	refName := git.RefName(ref) | ||||
| 	var runTargetCommit *git.Commit | ||||
| 	var err error | ||||
| 	if refName.IsTag() { | ||||
| 		runTargetCommit, err = gitRepo.GetTagCommit(refName.TagName()) | ||||
| 	} else if refName.IsBranch() { | ||||
| 		runTargetCommit, err = gitRepo.GetBranchCommit(refName.BranchName()) | ||||
| 	} else { | ||||
| 		refName = git.RefNameFromBranch(ref) | ||||
| 		runTargetCommit, err = gitRepo.GetBranchCommit(ref) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return util.ErrWrapLocale( | ||||
| 			util.NewNotExistErrorf("ref %q doesn't exist", ref), | ||||
| 			"form.target_ref_not_exist", ref, | ||||
| 		) | ||||
| 	} | ||||
| 
 | ||||
| 	// get workflow entry from 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 | ||||
| 		} | ||||
| 
 | ||||
| 		content, err := actions.GetContentFromEntry(entry) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		workflows, err = jobparser.Parse(content) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		break | ||||
| 	} | ||||
| 
 | ||||
| 	if len(workflows) == 0 { | ||||
| 		return util.ErrWrapLocale( | ||||
| 			util.NewNotExistErrorf("workflow %q doesn't exist", workflowID), | ||||
| 			"actions.workflow.not_found", workflowID, | ||||
| 		) | ||||
| 	} | ||||
| 
 | ||||
| 	// get inputs from post | ||||
| 	workflow := &model.Workflow{ | ||||
| 		RawOn: workflows[0].RawOn, | ||||
| 	} | ||||
| 	inputsWithDefaults := make(map[string]any) | ||||
| 	if workflowDispatch := workflow.WorkflowDispatchConfig(); workflowDispatch != nil { | ||||
| 		if err = processInputs(workflowDispatch, inputsWithDefaults); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// ctx.Req.PostForm -> WorkflowDispatchPayload.Inputs -> ActionRun.EventPayload -> runner: ghc.Event | ||||
| 	// https://docs.github.com/en/actions/learn-github-actions/contexts#github-context | ||||
| 	// https://docs.github.com/en/webhooks/webhook-events-and-payloads#workflow_dispatch | ||||
| 	workflowDispatchPayload := &api.WorkflowDispatchPayload{ | ||||
| 		Workflow:   workflowID, | ||||
| 		Ref:        ref, | ||||
| 		Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeNone}), | ||||
| 		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, | ||||
| 	} | ||||
| 
 | ||||
| 	// cancel running jobs of the same workflow | ||||
| 	if err := actions_model.CancelPreviousJobs( | ||||
| 		ctx, | ||||
| 		run.RepoID, | ||||
| 		run.Ref, | ||||
| 		run.WorkflowID, | ||||
| 		run.Event, | ||||
| 	); err != nil { | ||||
| 		log.Error("CancelRunningJobs: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	// Insert the action run and its associated jobs into the database | ||||
| 	if err := actions_model.InsertRun(ctx, run, workflows); err != nil { | ||||
| 		return fmt.Errorf("InsertRun: %w", err) | ||||
| 	} | ||||
| 
 | ||||
| 	allJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID}) | ||||
| 	if err != nil { | ||||
| 		log.Error("FindRunJobs: %v", err) | ||||
| 	} | ||||
| 	CreateCommitStatus(ctx, allJobs...) | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| @ -22,6 +22,9 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| // APIContext is a specific context for API service | ||||
| // ATTENTION: This struct should never be manually constructed in routes/services, | ||||
| // it has many internal details which should be carefully prepared by the framework. | ||||
| // If it is abused, it would cause strange bugs like panic/resource-leak. | ||||
| type APIContext struct { | ||||
| 	*Base | ||||
| 
 | ||||
|  | ||||
| @ -23,6 +23,10 @@ type BaseContextKeyType struct{} | ||||
| 
 | ||||
| var BaseContextKey BaseContextKeyType | ||||
| 
 | ||||
| // Base is the base context for all web handlers | ||||
| // ATTENTION: This struct should never be manually constructed in routes/services, | ||||
| // it has many internal details which should be carefully prepared by the framework. | ||||
| // If it is abused, it would cause strange bugs like panic/resource-leak. | ||||
| type Base struct { | ||||
| 	reqctx.RequestContext | ||||
| 
 | ||||
|  | ||||
| @ -34,7 +34,10 @@ type Render interface { | ||||
| 	HTML(w io.Writer, status int, name templates.TplName, data any, templateCtx context.Context) error | ||||
| } | ||||
| 
 | ||||
| // Context represents context of a request. | ||||
| // Context represents context of a web request. | ||||
| // ATTENTION: This struct should never be manually constructed in routes/services, | ||||
| // it has many internal details which should be carefully prepared by the framework. | ||||
| // If it is abused, it would cause strange bugs like panic/resource-leak. | ||||
| type Context struct { | ||||
| 	*Base | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										356
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										356
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							| @ -4421,6 +4421,275 @@ | ||||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/repos/{owner}/{repo}/actions/workflows": { | ||||
|       "get": { | ||||
|         "produces": [ | ||||
|           "application/json" | ||||
|         ], | ||||
|         "tags": [ | ||||
|           "repository" | ||||
|         ], | ||||
|         "summary": "List repository workflows", | ||||
|         "operationId": "ActionsListRepositoryWorkflows", | ||||
|         "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" | ||||
|           }, | ||||
|           "422": { | ||||
|             "$ref": "#/responses/validationError" | ||||
|           }, | ||||
|           "500": { | ||||
|             "$ref": "#/responses/error" | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/repos/{owner}/{repo}/actions/workflows/{workflow_id}": { | ||||
|       "get": { | ||||
|         "produces": [ | ||||
|           "application/json" | ||||
|         ], | ||||
|         "tags": [ | ||||
|           "repository" | ||||
|         ], | ||||
|         "summary": "Get a workflow", | ||||
|         "operationId": "ActionsGetWorkflow", | ||||
|         "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" | ||||
|           }, | ||||
|           "422": { | ||||
|             "$ref": "#/responses/validationError" | ||||
|           }, | ||||
|           "500": { | ||||
|             "$ref": "#/responses/error" | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable": { | ||||
|       "put": { | ||||
|         "produces": [ | ||||
|           "application/json" | ||||
|         ], | ||||
|         "tags": [ | ||||
|           "repository" | ||||
|         ], | ||||
|         "summary": "Disable a workflow", | ||||
|         "operationId": "ActionsDisableWorkflow", | ||||
|         "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": { | ||||
|           "204": { | ||||
|             "description": "No Content" | ||||
|           }, | ||||
|           "400": { | ||||
|             "$ref": "#/responses/error" | ||||
|           }, | ||||
|           "403": { | ||||
|             "$ref": "#/responses/forbidden" | ||||
|           }, | ||||
|           "404": { | ||||
|             "$ref": "#/responses/notFound" | ||||
|           }, | ||||
|           "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": "ActionsDispatchWorkflow", | ||||
|         "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" | ||||
|           }, | ||||
|           "422": { | ||||
|             "$ref": "#/responses/validationError" | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable": { | ||||
|       "put": { | ||||
|         "produces": [ | ||||
|           "application/json" | ||||
|         ], | ||||
|         "tags": [ | ||||
|           "repository" | ||||
|         ], | ||||
|         "summary": "Enable a workflow", | ||||
|         "operationId": "ActionsEnableWorkflow", | ||||
|         "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": { | ||||
|           "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": [ | ||||
| @ -18680,6 +18949,56 @@ | ||||
|       }, | ||||
|       "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": "string", | ||||
|           "x-go-name": "ID" | ||||
|         }, | ||||
|         "name": { | ||||
|           "type": "string", | ||||
|           "x-go-name": "Name" | ||||
|         }, | ||||
|         "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": { | ||||
| @ -19688,6 +20007,28 @@ | ||||
|       }, | ||||
|       "x-go-package": "code.gitea.io/gitea/modules/structs" | ||||
|     }, | ||||
|     "CreateActionWorkflowDispatch": { | ||||
|       "description": "CreateActionWorkflowDispatch represents the payload for triggering a workflow dispatch event", | ||||
|       "type": "object", | ||||
|       "required": [ | ||||
|         "ref" | ||||
|       ], | ||||
|       "properties": { | ||||
|         "inputs": { | ||||
|           "type": "object", | ||||
|           "additionalProperties": { | ||||
|             "type": "string" | ||||
|           }, | ||||
|           "x-go-name": "Inputs" | ||||
|         }, | ||||
|         "ref": { | ||||
|           "type": "string", | ||||
|           "x-go-name": "Ref", | ||||
|           "example": "refs/heads/main" | ||||
|         } | ||||
|       }, | ||||
|       "x-go-package": "code.gitea.io/gitea/modules/structs" | ||||
|     }, | ||||
|     "CreateBranchProtectionOption": { | ||||
|       "description": "CreateBranchProtectionOption options for creating a branch protection", | ||||
|       "type": "object", | ||||
| @ -25687,6 +26028,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": { | ||||
|  | ||||
| @ -5,6 +5,7 @@ package integration | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| @ -22,6 +23,7 @@ import ( | ||||
| 	actions_module "code.gitea.io/gitea/modules/actions" | ||||
| 	"code.gitea.io/gitea/modules/git" | ||||
| 	"code.gitea.io/gitea/modules/gitrepo" | ||||
| 	"code.gitea.io/gitea/modules/json" | ||||
| 	"code.gitea.io/gitea/modules/setting" | ||||
| 	api "code.gitea.io/gitea/modules/structs" | ||||
| 	"code.gitea.io/gitea/modules/test" | ||||
| @ -72,9 +74,19 @@ func TestPullRequestTargetEvent(t *testing.T) { | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, baseRepo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation:     "create", | ||||
| 					TreePath:      ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader("name: test\non:\n  pull_request_target:\n    paths:\n      - 'file_*.txt'\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo helloworld\n"), | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   pull_request_target: | ||||
|     paths: | ||||
|       - 'file_*.txt' | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| @ -228,9 +240,19 @@ func TestSkipCI(t *testing.T) { | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation:     "create", | ||||
| 					TreePath:      ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader("name: test\non:\n  push:\n    branches: [master]\n  pull_request:\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo helloworld\n"), | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   push: | ||||
|     branches: [master] | ||||
|   pull_request: | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| @ -347,9 +369,17 @@ func TestCreateDeleteRefEvent(t *testing.T) { | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation:     "create", | ||||
| 					TreePath:      ".gitea/workflows/createdelete.yml", | ||||
| 					ContentReader: strings.NewReader("name: test\non:\n  [create,delete]\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo helloworld\n"), | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/createdelete.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   [create,delete] | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| @ -461,9 +491,18 @@ func TestPullRequestCommitStatusEvent(t *testing.T) { | ||||
| 		addWorkflow, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation:     "create", | ||||
| 					TreePath:      ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader("name: test\non:\n  pull_request:\n    types: [assigned, unassigned, labeled, unlabeled, opened, edited, closed, reopened, synchronize, milestoned, demilestoned, review_requested, review_request_removed]\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo helloworld\n"), | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/pr.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   pull_request: | ||||
|     types: [assigned, unassigned, labeled, unlabeled, opened, edited, closed, reopened, synchronize, milestoned, demilestoned, review_requested, review_request_removed] | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| @ -651,3 +690,681 @@ func insertFakeStatus(t *testing.T, repo *repo_model.Repository, sha, targetURL, | ||||
| 	}) | ||||
| 	assert.NoError(t, err) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowDispatchPublicApi(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-dispatch-event", | ||||
| 			Description:   "test workflow-dispatch ci event", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// Get the commit ID of the default branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch) | ||||
| 		assert.NoError(t, err) | ||||
| 		values := url.Values{} | ||||
| 		values.Set("ref", "main") | ||||
| 		req := NewRequestWithURLValues(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), values). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/main", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  branch.CommitID, | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowDispatchPublicApiWithInputs(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-dispatch-event", | ||||
| 			Description:   "test workflow-dispatch ci event", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch: { inputs: { myinput: { default: def }, myinput2: { default: def2 }, myinput3: { type: boolean, default: false } } } | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// Get the commit ID of the default branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch) | ||||
| 		assert.NoError(t, err) | ||||
| 		values := url.Values{} | ||||
| 		values.Set("ref", "main") | ||||
| 		values.Set("inputs[myinput]", "val0") | ||||
| 		values.Set("inputs[myinput3]", "true") | ||||
| 		req := NewRequestWithURLValues(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), values). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/main", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  branch.CommitID, | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 		dispatchPayload := &api.WorkflowDispatchPayload{} | ||||
| 		err = json.Unmarshal([]byte(run.EventPayload), dispatchPayload) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput2") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput3") | ||||
| 		assert.Equal(t, "val0", dispatchPayload.Inputs["myinput"]) | ||||
| 		assert.Equal(t, "def2", dispatchPayload.Inputs["myinput2"]) | ||||
| 		assert.Equal(t, "true", dispatchPayload.Inputs["myinput3"]) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowDispatchPublicApiJSON(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-dispatch-event", | ||||
| 			Description:   "test workflow-dispatch ci event", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch: { inputs: { myinput: { default: def }, myinput2: { default: def2 }, myinput3: { type: boolean, default: false } } } | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// Get the commit ID of the default branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch) | ||||
| 		assert.NoError(t, err) | ||||
| 		inputs := &api.CreateActionWorkflowDispatch{ | ||||
| 			Ref: "main", | ||||
| 			Inputs: map[string]string{ | ||||
| 				"myinput":  "val0", | ||||
| 				"myinput3": "true", | ||||
| 			}, | ||||
| 		} | ||||
| 
 | ||||
| 		req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), inputs). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/main", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  branch.CommitID, | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowDispatchPublicApiWithInputsJSON(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-dispatch-event", | ||||
| 			Description:   "test workflow-dispatch ci event", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch: { inputs: { myinput: { default: def }, myinput2: { default: def2 }, myinput3: { type: boolean, default: false } } } | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// Get the commit ID of the default branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch) | ||||
| 		assert.NoError(t, err) | ||||
| 		inputs := &api.CreateActionWorkflowDispatch{ | ||||
| 			Ref: "main", | ||||
| 			Inputs: map[string]string{ | ||||
| 				"myinput":  "val0", | ||||
| 				"myinput3": "true", | ||||
| 			}, | ||||
| 		} | ||||
| 		req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), inputs). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/main", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  branch.CommitID, | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 		dispatchPayload := &api.WorkflowDispatchPayload{} | ||||
| 		err = json.Unmarshal([]byte(run.EventPayload), dispatchPayload) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput2") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput3") | ||||
| 		assert.Equal(t, "val0", dispatchPayload.Inputs["myinput"]) | ||||
| 		assert.Equal(t, "def2", dispatchPayload.Inputs["myinput2"]) | ||||
| 		assert.Equal(t, "true", dispatchPayload.Inputs["myinput3"]) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowDispatchPublicApiWithInputsNonDefaultBranchJSON(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-dispatch-event", | ||||
| 			Description:   "test workflow-dispatch ci event", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err = files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "update", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch: { inputs: { myinput: { default: def }, myinput2: { default: def2 }, myinput3: { type: boolean, default: false } } } | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "dispatch", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		// Get the commit ID of the dispatch branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		commit, err := gitRepo.GetBranchCommit("dispatch") | ||||
| 		assert.NoError(t, err) | ||||
| 		inputs := &api.CreateActionWorkflowDispatch{ | ||||
| 			Ref: "refs/heads/dispatch", | ||||
| 			Inputs: map[string]string{ | ||||
| 				"myinput":  "val0", | ||||
| 				"myinput3": "true", | ||||
| 			}, | ||||
| 		} | ||||
| 		req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), inputs). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/dispatch", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  commit.ID.String(), | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 		dispatchPayload := &api.WorkflowDispatchPayload{} | ||||
| 		err = json.Unmarshal([]byte(run.EventPayload), dispatchPayload) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput2") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput3") | ||||
| 		assert.Equal(t, "val0", dispatchPayload.Inputs["myinput"]) | ||||
| 		assert.Equal(t, "def2", dispatchPayload.Inputs["myinput2"]) | ||||
| 		assert.Equal(t, "true", dispatchPayload.Inputs["myinput3"]) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestWorkflowApi(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, u *url.URL) { | ||||
| 		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 		session := loginUser(t, user2.Name) | ||||
| 		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 
 | ||||
| 		// create the repo | ||||
| 		repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ | ||||
| 			Name:          "workflow-api", | ||||
| 			Description:   "test workflow apis", | ||||
| 			AutoInit:      true, | ||||
| 			Gitignores:    "Go", | ||||
| 			License:       "MIT", | ||||
| 			Readme:        "Default", | ||||
| 			DefaultBranch: "main", | ||||
| 			IsPrivate:     false, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, repo) | ||||
| 
 | ||||
| 		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/workflows", repo.FullName())). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp := MakeRequest(t, req, http.StatusOK) | ||||
| 		workflows := &api.ActionWorkflowResponse{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflows) | ||||
| 		assert.Empty(t, workflows.Workflows) | ||||
| 
 | ||||
| 		// add workflow file to the repo | ||||
| 		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ | ||||
| 			Files: []*files_service.ChangeRepoFile{ | ||||
| 				{ | ||||
| 					Operation: "create", | ||||
| 					TreePath:  ".gitea/workflows/dispatch.yml", | ||||
| 					ContentReader: strings.NewReader(`name: test | ||||
| on: | ||||
|   workflow_dispatch: { inputs: { myinput: { default: def }, myinput2: { default: def2 }, myinput3: { type: boolean, default: false } } } | ||||
| jobs: | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: echo helloworld | ||||
| `), | ||||
| 				}, | ||||
| 			}, | ||||
| 			Message:   "add workflow", | ||||
| 			OldBranch: "main", | ||||
| 			NewBranch: "main", | ||||
| 			Author: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Committer: &files_service.IdentityOptions{ | ||||
| 				GitUserName:  user2.Name, | ||||
| 				GitUserEmail: user2.Email, | ||||
| 			}, | ||||
| 			Dates: &files_service.CommitDateOptions{ | ||||
| 				Author:    time.Now(), | ||||
| 				Committer: time.Now(), | ||||
| 			}, | ||||
| 		}) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotEmpty(t, addWorkflowToBaseResp) | ||||
| 
 | ||||
| 		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/workflows", repo.FullName())). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		json.NewDecoder(resp.Body).Decode(workflows) | ||||
| 		assert.Len(t, workflows.Workflows, 1) | ||||
| 		assert.Equal(t, "dispatch.yml", workflows.Workflows[0].Name) | ||||
| 		assert.Equal(t, ".gitea/workflows/dispatch.yml", workflows.Workflows[0].Path) | ||||
| 		assert.Equal(t, ".gitea/workflows/dispatch.yml", workflows.Workflows[0].Path) | ||||
| 		assert.Equal(t, "active", workflows.Workflows[0].State) | ||||
| 
 | ||||
| 		// Use a hardcoded api path | ||||
| 		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/%s", repo.FullName(), workflows.Workflows[0].ID)). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		workflow := &api.ActionWorkflow{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflow) | ||||
| 		assert.Equal(t, workflows.Workflows[0].ID, workflow.ID) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Path, workflow.Path) | ||||
| 		assert.Equal(t, workflows.Workflows[0].URL, workflow.URL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].HTMLURL, workflow.HTMLURL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Name, workflow.Name) | ||||
| 		assert.Equal(t, workflows.Workflows[0].State, workflow.State) | ||||
| 
 | ||||
| 		// Use the provided url instead of the hardcoded one | ||||
| 		req = NewRequest(t, "GET", workflows.Workflows[0].URL). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		workflow = &api.ActionWorkflow{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflow) | ||||
| 		assert.Equal(t, workflows.Workflows[0].ID, workflow.ID) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Path, workflow.Path) | ||||
| 		assert.Equal(t, workflows.Workflows[0].URL, workflow.URL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].HTMLURL, workflow.HTMLURL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Name, workflow.Name) | ||||
| 		assert.Equal(t, workflows.Workflows[0].State, workflow.State) | ||||
| 
 | ||||
| 		// Disable the workflow | ||||
| 		req = NewRequest(t, "PUT", workflows.Workflows[0].URL+"/disable"). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		// Use the provided url instead of the hardcoded one | ||||
| 		req = NewRequest(t, "GET", workflows.Workflows[0].URL). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		workflow = &api.ActionWorkflow{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflow) | ||||
| 		assert.Equal(t, workflows.Workflows[0].ID, workflow.ID) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Path, workflow.Path) | ||||
| 		assert.Equal(t, workflows.Workflows[0].URL, workflow.URL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].HTMLURL, workflow.HTMLURL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Name, workflow.Name) | ||||
| 		assert.Equal(t, "disabled_manually", workflow.State) | ||||
| 
 | ||||
| 		inputs := &api.CreateActionWorkflowDispatch{ | ||||
| 			Ref: "main", | ||||
| 			Inputs: map[string]string{ | ||||
| 				"myinput":  "val0", | ||||
| 				"myinput3": "true", | ||||
| 			}, | ||||
| 		} | ||||
| 		// Since the workflow is disabled, so the response code is 403 forbidden | ||||
| 		req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), inputs). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusForbidden) | ||||
| 
 | ||||
| 		// Enable the workflow again | ||||
| 		req = NewRequest(t, "PUT", workflows.Workflows[0].URL+"/enable"). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		// Use the provided url instead of the hardcoded one | ||||
| 		req = NewRequest(t, "GET", workflows.Workflows[0].URL). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		workflow = &api.ActionWorkflow{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflow) | ||||
| 		assert.Equal(t, workflows.Workflows[0].ID, workflow.ID) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Path, workflow.Path) | ||||
| 		assert.Equal(t, workflows.Workflows[0].URL, workflow.URL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].HTMLURL, workflow.HTMLURL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Name, workflow.Name) | ||||
| 		assert.Equal(t, workflows.Workflows[0].State, workflow.State) | ||||
| 
 | ||||
| 		req = NewRequest(t, "GET", workflows.Workflows[0].URL). | ||||
| 			AddTokenAuth(token) | ||||
| 		resp = MakeRequest(t, req, http.StatusOK) | ||||
| 		workflow = &api.ActionWorkflow{} | ||||
| 		json.NewDecoder(resp.Body).Decode(workflow) | ||||
| 		assert.Equal(t, workflows.Workflows[0].ID, workflow.ID) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Path, workflow.Path) | ||||
| 		assert.Equal(t, workflows.Workflows[0].URL, workflow.URL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].HTMLURL, workflow.HTMLURL) | ||||
| 		assert.Equal(t, workflows.Workflows[0].Name, workflow.Name) | ||||
| 		assert.Equal(t, workflows.Workflows[0].State, workflow.State) | ||||
| 
 | ||||
| 		// Get the commit ID of the default branch | ||||
| 		gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) | ||||
| 		assert.NoError(t, err) | ||||
| 		defer gitRepo.Close() | ||||
| 		branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch) | ||||
| 		assert.NoError(t, err) | ||||
| 		inputs = &api.CreateActionWorkflowDispatch{ | ||||
| 			Ref: "main", | ||||
| 			Inputs: map[string]string{ | ||||
| 				"myinput":  "val0", | ||||
| 				"myinput3": "true", | ||||
| 			}, | ||||
| 		} | ||||
| 		req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/workflows/dispatch.yml/dispatches", repo.FullName()), inputs). | ||||
| 			AddTokenAuth(token) | ||||
| 		_ = MakeRequest(t, req, http.StatusNoContent) | ||||
| 
 | ||||
| 		run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ | ||||
| 			Title:      "add workflow", | ||||
| 			RepoID:     repo.ID, | ||||
| 			Event:      "workflow_dispatch", | ||||
| 			Ref:        "refs/heads/main", | ||||
| 			WorkflowID: "dispatch.yml", | ||||
| 			CommitSHA:  branch.CommitID, | ||||
| 		}) | ||||
| 		assert.NotNil(t, run) | ||||
| 		dispatchPayload := &api.WorkflowDispatchPayload{} | ||||
| 		err = json.Unmarshal([]byte(run.EventPayload), dispatchPayload) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput2") | ||||
| 		assert.Contains(t, dispatchPayload.Inputs, "myinput3") | ||||
| 		assert.Equal(t, "val0", dispatchPayload.Inputs["myinput"]) | ||||
| 		assert.Equal(t, "def2", dispatchPayload.Inputs["myinput2"]) | ||||
| 		assert.Equal(t, "true", dispatchPayload.Inputs["myinput3"]) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user