Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow_run api + webhook #33964

Draft
wants to merge 40 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
edb2459
wip event
ChristopherHX Mar 14, 2025
013a0af
wip
ChristopherHX Mar 17, 2025
e93f9d2
update comment
ChristopherHX Mar 17, 2025
6701375
add routes
ChristopherHX Mar 17, 2025
988cafe
remove duplicated code
ChristopherHX Mar 17, 2025
20f5d6e
use ToActionsStatus
ChristopherHX Mar 17, 2025
a178e4b
use cached task instance if available
ChristopherHX Mar 17, 2025
295bf45
cleanup and fix webhook type bug
ChristopherHX Mar 17, 2025
392baec
remove dead code
ChristopherHX Mar 17, 2025
fb8a221
Move GetActionWorkflow to convert
ChristopherHX Mar 17, 2025
ccb9dec
fixup
ChristopherHX Mar 17, 2025
658583a
Merge branch 'main' of https://github.com/go-gitea/gitea into workflo…
ChristopherHX Mar 17, 2025
da738ea
webhook deliver the workflow field
ChristopherHX Mar 17, 2025
171efe5
update context with name
ChristopherHX Mar 17, 2025
605ed19
invoke workflow_run
ChristopherHX Mar 17, 2025
c3f6f13
prevent endless workflow_run trigger
ChristopherHX Mar 17, 2025
ca6bbc6
wip
ChristopherHX Mar 17, 2025
875c774
wip test and fixes
ChristopherHX Mar 18, 2025
22bfd96
fix keda scaler compat
ChristopherHX Mar 18, 2025
6c9dae9
fix api result
ChristopherHX Mar 18, 2025
5588588
add missing translate keys
ChristopherHX Mar 18, 2025
5a0f4c9
add other webhook types
ChristopherHX Mar 21, 2025
6df1854
..
ChristopherHX Mar 21, 2025
a983d34
fix workflow_run action event processing
ChristopherHX Mar 21, 2025
b650ca9
Merge branch 'main' of https://github.com/go-gitea/gitea into workflo…
ChristopherHX Mar 21, 2025
8cfb047
fix lint
ChristopherHX Mar 21, 2025
8fd54f2
update swagger docu
ChristopherHX Mar 21, 2025
c43cb79
...
ChristopherHX Mar 21, 2025
23de934
add branches-ignore to workflow_run
ChristopherHX Mar 21, 2025
956556d
update comment
ChristopherHX Mar 21, 2025
bb85519
allow workflow_run for recusive depth of 5
ChristopherHX Mar 21, 2025
cdefda1
fix comment
ChristopherHX Mar 21, 2025
d404c60
Sync run status with db prior webhook delivery
ChristopherHX Mar 21, 2025
0940208
fix lint
ChristopherHX Mar 21, 2025
4629a68
fix comment
ChristopherHX Mar 21, 2025
9b3eb4c
change action of workflow_run to align
ChristopherHX Mar 21, 2025
500a9bf
Merge branch 'main' of https://github.com/go-gitea/gitea into workflo…
ChristopherHX Mar 21, 2025
5beb9ae
...
ChristopherHX Mar 21, 2025
8acd36b
Merge branch 'main' of https://github.com/go-gitea/gitea into workflo…
ChristopherHX Mar 27, 2025
4ae2a2b
Merge branch 'main' into workflow-webhook-api
ChristopherHX Apr 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ func (run *ActionRun) GetPullRequestEventPayload() (*api.PullRequestPayload, err
return nil, fmt.Errorf("event %s is not a pull request event", run.Event)
}

func (run *ActionRun) GetWorkflowRunEventPayload() (*api.WorkflowRunPayload, error) {
if run.Event == webhook_module.HookEventWorkflowRun {
var payload api.WorkflowRunPayload
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
return nil, err
}
return &payload, nil
}
return nil, fmt.Errorf("event %s is not a pull request event", run.Event)
}

func (run *ActionRun) IsSchedule() bool {
return run.ScheduleID > 0
}
Expand Down
2 changes: 1 addition & 1 deletion models/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestWebhook_EventsArray(t *testing.T) {
"pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
"pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
"pull_request_review_comment", "pull_request_sync", "pull_request_review_request", "wiki", "repository", "release",
"package", "status", "workflow_job",
"package", "status", "workflow_run", "workflow_job",
},
(&Webhook{
HookEvent: &webhook_module.HookEvent{SendEverything: true},
Expand Down
54 changes: 54 additions & 0 deletions modules/actions/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
webhook_module.HookEventPackage:
return matchPackageEvent(payload.(*api.PackagePayload), evt)

case // workflow_run
webhook_module.HookEventWorkflowRun:
return matchWorkflowRunEvent(payload.(*api.WorkflowRunPayload), evt)

default:
log.Warn("unsupported event %q", triggedEvent)
return false
Expand Down Expand Up @@ -698,3 +702,53 @@ func matchPackageEvent(payload *api.PackagePayload, evt *jobparser.Event) bool {
}
return matchTimes == len(evt.Acts())
}

func matchWorkflowRunEvent(payload *api.WorkflowRunPayload, evt *jobparser.Event) bool {
// with no special filter parameters
if len(evt.Acts()) == 0 {
return true
}

matchTimes := 0
// all acts conditions should be satisfied
for cond, vals := range evt.Acts() {
switch cond {
case "types":
action := payload.Action
for _, val := range vals {
if glob.MustCompile(val, '/').Match(action) {
matchTimes++
break
}
}
case "workflows":
workflow := payload.Workflow
patterns, err := workflowpattern.CompilePatterns(vals...)
if err != nil {
break
}
if !workflowpattern.Skip(patterns, []string{workflow.Name}, &workflowpattern.EmptyTraceWriter{}) {
matchTimes++
}
case "branches":
patterns, err := workflowpattern.CompilePatterns(vals...)
if err != nil {
break
}
if !workflowpattern.Skip(patterns, []string{payload.WorkflowRun.HeadBranch}, &workflowpattern.EmptyTraceWriter{}) {
matchTimes++
}
case "branches-ignore":
patterns, err := workflowpattern.CompilePatterns(vals...)
if err != nil {
break
}
if !workflowpattern.Filter(patterns, []string{payload.WorkflowRun.HeadBranch}, &workflowpattern.EmptyTraceWriter{}) {
matchTimes++
}
default:
log.Warn("workflow run event unsupported condition %q", cond)
}
}
return matchTimes == len(evt.Acts())
}
16 changes: 16 additions & 0 deletions modules/structs/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,22 @@ func (p *CommitStatusPayload) JSONPayload() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}

