Skip to content

Commit 8615c68

Browse files
committed
decouple Context (it should never manually construct any web/api Context)
1 parent 7853bc9 commit 8615c68

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

routers/api/v1/repo/action.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ func ActionsDisableWorkflow(ctx *context.APIContext) {
710710
// "$ref": "#/responses/validationError"
711711

712712
workflowID := ctx.PathParam("workflow_id")
713-
err := actions_service.DisableActionWorkflow(ctx, workflowID)
713+
err := actions_service.EnableOrDisableWorkflow(ctx, workflowID, false)
714714
if err != nil {
715715
if errors.Is(err, util.ErrNotExist) {
716716
ctx.Error(http.StatusNotFound, "DisableActionWorkflow", err)
@@ -768,11 +768,7 @@ func ActionsDispatchWorkflow(ctx *context.APIContext) {
768768
return
769769
}
770770

771-
err := actions_service.DispatchActionWorkflow(&context.Context{
772-
Base: ctx.Base,
773-
Doer: ctx.Doer,
774-
Repo: ctx.Repo,
775-
}, workflowID, opt.Ref, func(workflowDispatch *model.WorkflowDispatch, inputs map[string]any) error {
771+
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 {
776772
if strings.Contains(ctx.Req.Header.Get("Content-Type"), "form-urlencoded") {
777773
// The chi framework's "Binding" doesn't support to bind the form map values into a map[string]string
778774
// So we have to manually read the `inputs[key]` from the form
@@ -844,7 +840,7 @@ func ActionsEnableWorkflow(ctx *context.APIContext) {
844840
// "$ref": "#/responses/validationError"
845841

846842
workflowID := ctx.PathParam("workflow_id")
847-
err := actions_service.EnableActionWorkflow(ctx, workflowID)
843+
err := actions_service.EnableOrDisableWorkflow(ctx, workflowID, true)
848844
if err != nil {
849845
if errors.Is(err, util.ErrNotExist) {
850846
ctx.Error(http.StatusNotFound, "EnableActionWorkflow", err)

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ func Run(ctx *context_module.Context) {
787787
ctx.ServerError("ref", nil)
788788
return
789789
}
790-
err := actions_service.DispatchActionWorkflow(ctx, workflowID, ref, func(workflowDispatch *model.WorkflowDispatch, inputs map[string]any) error {
790+
err := actions_service.DispatchActionWorkflow(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, workflowID, ref, func(workflowDispatch *model.WorkflowDispatch, inputs map[string]any) error {
791791
for name, config := range workflowDispatch.Inputs {
792792
value := ctx.Req.PostFormValue(name)
793793
if config.Type == "boolean" {

services/actions/workflow.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import (
1616
access_model "code.gitea.io/gitea/models/perm/access"
1717
repo_model "code.gitea.io/gitea/models/repo"
1818
"code.gitea.io/gitea/models/unit"
19+
user_model "code.gitea.io/gitea/models/user"
1920
"code.gitea.io/gitea/modules/actions"
2021
"code.gitea.io/gitea/modules/git"
2122
"code.gitea.io/gitea/modules/log"
23+
"code.gitea.io/gitea/modules/reqctx"
2224
api "code.gitea.io/gitea/modules/structs"
2325
"code.gitea.io/gitea/modules/util"
2426
"code.gitea.io/gitea/services/context"
@@ -81,7 +83,7 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder
8183
}
8284
}
8385

84-
func disableOrEnableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
86+
func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
8587
workflow, err := GetActionWorkflow(ctx, workflowID)
8688
if err != nil {
8789
return err
@@ -137,11 +139,7 @@ func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionW
137139
return nil, util.NewNotExistErrorf("workflow %q not found", workflowID)
138140
}
139141

140-
func DisableActionWorkflow(ctx *context.APIContext, workflowID string) error {
141-
return disableOrEnableWorkflow(ctx, workflowID, false)
142-
}
143-
144-
func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
142+
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 {
145143
if workflowID == "" {
146144
return util.ErrWrapLocale(
147145
util.NewNotExistErrorf("workflowID is empty"),
@@ -157,7 +155,7 @@ func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, proces
157155
}
158156

159157
// can not rerun job when workflow is disabled
160-
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
158+
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
161159
cfg := cfgUnit.ActionsConfig()
162160
if cfg.IsWorkflowDisabled(workflowID) {
163161
return util.ErrWrapLocale(
@@ -171,12 +169,12 @@ func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, proces
171169
var runTargetCommit *git.Commit
172170
var err error
173171
if refName.IsTag() {
174-
runTargetCommit, err = ctx.Repo.GitRepo.GetTagCommit(refName.TagName())
172+
runTargetCommit, err = gitRepo.GetTagCommit(refName.TagName())
175173
} else if refName.IsBranch() {
176-
runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(refName.BranchName())
174+
runTargetCommit, err = gitRepo.GetBranchCommit(refName.BranchName())
177175
} else {
178176
refName = git.RefNameFromBranch(ref)
179-
runTargetCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ref)
177+
runTargetCommit, err = gitRepo.GetBranchCommit(ref)
180178
}
181179
if err != nil {
182180
return util.ErrWrapLocale(
@@ -233,9 +231,9 @@ func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, proces
233231
workflowDispatchPayload := &api.WorkflowDispatchPayload{
234232
Workflow: workflowID,
235233
Ref: ref,
236-
Repository: convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeNone}),
234+
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeNone}),
237235
Inputs: inputsWithDefaults,
238-
Sender: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone),
236+
Sender: convert.ToUserWithAccessMode(ctx, doer, perm.AccessModeNone),
239237
}
240238
var eventPayload []byte
241239
if eventPayload, err = workflowDispatchPayload.JSONPayload(); err != nil {
@@ -244,10 +242,10 @@ func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, proces
244242

245243
run := &actions_model.ActionRun{
246244
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
247-
RepoID: ctx.Repo.Repository.ID,
248-
OwnerID: ctx.Repo.Repository.OwnerID,
245+
RepoID: repo.ID,
246+
OwnerID: repo.OwnerID,
249247
WorkflowID: workflowID,
250-
TriggerUserID: ctx.Doer.ID,
248+
TriggerUserID: doer.ID,
251249
Ref: string(refName),
252250
CommitSHA: runTargetCommit.ID.String(),
253251
IsForkPullRequest: false,
@@ -281,7 +279,3 @@ func DispatchActionWorkflow(ctx *context.Context, workflowID, ref string, proces
281279

282280
return nil
283281
}
284-
285-
func EnableActionWorkflow(ctx *context.APIContext, workflowID string) error {
286-
return disableOrEnableWorkflow(ctx, workflowID, true)
287-
}

0 commit comments

Comments
 (0)