mirror of
https://github.com/go-gitea/gitea.git
synced 2026-06-17 19:10:22 +03:00
feat(api): Add GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs (#37196)
- Add GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
endpoint, matching the
https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2026-03-10#list-workflow-runs-for-a-workflow
---------
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: bircni <bircni@icloud.com>
This commit is contained in:
@@ -818,7 +818,7 @@ func (n *actionsNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *rep
|
||||
return
|
||||
}
|
||||
run.Repo = repo
|
||||
convertedRun, err := convert.ToActionWorkflowRun(ctx, run, nil)
|
||||
convertedRun, err := convert.ToActionWorkflowRun(ctx, run, nil, false)
|
||||
if err != nil {
|
||||
log.Error("ToActionWorkflowRun: %v", err)
|
||||
return
|
||||
|
||||
@@ -121,7 +121,7 @@ func TestToActionWorkflowRun_UsesTriggerEvent(t *testing.T) {
|
||||
run.Event = "push"
|
||||
run.TriggerEvent = "schedule"
|
||||
|
||||
apiRun, err := ToActionWorkflowRun(t.Context(), run, nil)
|
||||
apiRun, err := ToActionWorkflowRun(t.Context(), run, nil, false)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "schedule", apiRun.Event)
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/util"
|
||||
webhook_module "gitea.dev/modules/webhook"
|
||||
asymkey_service "gitea.dev/services/asymkey"
|
||||
"gitea.dev/services/gitdiff"
|
||||
|
||||
@@ -256,11 +257,8 @@ func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.Action
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ToActionWorkflowRun(ctx context.Context, run *actions_model.ActionRun, attempt *actions_model.ActionRunAttempt) (_ *api.ActionWorkflowRun, err error) {
|
||||
if err := run.LoadRepo(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := run.LoadTriggerUser(ctx); err != nil {
|
||||
func ToActionWorkflowRun(ctx context.Context, run *actions_model.ActionRun, attempt *actions_model.ActionRunAttempt, excludePullRequests bool) (_ *api.ActionWorkflowRun, err error) {
|
||||
if err := run.LoadAttributes(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -293,7 +291,15 @@ func ToActionWorkflowRun(ctx context.Context, run *actions_model.ActionRun, atte
|
||||
completedAt = attempt.Stopped.AsLocalTime()
|
||||
triggerUser = attempt.TriggerUser
|
||||
if attempt.Attempt > 1 {
|
||||
previousAttemptURL = new(fmt.Sprintf("%s/actions/runs/%d/attempts/%d", run.Repo.APIURL(ctx), run.ID, attempt.Attempt-1))
|
||||
url := fmt.Sprintf("%s/actions/runs/%d/attempts/%d", run.Repo.APIURL(ctx), run.ID, attempt.Attempt-1)
|
||||
previousAttemptURL = &url
|
||||
}
|
||||
}
|
||||
pullRequests := []*api.PullRequestMinimal{}
|
||||
if !excludePullRequests {
|
||||
pullRequests, err = loadPullRequestsForRun(ctx, run)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,6 +322,89 @@ func ToActionWorkflowRun(ctx context.Context, run *actions_model.ActionRun, atte
|
||||
Repository: ToRepo(ctx, run.Repo, access_model.Permission{AccessMode: perm.AccessModeNone}),
|
||||
TriggerActor: ToUser(ctx, triggerUser, nil),
|
||||
Actor: ToUser(ctx, actor, nil),
|
||||
PullRequests: pullRequests,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// loadPullRequestsForRun returns the pull requests associated with a run, matching
|
||||
// GitHub's `pull_requests` field on workflow run responses:
|
||||
// - For pull_request / pull_request_review events, the PR whose ref triggered the run.
|
||||
// - For push events, open PRs whose head branch matches the pushed ref in the same repo.
|
||||
// - For other events, no PRs.
|
||||
func loadPullRequestsForRun(ctx context.Context, run *actions_model.ActionRun) ([]*api.PullRequestMinimal, error) {
|
||||
result := []*api.PullRequestMinimal{}
|
||||
refName := git.RefName(run.Ref)
|
||||
var prs issues_model.PullRequestList
|
||||
switch {
|
||||
case run.Event.IsPullRequest() || run.Event.IsPullRequestReview():
|
||||
index, err := strconv.ParseInt(refName.PullName(), 10, 64)
|
||||
if err != nil {
|
||||
return result, nil
|
||||
}
|
||||
pr, err := issues_model.GetPullRequestByIndex(ctx, run.RepoID, index)
|
||||
if err != nil {
|
||||
if issues_model.IsErrPullRequestNotExist(err) {
|
||||
return result, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
prs = issues_model.PullRequestList{pr}
|
||||
case run.Event == webhook_module.HookEventPush:
|
||||
branch := refName.BranchName()
|
||||
if branch == "" {
|
||||
return result, nil
|
||||
}
|
||||
var err error
|
||||
prs, err = issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, run.RepoID, branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return result, nil
|
||||
}
|
||||
for _, pr := range prs {
|
||||
minimal, err := toPullRequestMinimal(ctx, run.Repo, pr, run.CommitSHA)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, minimal)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func toPullRequestMinimal(ctx context.Context, repo *repo_model.Repository, pr *issues_model.PullRequest, headSHA string) (*api.PullRequestMinimal, error) {
|
||||
if err := pr.LoadBaseRepo(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := pr.LoadHeadRepo(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
headRepo := pr.HeadRepo
|
||||
if headRepo == nil {
|
||||
headRepo = pr.BaseRepo
|
||||
}
|
||||
return &api.PullRequestMinimal{
|
||||
ID: pr.ID,
|
||||
Number: pr.Index,
|
||||
URL: fmt.Sprintf("%s/pulls/%d", repo.APIURL(ctx), pr.Index),
|
||||
Head: api.PullRequestMinimalHead{
|
||||
Ref: pr.HeadBranch,
|
||||
SHA: headSHA,
|
||||
Repo: api.PullRequestMinimalHeadRepo{
|
||||
ID: headRepo.ID,
|
||||
URL: headRepo.APIURL(ctx),
|
||||
Name: headRepo.Name,
|
||||
},
|
||||
},
|
||||
Base: api.PullRequestMinimalHead{
|
||||
Ref: pr.BaseBranch,
|
||||
SHA: pr.MergeBase,
|
||||
Repo: api.PullRequestMinimalHeadRepo{
|
||||
ID: pr.BaseRepo.ID,
|
||||
URL: pr.BaseRepo.APIURL(ctx),
|
||||
Name: pr.BaseRepo.Name,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1046,7 +1046,7 @@ func (*webhookNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_
|
||||
}
|
||||
|
||||
run.Repo = repo
|
||||
convertedRun, err := convert.ToActionWorkflowRun(ctx, run, nil)
|
||||
convertedRun, err := convert.ToActionWorkflowRun(ctx, run, nil, false)
|
||||
if err != nil {
|
||||
log.Error("ToActionWorkflowRun: %v", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user