From a4a567f29f69e39d57a9fa7008d708f7fd080e58 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 22 Aug 2023 11:30:02 +0900 Subject: [PATCH] Check disabled workflow when rerun jobs (#26535) In GitHub, we can not rerun jobs if the workflow is disabled. --------- Co-authored-by: silverwind Co-authored-by: wxiaoguang --- options/locale/locale_en-US.ini | 1 + routers/web/repo/actions/view.go | 30 +++--- routers/web/web.go | 4 +- templates/base/head_script.tmpl | 2 + web_src/js/bootstrap.js | 10 +- web_src/js/components/RepoActionView.vue | 15 +-- web_src/js/features/common-global.js | 116 +++++++++-------------- 7 files changed, 77 insertions(+), 101 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 4ba7608aaa..587a2b14bc 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3503,6 +3503,7 @@ workflow.disable = Disable Workflow workflow.disable_success = Workflow '%s' disabled successfully. workflow.enable = Enable Workflow workflow.enable_success = Workflow '%s' enabled successfully. +workflow.disabled = Workflow is disabled. need_approval_desc = Need approval to run workflows for fork pull request. diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index af2ec21e4b..e4ca6a7198 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -259,31 +259,35 @@ func ViewPost(ctx *context_module.Context) { ctx.JSON(http.StatusOK, resp) } -func RerunOne(ctx *context_module.Context) { +// Rerun will rerun jobs in the given run +// jobIndex = 0 means rerun all jobs +func Rerun(ctx *context_module.Context) { runIndex := ctx.ParamsInt64("run") jobIndex := ctx.ParamsInt64("job") - job, _ := getRunJobs(ctx, runIndex, jobIndex) - if ctx.Written() { - return - } - - if err := rerunJob(ctx, job); err != nil { + run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) + if err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) return } - ctx.JSON(http.StatusOK, struct{}{}) -} + // can not rerun job when workflow is disabled + cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions) + cfg := cfgUnit.ActionsConfig() + if cfg.IsWorkflowDisabled(run.WorkflowID) { + ctx.JSONError(ctx.Locale.Tr("actions.workflow.disabled")) + return + } -func RerunAll(ctx *context_module.Context) { - runIndex := ctx.ParamsInt64("run") - - _, jobs := getRunJobs(ctx, runIndex, 0) + job, jobs := getRunJobs(ctx, runIndex, jobIndex) if ctx.Written() { return } + if jobIndex != 0 { + jobs = []*actions_model.ActionRunJob{job} + } + for _, j := range jobs { if err := rerunJob(ctx, j); err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) diff --git a/routers/web/web.go b/routers/web/web.go index e70e360d59..bbab9b37b5 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1211,14 +1211,14 @@ func registerRoutes(m *web.Route) { m.Combo(""). Get(actions.View). Post(web.Bind(actions.ViewRequest{}), actions.ViewPost) - m.Post("/rerun", reqRepoActionsWriter, actions.RerunOne) + m.Post("/rerun", reqRepoActionsWriter, actions.Rerun) m.Get("/logs", actions.Logs) }) m.Post("/cancel", reqRepoActionsWriter, actions.Cancel) m.Post("/approve", reqRepoActionsWriter, actions.Approve) m.Post("/artifacts", actions.ArtifactsView) m.Get("/artifacts/{artifact_name}", actions.ArtifactsDownloadView) - m.Post("/rerun", reqRepoActionsWriter, actions.RerunAll) + m.Post("/rerun", reqRepoActionsWriter, actions.Rerun) }) }, reqRepoActionsReader, actions.MustEnableActions) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index c7477ff4c0..03916fa637 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -4,7 +4,9 @@ If you are customizing Gitea, please do not change this file. If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. */}}