@@ -5,9 +5,9 @@ package repo
55
66import (
77 "errors"
8- "fmt"
98 "net/http"
109 "net/url"
10+ "strings"
1111
1212 actions_model "code.gitea.io/gitea/models/actions"
1313 "code.gitea.io/gitea/models/db"
@@ -589,16 +589,8 @@ func ListActionTasks(ctx *context.APIContext) {
589589 ctx .JSON (http .StatusOK , & res )
590590}
591591
592- // ActionWorkflow implements actions_service.WorkflowAPI
593- type ActionWorkflow struct {}
594-
595- // NewActionWorkflow creates a new ActionWorkflow service
596- func NewActionWorkflow () actions_service.WorkflowAPI {
597- return ActionWorkflow {}
598- }
599-
600- func (a ActionWorkflow ) ListRepositoryWorkflows (ctx * context.APIContext ) {
601- // swagger:operation GET /repos/{owner}/{repo}/actions/workflows repository ListRepositoryWorkflows
592+ func ActionsListRepositoryWorkflows (ctx * context.APIContext ) {
593+ // swagger:operation GET /repos/{owner}/{repo}/actions/workflows repository ActionsListRepositoryWorkflows
602594 // ---
603595 // summary: List repository workflows
604596 // produces:
@@ -637,8 +629,8 @@ func (a ActionWorkflow) ListRepositoryWorkflows(ctx *context.APIContext) {
637629 ctx .JSON (http .StatusOK , & api.ActionWorkflowResponse {Workflows : workflows , TotalCount : int64 (len (workflows ))})
638630}
639631
640- func ( a ActionWorkflow ) GetWorkflow (ctx * context.APIContext ) {
641- // swagger:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} repository GetWorkflow
632+ func ActionsGetWorkflow (ctx * context.APIContext ) {
633+ // swagger:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} repository ActionsGetWorkflow
642634 // ---
643635 // summary: Get a workflow
644636 // produces:
@@ -674,27 +666,21 @@ func (a ActionWorkflow) GetWorkflow(ctx *context.APIContext) {
674666 // "$ref": "#/responses/error"
675667
676668 workflowID := ctx .PathParam ("workflow_id" )
677- if len (workflowID ) == 0 {
678- ctx .Error (http .StatusUnprocessableEntity , "MissingWorkflowParameter" , util .NewInvalidArgumentErrorf ("workflow_id is required parameter" ))
679- return
680- }
681-
682669 workflow , err := actions_service .GetActionWorkflow (ctx , workflowID )
683670 if err != nil {
684- ctx .Error (http .StatusInternalServerError , "GetActionWorkflow" , err )
685- return
686- }
687-
688- if workflow == nil {
689- ctx .Error (http .StatusNotFound , "GetActionWorkflow" , err )
671+ if errors .Is (err , util .ErrNotExist ) {
672+ ctx .Error (http .StatusNotFound , "GetActionWorkflow" , err )
673+ } else {
674+ ctx .Error (http .StatusInternalServerError , "GetActionWorkflow" , err )
675+ }
690676 return
691677 }
692678
693679 ctx .JSON (http .StatusOK , workflow )
694680}
695681
696- func ( a ActionWorkflow ) DisableWorkflow (ctx * context.APIContext ) {
697- // swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable repository DisableWorkflow
682+ func ActionsDisableWorkflow (ctx * context.APIContext ) {
683+ // swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable repository ActionsDisableWorkflow
698684 // ---
699685 // summary: Disable a workflow
700686 // produces:
@@ -728,22 +714,21 @@ func (a ActionWorkflow) DisableWorkflow(ctx *context.APIContext) {
728714 // "$ref": "#/responses/validationError"
729715
730716 workflowID := ctx .PathParam ("workflow_id" )
731- if len (workflowID ) == 0 {
732- ctx .Error (http .StatusUnprocessableEntity , "MissingWorkflowParameter" , util .NewInvalidArgumentErrorf ("workflow_id is required parameter" ))
733- return
734- }
735-
736- err := actions_service .DisableActionWorkflow (ctx , workflowID )
717+ err := actions_service .EnableOrDisableWorkflow (ctx , workflowID , false )
737718 if err != nil {
738- ctx .Error (http .StatusInternalServerError , "DisableActionWorkflow" , err )
719+ if errors .Is (err , util .ErrNotExist ) {
720+ ctx .Error (http .StatusNotFound , "DisableActionWorkflow" , err )
721+ } else {
722+ ctx .Error (http .StatusInternalServerError , "DisableActionWorkflow" , err )
723+ }
739724 return
740725 }
741726
742727 ctx .Status (http .StatusNoContent )
743728}
744729
745- func ( a ActionWorkflow ) DispatchWorkflow (ctx * context.APIContext ) {
746- // swagger:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches repository DispatchWorkflow
730+ func ActionsDispatchWorkflow (ctx * context.APIContext ) {
731+ // swagger:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches repository ActionsDispatchWorkflow
747732 // ---
748733 // summary: Create a workflow dispatch event
749734 // produces:
@@ -780,60 +765,49 @@ func (a ActionWorkflow) DispatchWorkflow(ctx *context.APIContext) {
780765 // "422":
781766 // "$ref": "#/responses/validationError"
782767
783- opt := web .GetForm (ctx ).(* api.CreateActionWorkflowDispatch )
784-
785768 workflowID := ctx .PathParam ("workflow_id" )
786- if len (workflowID ) == 0 {
787- ctx .Error (http .StatusUnprocessableEntity , "MissingWorkflowParameter" , util .NewInvalidArgumentErrorf ("workflow_id is required parameter" ))
788- return
789- }
790-
791- ref := opt .Ref
792- if len (ref ) == 0 {
769+ opt := web .GetForm (ctx ).(* api.CreateActionWorkflowDispatch )
770+ if opt .Ref == "" {
793771 ctx .Error (http .StatusUnprocessableEntity , "MissingWorkflowParameter" , util .NewInvalidArgumentErrorf ("ref is required parameter" ))
794772 return
795773 }
796774
797- err := actions_service .DispatchActionWorkflow (& context.Context {
798- Base : ctx .Base ,
799- Doer : ctx .Doer ,
800- Repo : ctx .Repo ,
801- }, workflowID , ref , func (workflowDispatch * model.WorkflowDispatch , inputs * map [string ]any ) error {
802- if workflowDispatch != nil {
803- // TODO figure out why the inputs map is empty for url form encoding workaround
804- if opt .Inputs == nil {
805- for name , config := range workflowDispatch .Inputs {
806- value := ctx .FormString ("inputs[" + name + "]" , config .Default )
807- (* inputs )[name ] = value
808- }
809- } else {
810- for name , config := range workflowDispatch .Inputs {
811- value , ok := opt .Inputs [name ]
812- if ok {
813- (* inputs )[name ] = value
814- } else {
815- (* inputs )[name ] = config .Default
816- }
775+ 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 {
776+ if strings .Contains (ctx .Req .Header .Get ("Content-Type" ), "form-urlencoded" ) {
777+ // The chi framework's "Binding" doesn't support to bind the form map values into a map[string]string
778+ // So we have to manually read the `inputs[key]` from the form
779+ for name , config := range workflowDispatch .Inputs {
780+ value := ctx .FormString ("inputs[" + name + "]" , config .Default )
781+ inputs [name ] = value
782+ }
783+ } else {
784+ for name , config := range workflowDispatch .Inputs {
785+ value , ok := opt .Inputs [name ]
786+ if ok {
787+ inputs [name ] = value
788+ } else {
789+ inputs [name ] = config .Default
817790 }
818791 }
819792 }
820793 return nil
821794 })
822795 if err != nil {
823- if terr , ok := err .(* actions_service.TranslateableError ); ok {
824- msg := ctx .Locale .TrString (terr .Translation , terr .Args ... )
825- ctx .Error (terr .GetCode (), msg , fmt .Errorf ("%s" , msg ))
826- return
796+ if errors .Is (err , util .ErrNotExist ) {
797+ ctx .Error (http .StatusNotFound , "DispatchActionWorkflow" , err )
798+ } else if errors .Is (err , util .ErrPermissionDenied ) {
799+ ctx .Error (http .StatusForbidden , "DispatchActionWorkflow" , err )
800+ } else {
801+ ctx .Error (http .StatusInternalServerError , "DispatchActionWorkflow" , err )
827802 }
828- ctx .Error (http .StatusInternalServerError , err .Error (), err )
829803 return
830804 }
831805
832806 ctx .Status (http .StatusNoContent )
833807}
834808
835- func ( a ActionWorkflow ) EnableWorkflow (ctx * context.APIContext ) {
836- // swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable repository EnableWorkflow
809+ func ActionsEnableWorkflow (ctx * context.APIContext ) {
810+ // swagger:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable repository ActionsEnableWorkflow
837811 // ---
838812 // summary: Enable a workflow
839813 // produces:
@@ -869,14 +843,13 @@ func (a ActionWorkflow) EnableWorkflow(ctx *context.APIContext) {
869843 // "$ref": "#/responses/validationError"
870844
871845 workflowID := ctx .PathParam ("workflow_id" )
872- if len (workflowID ) == 0 {
873- ctx .Error (http .StatusUnprocessableEntity , "MissingWorkflowParameter" , util .NewInvalidArgumentErrorf ("workflow_id is required parameter" ))
874- return
875- }
876-
877- err := actions_service .EnableActionWorkflow (ctx , workflowID )
846+ err := actions_service .EnableOrDisableWorkflow (ctx , workflowID , true )
878847 if err != nil {
879- ctx .Error (http .StatusInternalServerError , "EnableActionWorkflow" , err )
848+ if errors .Is (err , util .ErrNotExist ) {
849+ ctx .Error (http .StatusNotFound , "EnableActionWorkflow" , err )
850+ } else {
851+ ctx .Error (http .StatusInternalServerError , "EnableActionWorkflow" , err )
852+ }
880853 return
881854 }
882855
0 commit comments