Skip to content

Commit edb2459

Browse files
committed
wip event
1 parent cb6b33c commit edb2459

File tree

10 files changed

+102
-0
lines changed

10 files changed

+102
-0
lines changed

Diff for: modules/structs/hook.go

+16
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,22 @@ func (p *CommitStatusPayload) JSONPayload() ([]byte, error) {
470470
return json.MarshalIndent(p, "", " ")
471471
}
472472

473+
// WorkflowRunPayload represents a payload information of workflow run event.
474+
type WorkflowRunPayload struct {
475+
Action string `json:"action"`
476+
Workflow *ActionWorkflow `json:"workflow"`
477+
WorkflowRun *ActionWorkflowRun `json:"workflow_run"`
478+
PullRequest *PullRequest `json:"pull_request,omitempty"`
479+
Organization *Organization `json:"organization,omitempty"`
480+
Repo *Repository `json:"repository"`
481+
Sender *User `json:"sender"`
482+
}
483+
484+
// JSONPayload implements Payload
485+
func (p *WorkflowRunPayload) JSONPayload() ([]byte, error) {
486+
return json.MarshalIndent(p, "", " ")
487+
}
488+
473489
// WorkflowJobPayload represents a payload information of workflow job event.
474490
type WorkflowJobPayload struct {
475491
Action string `json:"action"`

Diff for: modules/structs/repo_actions.go

+12
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,20 @@ type ActionArtifact struct {
8787
// ActionWorkflowRun represents a WorkflowRun
8888
type ActionWorkflowRun struct {
8989
ID int64 `json:"id"`
90+
URL string `json:"url"`
91+
HTMLURL string `json:"html_url"`
92+
Event string `json:"event"`
93+
RunAttempt int64 `json:"run_attempt"`
94+
RunNumber int64 `json:"run_number"`
9095
RepositoryID int64 `json:"repository_id"`
9196
HeadSha string `json:"head_sha"`
97+
HeadBranch string `json:"head_branch,omitempty"`
98+
Status string `json:"status"`
99+
Conclusion string `json:"conclusion,omitempty"`
100+
// swagger:strfmt date-time
101+
StartedAt time.Time `json:"started_at,omitempty"`
102+
// swagger:strfmt date-time
103+
CompletedAt time.Time `json:"completed_at,omitempty"`
92104
}
93105

94106
// ActionArtifactsResponse returns ActionArtifacts

Diff for: modules/webhook/type.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
HookEventPullRequestReview HookEventType = "pull_request_review"
3939
// Actions event only
4040
HookEventSchedule HookEventType = "schedule"
41+
HookEventWorkflowRun HookEventType = "workflow_run"
4142
HookEventWorkflowJob HookEventType = "workflow_job"
4243
)
4344

Diff for: services/actions/clear_tasks.go

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func CancelAbandonedJobs(ctx context.Context) error {
125125
if updated {
126126
_ = job.LoadAttributes(ctx)
127127
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
128+
if job.Run != nil {
129+
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
130+
}
128131
}
129132
}
130133

Diff for: services/actions/notifier.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"context"
88

9+
actions_model "code.gitea.io/gitea/models/actions"
910
issues_model "code.gitea.io/gitea/models/issues"
1011
packages_model "code.gitea.io/gitea/models/packages"
1112
perm_model "code.gitea.io/gitea/models/perm"
@@ -762,3 +763,13 @@ func (n *actionsNotifier) MigrateRepository(ctx context.Context, doer, u *user_m
762763
Sender: convert.ToUser(ctx, doer, nil),
763764
}).Notify(ctx)
764765
}
766+
767+
func (n *actionsNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
768+
run.Status.IsBlocked()
769+
newNotifyInput(repo, sender, webhook_module.HookEventWorkflowRun).WithPayload(&api.WorkflowRunPayload{
770+
Action: "queued",
771+
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
772+
Organization: nil,
773+
Sender: convert.ToUser(ctx, sender, nil),
774+
}).Notify(ctx)
775+
}

