mirror of
https://github.com/go-gitea/gitea.git
synced 2026-06-14 03:29:55 +00:00
Backport #37088 Changes: - Make `GetActionWorkflow` only convert the target workflow - In `getActionWorkflowEntry`, use `branchName` instead of resolving the default branch name from `commit.GetBranchName()` - Add `ref` to `workflow_run` notify input to avoid the empty `ref` warning
This commit is contained in:
@@ -59,6 +59,10 @@ func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
|
|||||||
// Github "issues" event
|
// Github "issues" event
|
||||||
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
|
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
|
||||||
return true
|
return true
|
||||||
|
case webhook_module.HookEventWorkflowRun:
|
||||||
|
// GitHub "workflow_run" event
|
||||||
|
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -347,27 +347,6 @@ func (c *Commit) GetFileContent(filename string, limit int) (string, error) {
|
|||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
|
|
||||||
func (c *Commit) GetBranchName() (string, error) {
|
|
||||||
cmd := gitcmd.NewCommand("name-rev")
|
|
||||||
if DefaultFeatures().CheckVersionAtLeast("2.13.0") {
|
|
||||||
cmd.AddArguments("--exclude", "refs/tags/*")
|
|
||||||
}
|
|
||||||
cmd.AddArguments("--name-only", "--no-undefined").AddDynamicArguments(c.ID.String())
|
|
||||||
data, _, err := cmd.RunStdString(c.repo.Ctx, &gitcmd.RunOpts{Dir: c.repo.Path})
|
|
||||||
if err != nil {
|
|
||||||
// handle special case where git can not describe commit
|
|
||||||
if strings.Contains(err.Error(), "cannot describe") {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// name-rev commitID output will be "master" or "master~12"
|
|
||||||
return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommitFileStatus represents status of files in a commit.
|
// CommitFileStatus represents status of files in a commit.
|
||||||
type CommitFileStatus struct {
|
type CommitFileStatus struct {
|
||||||
Added []string
|
Added []string
|
||||||
|
|||||||
@@ -816,12 +816,14 @@ func (n *actionsNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *rep
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newNotifyInput(repo, sender, webhook_module.HookEventWorkflowRun).WithPayload(&api.WorkflowRunPayload{
|
newNotifyInput(repo, sender, webhook_module.HookEventWorkflowRun).
|
||||||
Action: status,
|
WithRef(git.RefNameFromBranch(repo.DefaultBranch).String()).
|
||||||
Workflow: convertedWorkflow,
|
WithPayload(&api.WorkflowRunPayload{
|
||||||
WorkflowRun: convertedRun,
|
Action: status,
|
||||||
Organization: org,
|
Workflow: convertedWorkflow,
|
||||||
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
WorkflowRun: convertedRun,
|
||||||
Sender: convert.ToUser(ctx, sender, nil),
|
Organization: org,
|
||||||
}).Notify(ctx)
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
||||||
|
Sender: convert.ToUser(ctx, sender, nil),
|
||||||
|
}).Notify(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -390,14 +390,12 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getActionWorkflowEntry(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
|
func getActionWorkflowEntry(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branchName, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
|
||||||
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
|
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
|
||||||
cfg := cfgUnit.ActionsConfig()
|
cfg := cfgUnit.ActionsConfig()
|
||||||
|
|
||||||
defaultBranch, _ := commit.GetBranchName()
|
|
||||||
|
|
||||||
workflowURL := fmt.Sprintf("%s/actions/workflows/%s", repo.APIURL(), util.PathEscapeSegments(entry.Name()))
|
workflowURL := fmt.Sprintf("%s/actions/workflows/%s", repo.APIURL(), util.PathEscapeSegments(entry.Name()))
|
||||||
workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", repo.HTMLURL(ctx), util.PathEscapeSegments(defaultBranch), util.PathEscapeSegments(folder), util.PathEscapeSegments(entry.Name()))
|
workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", repo.HTMLURL(ctx), util.PathEscapeSegments(branchName), util.PathEscapeSegments(folder), util.PathEscapeSegments(entry.Name()))
|
||||||
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", repo.HTMLURL(ctx), util.PathEscapeSegments(entry.Name()), url.QueryEscape(repo.DefaultBranch))
|
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", repo.HTMLURL(ctx), util.PathEscapeSegments(entry.Name()), url.QueryEscape(repo.DefaultBranch))
|
||||||
|
|
||||||
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
|
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
|
||||||
@@ -462,21 +460,26 @@ func ListActionWorkflows(ctx context.Context, gitrepo *git.Repository, repo *rep
|
|||||||
|
|
||||||
workflows := make([]*api.ActionWorkflow, len(entries))
|
workflows := make([]*api.ActionWorkflow, len(entries))
|
||||||
for i, entry := range entries {
|
for i, entry := range entries {
|
||||||
workflows[i] = getActionWorkflowEntry(ctx, repo, defaultBranchCommit, folder, entry)
|
workflows[i] = getActionWorkflowEntry(ctx, repo, defaultBranchCommit, repo.DefaultBranch, folder, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
return workflows, nil
|
return workflows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetActionWorkflow(ctx context.Context, gitrepo *git.Repository, repo *repo_model.Repository, workflowID string) (*api.ActionWorkflow, error) {
|
func GetActionWorkflow(ctx context.Context, gitrepo *git.Repository, repo *repo_model.Repository, workflowID string) (*api.ActionWorkflow, error) {
|
||||||
entries, err := ListActionWorkflows(ctx, gitrepo, repo)
|
defaultBranchCommit, err := gitrepo.GetBranchCommit(repo.DefaultBranch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
folder, entries, err := actions.ListWorkflows(defaultBranchCommit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.ID == workflowID {
|
if entry.Name() == workflowID {
|
||||||
return entry, nil
|
return getActionWorkflowEntry(ctx, repo, defaultBranchCommit, repo.DefaultBranch, folder, entry), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user