Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions models/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func (w *Webhook) HasPullRequestReviewRequestEvent() bool {
(w.ChooseEvents && w.HookEvents.PullRequestReviewRequest)
}

// HasWorkflowRunEvent returns if hook enabled workflow run event.
func (w *Webhook) HasWorkflowRunEvent() bool {
return w.SendEverything || (w.ChooseEvents && w.HookEvents.WorkflowRun)
}

// EventCheckers returns event checkers
func (w *Webhook) EventCheckers() []struct {
Has func() bool
Expand Down Expand Up @@ -336,6 +341,7 @@ func (w *Webhook) EventCheckers() []struct {
{w.HasReleaseEvent, webhook_module.HookEventRelease},
{w.HasPackageEvent, webhook_module.HookEventPackage},
{w.HasPullRequestReviewRequestEvent, webhook_module.HookEventPullRequestReviewRequest},
{w.HasWorkflowRunEvent, webhook_module.HookEventWorkflowRun},
}
}

Expand Down
19 changes: 19 additions & 0 deletions modules/structs/action_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package structs

type ActionRun struct {
ID int64 `json:"id"`
Title string `json:"title"`
Repo *Repository `json:"repo"`
Owner *User `json:"owner"`
TriggerUser *User `json:"trigger_user"`
WorkflowID string `json:"workflow_id"`
Index int64 `json:"index"`
Ref string `json:"ref"`
CommitSHA string `json:"commit_sha"`
Status string `json:"status"`
Version int `json:"version"`
Started string `json:"time_started"`
Stopped string `json:"time_stopped"`
Created string `json:"time_created"`
Updated string `json:"time_updated"`
}
7 changes: 7 additions & 0 deletions modules/structs/hook_artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package structs

type ArtifactPayload struct {
ArtifactID int64 `json:"artifact_id"`
ArtifactName string `json:"artifact_name"`
RunID int64 `json:"run_id"`
}
25 changes: 25 additions & 0 deletions modules/structs/hook_workflow_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package structs

import "code.gitea.io/gitea/modules/json"

var _ Payloader = &WorkflowRunPayload{}

type HookWorkflowRunAction string

const (
HookWorkflowRunRequested HookWorkflowRunAction = "requested"
HookWorkflowRunInProgress HookWorkflowRunAction = "in_progress"
HookWorkflowRunCompleted HookWorkflowRunAction = "completed"
)

type WorkflowRunPayload struct {
Action HookWorkflowRunAction `json:"action"`
Repository *Repository `json:"repository"`
Sender *User `json:"sender"`
ActionRun *ActionRun `json:"action_run"`
Artifacts *[]ArtifactPayload `json:"artifacts"`
}

func (p *WorkflowRunPayload) JSONPayload() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}
2 changes: 2 additions & 0 deletions modules/webhook/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type HookEvents struct {
Repository bool `json:"repository"`
Release bool `json:"release"`
Package bool `json:"package"`
WorkflowRun bool `json:"workflow_run"`
}

// ParseHookEvents converts a list of strings to HookEvents
Expand All @@ -54,6 +55,7 @@ func ParseHookEvents(eventTypes []string) HookEvents {
Wiki: util.SliceContainsString(eventTypes, HookEventWiki.Event(), caseInsensitive),
Repository: util.SliceContainsString(eventTypes, HookEventRepository.Event(), caseInsensitive),
Release: util.SliceContainsString(eventTypes, HookEventRelease.Event(), caseInsensitive),
WorkflowRun: util.SliceContainsString(eventTypes, HookEventWorkflowRun.Event(), caseInsensitive),
}
}

Expand Down
3 changes: 3 additions & 0 deletions modules/webhook/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
HookEventRepository HookEventType = "repository"
HookEventRelease HookEventType = "release"
HookEventPackage HookEventType = "package"
HookEventWorkflowRun HookEventType = "workflow_run"
)

// Event returns the HookEventType as an event string
Expand Down Expand Up @@ -63,6 +64,8 @@ func (h HookEventType) Event() string {
return "repository"
case HookEventRelease:
return "release"
case HookEventWorkflowRun:
return "workflow_run"
}
return ""
}
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 @@ -2220,6 +2220,8 @@ settings.event_pull_request_approvals = Pull Request Approvals
settings.event_pull_request_merge = Pull Request Merge
settings.event_package = Package
settings.event_package_desc = Package created or deleted in a repository.
settings.event_workflow_run = Workflow Run
settings.event_workflow_run_desc = Workflow runs created, started or completed.
settings.branch_filter = Branch filter
settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or <code>*</code>, events for all branches are reported. See <a href="https://pkg.go.dev/github.com/gobwas/glob#Compile">github.com/gobwas/glob</a> documentation for syntax. Examples: <code>master</code>, <code>{master,release*}</code>.
settings.authorization_header = Authorization Header
Expand Down
6 changes: 6 additions & 0 deletions routers/api/actions/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
actions_service "code.gitea.io/gitea/services/actions"
notify_service "code.gitea.io/gitea/services/notify"

runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
Expand Down Expand Up @@ -174,6 +175,11 @@ func (s *Service) UpdateTask(
return nil, status.Errorf(codes.Internal, "update task: %v", err)
}