Diff for: services/notify/notifier.go

+2
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,7 @@ type Notifier interface {
7979

8080
CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus)
8181

82+
WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun)
83+
8284
WorkflowJobStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, job *actions_model.ActionRunJob, task *actions_model.ActionTask)
8385
}

Diff for: services/notify/notify.go

+6
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit
376376
}
377377
}
378378

379+
func WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
380+
for _, notifier := range notifiers {
381+
notifier.WorkflowRunStatusUpdate(ctx, repo, sender, run)
382+
}
383+
}
384+
379385
func WorkflowJobStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, job *actions_model.ActionRunJob, task *actions_model.ActionTask) {
380386
for _, notifier := range notifiers {
381387
notifier.WorkflowJobStatusUpdate(ctx, repo, sender, job, task)

Diff for: services/notify/null.go

+3
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,8 @@ func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.R
214214
func (*NullNotifier) CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
215215
}
216216

217+
func (*NullNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
218+
}
219+
217220
func (*NullNotifier) WorkflowJobStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, job *actions_model.ActionRunJob, task *actions_model.ActionTask) {
218221
}

Diff for: services/webhook/notifier.go

+39
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,45 @@ func (*webhookNotifier) WorkflowJobStatusUpdate(ctx context.Context, repo *repo_
10301030
}
10311031
}
10321032

1033+
func (*webhookNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
1034+
source := EventSource{
1035+
Repository: repo,
1036+
Owner: repo.Owner,
1037+
}
1038+
1039+
var org *api.Organization
1040+
if repo.Owner.IsOrganization() {
1041+
org = convert.ToOrganization(ctx, organization.OrgFromUser(repo.Owner))
1042+
}
1043+
1044+
status, conclusion := toActionStatus(run.Status)
1045+
1046+
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventWorkflowJob, &api.WorkflowRunPayload{
1047+
Action: status,
1048+
Workflow: nil,
1049+
WorkflowRun: &api.ActionWorkflowRun{
1050+
ID: run.ID,
1051+
RunNumber: run.Index,
1052+
HTMLURL: run.HTMLURL(),
1053+
// Missing api endpoint for this location, artifacts are available under a nested url
1054+
URL: fmt.Sprintf("%s/actions/runs/%d", repo.APIURL(), run.ID),
1055+
Event: run.TriggerEvent,
1056+
RunAttempt: 0,
1057+
HeadSha: run.CommitSHA,
1058+
HeadBranch: git.RefName(run.Ref).BranchName(),
1059+
Status: status,
1060+
Conclusion: conclusion,
1061+
StartedAt: run.Started.AsTime().UTC(),
1062+
CompletedAt: run.Stopped.AsTime().UTC(),
1063+
},
1064+
Organization: org,
1065+
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner}),
1066+
Sender: convert.ToUser(ctx, sender, nil),
1067+
}); err != nil {
1068+
log.Error("PrepareWebhooks: %v", err)
1069+
}
1070+
}
1071+
10331072
func toActionStatus(status actions_model.Status) (string, string) {
10341073
var action string
10351074
var conclusion string

Diff for: templates/repo/settings/webhook/settings.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@
264264
<label>{{ctx.Locale.Tr "repo.settings.event_header_workflow"}}</label>
265265
</div>
266266
<!-- Workflow Job Event -->
267+
<div class="seven wide column">
268+
<div class="field">
269+
<div class="ui checkbox">
270+
<input name="workflow_run" type="checkbox" {{if .Webhook.HookEvents.Get "workflow_run"}}checked{{end}}>
271+
<label>{{ctx.Locale.Tr "repo.settings.event_workflow_run"}}</label>
272+
<span class="help">{{ctx.Locale.Tr "repo.settings.event_workflow_run_desc"}}</span>
273+
</div>
274+
</div>
275+
</div>
267276
<div class="seven wide column">
268277
<div class="field">
269278
<div class="ui checkbox">

0 commit comments

Comments
 (0)