Add ref selector for run workflow from

This commit is contained in:
pangliang 2023-12-05 13:16:13 +08:00
parent d0a70d3414
commit 2d2f9218dc
4 changed files with 96 additions and 7 deletions

View File

@ -591,6 +591,7 @@ org_still_own_repo = "This organization still owns one or more repositories, del
org_still_own_packages = "This organization still owns one or more packages, delete them first."
target_branch_not_exist = Target branch does not exist.
target_ref_not_exist = Target ref does not exist %s
admin_cannot_delete_self = You cannot delete yourself when you are an admin. Please remove your admin privileges first.
@ -3597,6 +3598,7 @@ workflow.disabled = Workflow is disabled.
workflow.run = Run Workflow
workflow.not_found = Workflow '%s' not found.
workflow.run_success = Workflow '%s' run successfully.
workflow.from_ref = Use workflow from
need_approval_desc = Need approval to run workflows for fork pull request.

View File

@ -5,10 +5,14 @@ package actions
import (
"bytes"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook"
"fmt"
"net/http"
"slices"
"strings"
actions_model "code.gitea.io/gitea/models/actions"
@ -140,6 +144,30 @@ func List(ctx *context.Context) {
if event.Name == webhook_module.HookEventWorkflowDispatch.Event() {
ctx.Data["AllowTriggerWorkflowDispatchEvent"] = true
ctx.Data["WorkflowDispatchConfig"] = event.WorkflowDispatch()
branchOpts := git_model.FindBranchOptions{
RepoID: ctx.Repo.Repository.ID,
IsDeletedBranch: util.OptionalBoolFalse,
ListOptions: db.ListOptions{
ListAll: true,
},
}
branches, err := git_model.FindBranchNames(ctx, branchOpts)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
}
// always put default branch on the top if it exists
if slices.Contains(branches, ctx.Repo.Repository.DefaultBranch) {
branches = util.SliceRemoveAll(branches, ctx.Repo.Repository.DefaultBranch)
branches = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...)
}
ctx.Data["Branches"] = branches
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
if err == nil {
ctx.Data["Tags"] = tags
}
break
}
}

View File

@ -5,6 +5,7 @@ package actions
import (
"archive/zip"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"compress/gzip"
"context"
@ -689,6 +690,12 @@ func Run(ctx *context_module.Context) {
return
}
ref := ctx.FormString("ref")
if len(ref) == 0 {
ctx.ServerError("workflow", nil)
return
}
// can not rerun job when workflow is disabled
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
cfg := cfgUnit.ActionsConfig()
@ -698,13 +705,32 @@ func Run(ctx *context_module.Context) {
return
}
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
// 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 default branch commit
defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
entries, err := actions.ListWorkflows(commit)
entries, err := actions.ListWorkflows(defaultBranchCommit)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
@ -734,13 +760,13 @@ func Run(ctx *context_module.Context) {
}
run := &actions_model.ActionRun{
Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
RepoID: ctx.Repo.Repository.ID,
OwnerID: ctx.Repo.Repository.OwnerID,
WorkflowID: workflow,
TriggerUserID: ctx.Doer.ID,
Ref: ctx.Repo.Repository.DefaultBranch,
CommitSHA: commit.ID.String(),
Ref: ref,
CommitSHA: runTargetCommit.ID.String(),
IsForkPullRequest: false,
Event: webhook_module.HookEventWorkflowDispatch,
TriggerEvent: webhook_module.HookEventWorkflowDispatch.Event(),

View File

@ -6,6 +6,39 @@
<div class="content">
<form class="ui form" action="{{$.Link}}/run?workflow={{$.CurWorkflow}}&actor={{$.CurActor}}&status={{.Status}}" method="post">
{{.CsrfTokenHtml}}
<div class="ui inline field required">
<label>{{ctx.Locale.Tr "actions.workflow.from_ref"}} :</label>
<div class="ui floating dropdown icon button">
<input type="hidden" name="ref" value="refs/heads/{{index .Branches 0}}">
<i>{{svg "octicon-git-branch"}}</i>
<span class="text">{{index .Branches 0}}</span>
<div class="menu">
<div class="ui icon search input">
<i class="search icon"></i>
<input type="text" placeholder="{{ctx.Locale.Tr "repo.filter_branch_and_tag"}}">
</div>
<div class="scrolling menu">
<div class="header">
{{svg "octicon-git-branch"}}
Branches
</div>
{{range $key, $item := .Branches}}
<div class="item {{if eq $key 0}}active selected{{end}}" data-value="refs/heads/{{.}}">{{.}}</div>
{{end}}
<div class="divider"></div>
<div class="header">
{{svg "octicon-tag"}}
Tags
</div>
{{range $key, $item := .Tags}}
<div class="item" data-value="refs/tags/{{.}}">{{.}}</div>
{{end}}
</div>
</div>
</div>
</div>
<div class="divider"></div>
{{if .WorkflowDispatchConfig}}
{{range $key, $item := .WorkflowDispatchConfig.Inputs}}
<div class="ui field {{if .Required}}required{{end}}">
@ -29,7 +62,7 @@
</div>
{{end}}
{{end}}
<button class="ui tiny blue button">Submit</button>
<button class="ui tiny blue button" type="submit">Submit</button>
</form>
</div>
</div>