// WorkflowRunPayload represents a payload information of workflow run event.
type WorkflowRunPayload struct {
Action string `json:"action"`
Workflow *ActionWorkflow `json:"workflow"`
WorkflowRun *ActionWorkflowRun `json:"workflow_run"`
PullRequest *PullRequest `json:"pull_request,omitempty"`
Organization *Organization `json:"organization,omitempty"`
Repo *Repository `json:"repository"`
Sender *User `json:"sender"`
}

// JSONPayload implements Payload
func (p *WorkflowRunPayload) JSONPayload() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}

// WorkflowJobPayload represents a payload information of workflow job event.
type WorkflowJobPayload struct {
Action string `json:"action"`
Expand Down
34 changes: 31 additions & 3 deletions modules/structs/repo_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,37 @@ type ActionArtifact struct {

// ActionWorkflowRun represents a WorkflowRun
type ActionWorkflowRun struct {
ID int64 `json:"id"`
RepositoryID int64 `json:"repository_id"`
HeadSha string `json:"head_sha"`
ID int64 `json:"id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
DisplayTitle string `json:"display_title"`
Path string `json:"path"`
Event string `json:"event"`
RunAttempt int64 `json:"run_attempt"`
RunNumber int64 `json:"run_number"`
RepositoryID int64 `json:"repository_id,omitempty"`
HeadSha string `json:"head_sha"`
HeadBranch string `json:"head_branch,omitempty"`
Status string `json:"status"`
Repository *Repository `json:"repository,omitempty"`
HeadRepository *Repository `json:"head_repository,omitempty"`
Conclusion string `json:"conclusion,omitempty"`
// swagger:strfmt date-time
StartedAt time.Time `json:"started_at,omitempty"`
// swagger:strfmt date-time
CompletedAt time.Time `json:"completed_at,omitempty"`
}

// ActionArtifactsResponse returns ActionArtifacts
type ActionWorkflowRunsResponse struct {
Entries []*ActionWorkflowRun `json:"workflow_runs"`
TotalCount int64 `json:"total_count"`
}

// ActionArtifactsResponse returns ActionArtifacts
type ActionWorkflowJobsResponse struct {
Entries []*ActionWorkflowJob `json:"jobs"`
TotalCount int64 `json:"total_count"`
}

// ActionArtifactsResponse returns ActionArtifacts
Expand Down
2 changes: 2 additions & 0 deletions modules/webhook/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
HookEventPullRequestReview HookEventType = "pull_request_review"
// Actions event only
HookEventSchedule HookEventType = "schedule"
HookEventWorkflowRun HookEventType = "workflow_run"
HookEventWorkflowJob HookEventType = "workflow_job"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ func AllEvents() []HookEventType {
HookEventRelease,
HookEventPackage,
HookEventStatus,
HookEventWorkflowRun,
HookEventWorkflowJob,
}
}
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,8 @@ settings.event_pull_request_review_request_desc = Pull request review requested
settings.event_pull_request_approvals = Pull Request Approvals
settings.event_pull_request_merge = Pull Request Merge
settings.event_header_workflow = Workflow Events
settings.event_workflow_run = Workflow Run
settings.event_workflow_run_desc = Gitea Actions Workflow run queued, waiting, in progress, or completed.
settings.event_workflow_job = Workflow Jobs
settings.event_workflow_job_desc = Gitea Actions Workflow job queued, waiting, in progress, or completed.
settings.event_package = Package
Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,11 @@ func Routes() *web.Router {
}, reqToken(), reqAdmin())
m.Group("/actions", func() {
m.Get("/tasks", repo.ListActionTasks)
m.Get("/runs", repo.GetWorkflowRuns)
m.Get("/runs/{run}", repo.GetWorkflowRun)
m.Get("/runs/{run}/jobs", repo.GetWorkflowJobs)
m.Get("/runs/{run}/artifacts", repo.GetArtifactsOfRun)
m.Get("/jobs/{job_id}", repo.GetWorkflowJob)
m.Get("/artifacts", repo.GetArtifacts)
m.Group("/artifacts/{artifact_id}", func() {
m.Get("", repo.GetArtifact)
Expand Down
Loading
Loading