if task.Status.IsDone() {
// notify task completed
notify_service.CompletedWorkflowRun(ctx, task.Job.Run)
}

for k, v := range req.Msg.Outputs {
if len(k) > 255 {
log.Warn("Ignore the output of task %d because the key is too long: %q", task.ID, k)
Expand Down
4 changes: 4 additions & 0 deletions routers/api/actions/runner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
secret_module "code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/actions"
notify_service "code.gitea.io/gitea/services/notify"

runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
"google.golang.org/protobuf/types/known/structpb"
Expand All @@ -31,6 +32,9 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv
return nil, false, nil
}

// notify action in progress
notify_service.StartedWorkflowRun(ctx, t.Job.Run)

actions.CreateCommitStatus(ctx, t.Job)

task := &runnerv1.Task{
Expand Down
1 change: 1 addition & 0 deletions routers/web/repo/setting/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
Wiki: form.Wiki,
Repository: form.Repository,
Package: form.Package,
WorkflowRun: form.WorkflowRun,
},
BranchFilter: form.BranchFilter,
}
Expand Down
3 changes: 3 additions & 0 deletions services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/convert"
notify_service "code.gitea.io/gitea/services/notify"

"github.com/nektos/act/pkg/jobparser"
"github.com/nektos/act/pkg/model"
Expand Down Expand Up @@ -273,6 +274,8 @@ func handleWorkflows(
continue
}

notify_service.RequestedWorkflowRun(ctx, run)

alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID})
if err != nil {
log.Error("FindRunJobs: %v", err)
Expand Down
31 changes: 31 additions & 0 deletions services/convert/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package convert

import (
"context"

models_actions "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
api "code.gitea.io/gitea/modules/structs"
)

// ToRepo converts an ActionRun to api.ActionRun
func ToActionRun(ctx context.Context, run *models_actions.ActionRun) *api.ActionRun {
return &api.ActionRun{
ID: run.ID,
Title: run.Title,
Repo: ToRepo(ctx, run.Repo, access_model.Permission{AccessMode: perm.AccessModeOwner}),
Owner: ToUser(ctx, run.Repo.Owner, nil),
TriggerUser: ToUser(ctx, run.TriggerUser, nil),
WorkflowID: run.WorkflowID,
Index: run.Index,
Ref: run.Ref,
CommitSHA: run.CommitSHA,
Status: run.Status.String(),
Version: run.Version,
Started: run.Started.FormatLong(),
Stopped: run.Stopped.FormatLong(),
Created: run.Created.FormatLong(),
Updated: run.Updated.FormatLong(),
}
}
17 changes: 17 additions & 0 deletions services/convert/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package convert

import (
"context"

models_actions "code.gitea.io/gitea/models/actions"
api "code.gitea.io/gitea/modules/structs"
)

// ToRepo converts an ActionRun to api.ActionRun
func ToArtifact(ctx context.Context, artifact *models_actions.ActionArtifact) *api.ArtifactPayload {
return &api.ArtifactPayload{
ArtifactID: artifact.ID,
ArtifactName: artifact.ArtifactName,
RunID: artifact.RunID,
}
}
1 change: 1 addition & 0 deletions services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ type WebhookForm struct {
Wiki bool
Repository bool
Package bool
WorkflowRun bool
Active bool
BranchFilter string `binding:"GlobPattern"`
AuthorizationHeader string
Expand Down
5 changes: 5 additions & 0 deletions services/notify/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package notify
import (
"context"

actions_model "code.gitea.io/gitea/models/actions"
issues_model "code.gitea.io/gitea/models/issues"
packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -74,4 +75,8 @@ type Notifier interface {
PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor)

ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository)

RequestedWorkflowRun(ctx context.Context, run *actions_model.ActionRun)
StartedWorkflowRun(ctx context.Context, run *actions_model.ActionRun)
CompletedWorkflowRun(ctx context.Context, run *actions_model.ActionRun)
}
19 changes: 19 additions & 0 deletions services/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package notify
import (
"context"

actions_model "code.gitea.io/gitea/models/actions"
issues_model "code.gitea.io/gitea/models/issues"
packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -367,3 +368,21 @@ func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
notifier.ChangeDefaultBranch(ctx, repo)
}
}

func RequestedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
for _, notifier := range notifiers {
notifier.RequestedWorkflowRun(ctx, run)
}
}

func StartedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
for _, notifier := range notifiers {
notifier.StartedWorkflowRun(ctx, run)
}
}

func CompletedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
for _, notifier := range notifiers {
notifier.CompletedWorkflowRun(ctx, run)
}
}
10 changes: 10 additions & 0 deletions services/notify/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package notify
import (
"context"

actions_model "code.gitea.io/gitea/models/actions"
issues_model "code.gitea.io/gitea/models/issues"
packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -208,3 +209,12 @@ func (*NullNotifier) PackageDelete(ctx context.Context, doer *user_model.User, p
// ChangeDefaultBranch places a place holder function
func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
}

func (*NullNotifier) RequestedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
}

func (*NullNotifier) StartedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
}

func (*NullNotifier) CompletedWorkflowRun(ctx context.Context, run *actions_model.ActionRun) {
}
Loading