mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-03 08:03:34 +00:00
## Summary This PR adds **scoped workflows** to Gitea Actions. Workflows defined centrally in a "source" repository that automatically run on every repository in scope: an organization's repositories, or (for instance admins) every repository on the instance. Each scoped run executes in the consuming repository's own context (its runners, secrets, and branch) while its content is read from the source repository, so an org or instance can mandate shared CI across many repositories without copying workflow files into each one. An owner or instance admin registers source repositories on a settings page and can mark individual workflows as **required**. A required scoped workflow cannot be opted out by a consuming repository and gates its pull-request merges; an optional one can be disabled per repository. Scoped workflows live under a dedicated `SCOPED_WORKFLOW_DIRS` (default `.gitea/scoped_workflows`), kept separate from regular `WORKFLOW_DIRS`. ## Main changes ### Configuration New `SCOPED_WORKFLOW_DIRS` setting, validated to not overlap with `WORKFLOW_DIRS`. Default: `.gitea/scoped_workflows` ### Data model & migration - New `action_scoped_workflow_source` table mapping a registering owner (`owner_id`, where `0` = instance-level) to a source repository, with a per-workflow `WorkflowConfigs` map. - `ActionRun` gains `WorkflowRepoID` / `WorkflowCommitSHA` (the pinned content source) and an `IsScopedRun` flag. ### Detection & run creation On consumer events, scoped workflows from the effective sources (the owner's own sources plus instance-level ones) are matched and turned into runs that execute in the consumer's context, with content pinned to the source repo's default-branch commit. `on: workflow_run` and `on: schedule` are currently not supported. ### Opt-out A consuming repository can disable an optional scoped workflow (tracked separately from regular `DisabledWorkflows`); required scoped workflows can never be disabled, opted out, or bypassed. ### Commit status A scoped run's status context format is `"<source repo full name>: <workflow display name> / <job> (<event>)"` (for example: `my-org/scoped-workflows: db-tests / test-sqlite (pull_request)`), keeping it distinct from a same-named repo-level workflow and from other sources. ### Required status checks Admins mark workflows required and supply status-check patterns. `EffectiveRequiredContexts` appends those patterns to the branch protection's required contexts and they are matched must-present-and-pass. If the status checks from scoped workflows fail, the PR cannot be merged. NOTE: scoped workflows' required status checks patterns can protect any target branch that has a protection rule, even though the rule's "Status Check" is disabled. A target branch with no protection rule cannot be protected. <details> <summary>Screenshots</summary> <img width="1400" alt="image" src="https://github.com/user-attachments/assets/a5d1db33-15ec-487e-93be-2bc04b4e6643" /> </details> ### Reusable workflows (`uses:`) A scoped workflow's local `uses: ./...` resolves against the source repository. `uses:` directory validation honors the instance-configurable `WORKFLOW_DIRS` and `SCOPED_WORKFLOW_DIRS` (previously hardcoded to `.gitea`/`.github/workflows`). ### Manual dispatch `workflow_dispatch` is supported for scoped workflows (web and API), resolving inputs/content from the source repo. ### Performance A process-local LRU cache keyed by source repo ID for the per-source workflow parse, so instance-level and owner-level sources don't open the source repo and parse workflow files on every event. ### UI Org / user / admin pages to register and remove sources, search repositories, and mark workflows required with their status-check patterns. The repository Actions sidebar groups scoped workflows by source with owner/instance labels and required/disabled badges. <details> <summary>Screenshots</summary> Scoped workflows setting page: <img width="1600" alt="image" src="https://github.com/user-attachments/assets/9d19f667-97a5-4935-92b2-e53f105e3642" /> Consumer repo's Actions runs list: <img width="1600" alt="image" src="https://github.com/user-attachments/assets/a77241f9-0aa9-41aa-ba73-12a9a688cb64" /> - `Owner`: this is a owner-level scoped workflows source repo - `Global`: this is a global scoped workflows source repo - `Required`: this scoped workflow is required, repo admin cannot disable it </details> --- Docs: https://gitea.com/gitea/docs/pulls/447 --------- Co-authored-by: bircni <bircni@icloud.com>
833 lines
29 KiB
Go
833 lines
29 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
actions_model "gitea.dev/models/actions"
|
|
issues_model "gitea.dev/models/issues"
|
|
"gitea.dev/models/organization"
|
|
packages_model "gitea.dev/models/packages"
|
|
perm_model "gitea.dev/models/perm"
|
|
access_model "gitea.dev/models/perm/access"
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/log"
|
|
"gitea.dev/modules/repository"
|
|
"gitea.dev/modules/setting"
|
|
api "gitea.dev/modules/structs"
|
|
"gitea.dev/modules/util"
|
|
webhook_module "gitea.dev/modules/webhook"
|
|
"gitea.dev/services/convert"
|
|
notify_service "gitea.dev/services/notify"
|
|
)
|
|
|
|
type actionsNotifier struct {
|
|
notify_service.NullNotifier
|
|
}
|
|
|
|
var _ notify_service.Notifier = &actionsNotifier{}
|
|
|
|
// NewNotifier create a new actionsNotifier notifier
|
|
func NewNotifier() notify_service.Notifier {
|
|
return &actionsNotifier{}
|
|
}
|
|
|
|
// NewIssue notifies issue created event
|
|
func (n *actionsNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, _ []*user_model.User) {
|
|
ctx = withMethod(ctx, "NewIssue")
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
return
|
|
}
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
return
|
|
}
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
|
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventIssues).WithPayload(&api.IssuePayload{
|
|
Action: api.HookIssueOpened,
|
|
Index: issue.Index,
|
|
Issue: convert.ToAPIIssue(ctx, issue.Poster, issue),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, issue.Poster, nil),
|
|
}).Notify(withMethod(ctx, "NewIssue"))
|
|
}
|
|
|
|
// IssueChangeContent notifies change content of issue
|
|
func (n *actionsNotifier) IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
|
|
ctx = withMethod(ctx, "IssueChangeContent")
|
|
n.notifyIssueChangeWithTitleOrContent(ctx, doer, issue)
|
|
}
|
|
|
|
func (n *actionsNotifier) IssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
|
|
ctx = withMethod(ctx, "IssueChangeTitle")
|
|
n.notifyIssueChangeWithTitleOrContent(ctx, doer, issue)
|
|
}
|
|
|
|
func (n *actionsNotifier) notifyIssueChangeWithTitleOrContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
|
|
var err error
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
|
log.Error("LoadRepo: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
|
if issue.IsPull {
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
|
log.Error("loadPullRequest: %v", err)
|
|
return
|
|
}
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequest).
|
|
WithDoer(doer).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: api.HookIssueEdited,
|
|
Index: issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).
|
|
WithPullRequest(issue.PullRequest).
|
|
Notify(ctx)
|
|
return
|
|
}
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventIssues).
|
|
WithDoer(doer).
|
|
WithPayload(&api.IssuePayload{
|
|
Action: api.HookIssueEdited,
|
|
Index: issue.Index,
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).
|
|
Notify(ctx)
|
|
}
|
|
|
|
// IssueChangeStatus notifies close or reopen issue to notifiers
|
|
func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, _ *issues_model.Comment, isClosed bool) {
|
|
ctx = withMethod(ctx, "IssueChangeStatus")
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
|
if issue.IsPull {
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
log.Error("LoadPullRequest: %v", err)
|
|
return
|
|
}
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
Index: issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
CommitID: commitID,
|
|
}
|
|
if isClosed {
|
|
apiPullRequest.Action = api.HookIssueClosed
|
|
} else {
|
|
apiPullRequest.Action = api.HookIssueReOpened
|
|
}
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequest).
|
|
WithDoer(doer).
|
|
WithPayload(apiPullRequest).
|
|
WithPullRequest(issue.PullRequest).
|
|
Notify(ctx)
|
|
return
|
|
}
|
|
apiIssue := &api.IssuePayload{
|
|
Index: issue.Index,
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}
|
|
if isClosed {
|
|
apiIssue.Action = api.HookIssueClosed
|
|
} else {
|
|
apiIssue.Action = api.HookIssueReOpened
|
|
}
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventIssues).
|
|
WithDoer(doer).
|
|
WithPayload(apiIssue).
|
|
Notify(ctx)
|
|
}
|
|
|
|
// IssueChangeAssignee notifies assigned or unassigned to notifiers
|
|
func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
|
|
ctx = withMethod(ctx, "IssueChangeAssignee")
|
|
|
|
var action api.HookIssueAction
|
|
if removed {
|
|
action = api.HookIssueUnassigned
|
|
} else {
|
|
action = api.HookIssueAssigned
|
|
}
|
|
|
|
hookEvent := webhook_module.HookEventIssueAssign
|
|
if issue.IsPull {
|
|
hookEvent = webhook_module.HookEventPullRequestAssign
|
|
}
|
|
|
|
notifyIssueChange(ctx, doer, issue, hookEvent, action, nil, nil)
|
|
}
|
|
|
|
// IssueChangeMilestone notifies assignee to notifiers
|
|
func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
|
|
ctx = withMethod(ctx, "IssueChangeMilestone")
|
|
|
|
var action api.HookIssueAction
|
|
if issue.MilestoneID > 0 {
|
|
action = api.HookIssueMilestoned
|
|
} else {
|
|
action = api.HookIssueDemilestoned
|
|
}
|
|
|
|
hookEvent := webhook_module.HookEventIssueMilestone
|
|
if issue.IsPull {
|
|
hookEvent = webhook_module.HookEventPullRequestMilestone
|
|
}
|
|
|
|
notifyIssueChange(ctx, doer, issue, hookEvent, action, nil, nil)
|
|
}
|
|
|
|
func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
|
|
addedLabels, removedLabels []*issues_model.Label,
|
|
) {
|
|
ctx = withMethod(ctx, "IssueChangeLabels")
|
|
|
|
hookEvent := webhook_module.HookEventIssueLabel
|
|
if issue.IsPull {
|
|
hookEvent = webhook_module.HookEventPullRequestLabel
|
|
}
|
|
|
|
notifyIssueChange(ctx, doer, issue, hookEvent, api.HookIssueLabelUpdated, addedLabels, removedLabels)
|
|
}
|
|
|
|
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction, addedLabels, removedLabels []*issues_model.Label) {
|
|
var err error
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
|
log.Error("LoadRepo: %v", err)
|
|
return
|
|
}
|
|
|
|
if err = issue.LoadPoster(ctx); err != nil {
|
|
log.Error("LoadPoster: %v", err)
|
|
return
|
|
}
|
|
|
|
var addedAPILabels []*api.Label
|
|
if addedLabels != nil {
|
|
addedAPILabels = make([]*api.Label, 0, len(addedLabels))
|
|
for _, label := range addedLabels {
|
|
addedAPILabels = append(addedAPILabels, convert.ToLabel(label, issue.Repo, doer))
|
|
}
|
|
}
|
|
|
|
// Get removed labels from context if present
|
|
var removedAPILabels []*api.Label
|
|
if removedLabels != nil {
|
|
removedAPILabels = make([]*api.Label, 0, len(removedLabels))
|
|
for _, label := range removedLabels {
|
|
removedAPILabels = append(removedAPILabels, convert.ToLabel(label, issue.Repo, doer))
|
|
}
|
|
}
|
|
|
|
if issue.IsPull {
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
|
log.Error("loadPullRequest: %v", err)
|
|
return
|
|
}
|
|
|
|
payload := &api.PullRequestPayload{
|
|
Action: action,
|
|
Index: issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Changes: &api.ChangesPayload{
|
|
AddedLabels: addedAPILabels,
|
|
RemovedLabels: removedAPILabels,
|
|
},
|
|
}
|
|
|
|
newNotifyInputFromIssue(issue, event).
|
|
WithDoer(doer).
|
|
WithPayload(payload).
|
|
WithPullRequest(issue.PullRequest).
|
|
Notify(ctx)
|
|
return
|
|
}
|
|
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
|
payload := &api.IssuePayload{
|
|
Action: action,
|
|
Index: issue.Index,
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Changes: &api.ChangesPayload{
|
|
AddedLabels: addedAPILabels,
|
|
RemovedLabels: removedAPILabels,
|
|
},
|
|
}
|
|
|
|
newNotifyInputFromIssue(issue, event).
|
|
WithDoer(doer).
|
|
WithPayload(payload).
|
|
Notify(ctx)
|
|
}
|
|
|
|
// CreateIssueComment notifies comment on an issue to notifiers
|
|
func (n *actionsNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
|
|
issue *issues_model.Issue, comment *issues_model.Comment, _ []*user_model.User,
|
|
) {
|
|
ctx = withMethod(ctx, "CreateIssueComment")
|
|
|
|
if issue.IsPull {
|
|
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventPullRequestComment, api.HookIssueCommentCreated)
|
|
return
|
|
}
|
|
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventIssueComment, api.HookIssueCommentCreated)
|
|
}
|
|
|
|
func (n *actionsNotifier) UpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
|
|
ctx = withMethod(ctx, "UpdateComment")
|
|
|
|
if c.Issue.IsPull {
|
|
notifyIssueCommentChange(ctx, doer, c, oldContent, webhook_module.HookEventPullRequestComment, api.HookIssueCommentEdited)
|
|
return
|
|
}
|
|
notifyIssueCommentChange(ctx, doer, c, oldContent, webhook_module.HookEventIssueComment, api.HookIssueCommentEdited)
|
|
}
|
|
|
|
func (n *actionsNotifier) DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
|
|
ctx = withMethod(ctx, "DeleteComment")
|
|
|
|
if comment.Issue.IsPull {
|
|
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventPullRequestComment, api.HookIssueCommentDeleted)
|
|
return
|
|
}
|
|
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventIssueComment, api.HookIssueCommentDeleted)
|
|
}
|
|
|
|
func notifyIssueCommentChange(ctx context.Context, doer *user_model.User, comment *issues_model.Comment, oldContent string, event webhook_module.HookEventType, action api.HookIssueCommentAction) {
|
|
comment.Issue = nil // force issue to be loaded
|
|
if err := comment.LoadIssue(ctx); err != nil {
|
|
log.Error("LoadIssue: %v", err)
|
|
return
|
|
}
|
|
if err := comment.Issue.LoadAttributes(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, _ := access_model.GetDoerRepoPermission(ctx, comment.Issue.Repo, doer)
|
|
|
|
payload := &api.IssueCommentPayload{
|
|
Action: action,
|
|
Issue: convert.ToAPIIssue(ctx, doer, comment.Issue),
|
|
Comment: convert.ToAPIComment(ctx, comment.Issue.Repo, comment),
|
|
Repository: convert.ToRepo(ctx, comment.Issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
IsPull: comment.Issue.IsPull,
|
|
}
|
|
|
|
if action == api.HookIssueCommentEdited {
|
|
payload.Changes = &api.ChangesPayload{
|
|
Body: &api.ChangesFromPayload{
|
|
From: oldContent,
|
|
},
|
|
}
|
|
}
|
|
|
|
if comment.Issue.IsPull {
|
|
if err := comment.Issue.LoadPullRequest(ctx); err != nil {
|
|
log.Error("LoadPullRequest: %v", err)
|
|
return
|
|
}
|
|
newNotifyInputFromIssue(comment.Issue, event).
|
|
WithDoer(doer).
|
|
WithPayload(payload).
|
|
WithPullRequest(comment.Issue.PullRequest).
|
|
Notify(ctx)
|
|
return
|
|
}
|
|
|
|
newNotifyInputFromIssue(comment.Issue, event).
|
|
WithDoer(doer).
|
|
WithPayload(payload).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) NewPullRequest(ctx context.Context, pull *issues_model.PullRequest, _ []*user_model.User) {
|
|
ctx = withMethod(ctx, "NewPullRequest")
|
|
|
|
if err := pull.LoadIssue(ctx); err != nil {
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
return
|
|
}
|
|
if err := pull.Issue.LoadRepo(ctx); err != nil {
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
return
|
|
}
|
|
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, pull.Issue.Repo, pull.Issue.Poster)
|
|
|
|
newNotifyInputFromIssue(pull.Issue, webhook_module.HookEventPullRequest).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: api.HookIssueOpened,
|
|
Index: pull.Issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
|
|
Repository: convert.ToRepo(ctx, pull.Issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, pull.Issue.Poster, nil),
|
|
}).
|
|
WithPullRequest(pull).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
|
ctx = withMethod(ctx, "CreateRepository")
|
|
|
|
newNotifyInput(repo, doer, webhook_module.HookEventRepository).WithPayload(&api.RepositoryPayload{
|
|
Action: api.HookRepoCreated,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
|
ctx = withMethod(ctx, "ForkRepository")
|
|
|
|
oldPermission, _ := access_model.GetDoerRepoPermission(ctx, oldRepo, doer)
|
|
permission, _ := access_model.GetDoerRepoPermission(ctx, repo, doer)
|
|
|
|
// forked webhook
|
|
newNotifyInput(oldRepo, doer, webhook_module.HookEventFork).WithPayload(&api.ForkPayload{
|
|
Forkee: convert.ToRepo(ctx, oldRepo, oldPermission),
|
|
Repo: convert.ToRepo(ctx, repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).Notify(ctx)
|
|
|
|
u := repo.MustOwner(ctx)
|
|
|
|
// Add to hook queue for created repo after session commit.
|
|
if u.IsOrganization() {
|
|
newNotifyInput(repo, doer, webhook_module.HookEventRepository).
|
|
WithRef(git.RefNameFromBranch(oldRepo.DefaultBranch).String()).
|
|
WithPayload(&api.RepositoryPayload{
|
|
Action: api.HookRepoCreated,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).Notify(ctx)
|
|
}
|
|
}
|
|
|
|
func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, _ *issues_model.Comment, _ []*user_model.User) {
|
|
ctx = withMethod(ctx, "PullRequestReview")
|
|
|
|
var reviewHookType webhook_module.HookEventType
|
|
|
|
switch review.Type {
|
|
case issues_model.ReviewTypeApprove:
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewApproved
|
|
case issues_model.ReviewTypeComment:
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewComment
|
|
case issues_model.ReviewTypeReject:
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewRejected
|
|
default:
|
|
// unsupported review webhook type here
|
|
log.Error("Unsupported review webhook type")
|
|
return
|
|
}
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
log.Error("pr.LoadIssue: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, err := access_model.GetIndividualUserRepoPermission(ctx, review.Issue.Repo, review.Issue.Poster)
|
|
if err != nil {
|
|
log.Error("models.GetIndividualUserRepoPermission: %v", err)
|
|
return
|
|
}
|
|
|
|
newNotifyInput(review.Issue.Repo, review.Reviewer, reviewHookType).
|
|
WithRef(review.CommitID).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: api.HookIssueReviewed,
|
|
Index: review.Issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
|
Repository: convert.ToRepo(ctx, review.Issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, review.Reviewer, nil),
|
|
Review: &api.ReviewPayload{
|
|
Type: string(reviewHookType),
|
|
Content: review.Content,
|
|
},
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
|
|
if !issue.IsPull {
|
|
log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID)
|
|
return
|
|
}
|
|
|
|
ctx = withMethod(ctx, "PullRequestReviewRequest")
|
|
|
|
permission, _ := access_model.GetDoerRepoPermission(ctx, issue.Repo, doer)
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
return
|
|
}
|
|
var action api.HookIssueAction
|
|
if isRequest {
|
|
action = api.HookIssueReviewRequested
|
|
} else {
|
|
action = api.HookIssueReviewRequestRemoved
|
|
}
|
|
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestReviewRequest).
|
|
WithDoer(doer).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: action,
|
|
Index: issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
|
RequestedReviewer: convert.ToUser(ctx, reviewer, nil),
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).
|
|
WithPullRequest(issue.PullRequest).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
ctx = withMethod(ctx, "MergePullRequest")
|
|
|
|
// Reload pull request information.
|
|
if err := pr.LoadAttributes(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, err := access_model.GetDoerRepoPermission(ctx, pr.Issue.Repo, doer)
|
|
if err != nil {
|
|
log.Error("models.GetDoerRepoPermission: %v", err)
|
|
return
|
|
}
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
Index: pr.Issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Action: api.HookIssueClosed,
|
|
}
|
|
|
|
newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest).
|
|
WithRef(pr.MergedCommitID).
|
|
WithPayload(apiPullRequest).
|
|
WithPullRequest(pr).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
|
commitID, _ := git.NewIDFromString(opts.NewCommitID)
|
|
if commitID.IsZero() {
|
|
log.Trace("new commitID is empty")
|
|
return
|
|
}
|
|
|
|
ctx = withMethod(ctx, "PushCommits")
|
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo)
|
|
if err != nil {
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
return
|
|
}
|
|
|
|
newNotifyInput(repo, pusher, webhook_module.HookEventPush).
|
|
WithRef(opts.RefFullName.String()).
|
|
WithPayload(&api.PushPayload{
|
|
Ref: opts.RefFullName.String(),
|
|
Before: opts.OldCommitID,
|
|
After: opts.NewCommitID,
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
Commits: apiCommits,
|
|
HeadCommit: apiHeadCommit,
|
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Pusher: apiPusher,
|
|
Sender: apiPusher,
|
|
}).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) CreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
|
|
ctx = withMethod(ctx, "CreateRef")
|
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
|
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
|
|
|
|
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
|
|
WithRef(refFullName.String()).
|
|
WithPayload(&api.CreatePayload{
|
|
Ref: refFullName.String(), // HINT: here is inconsistent with the Webhook's payload: webhook uses ShortName
|
|
Sha: refID,
|
|
RefType: string(refFullName.RefType()),
|
|
Repo: apiRepo,
|
|
Sender: apiPusher,
|
|
}).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
|
ctx = withMethod(ctx, "DeleteRef")
|
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
|
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
|
|
|
|
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
|
|
WithPayload(&api.DeletePayload{
|
|
Ref: refFullName.String(), // HINT: here is inconsistent with the Webhook's payload: webhook uses ShortName
|
|
RefType: string(refFullName.RefType()),
|
|
PusherType: api.PusherTypeUser,
|
|
Repo: apiRepo,
|
|
Sender: apiPusher,
|
|
}).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) SyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
|
ctx = withMethod(ctx, "SyncPushCommits")
|
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo)
|
|
if err != nil {
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
return
|
|
}
|
|
|
|
newNotifyInput(repo, pusher, webhook_module.HookEventPush).
|
|
WithRef(opts.RefFullName.String()).
|
|
WithPayload(&api.PushPayload{
|
|
Ref: opts.RefFullName.String(),
|
|
Before: opts.OldCommitID,
|
|
After: opts.NewCommitID,
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
Commits: apiCommits,
|
|
TotalCommits: commits.Len,
|
|
HeadCommit: apiHeadCommit,
|
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Pusher: apiPusher,
|
|
Sender: apiPusher,
|
|
}).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) SyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
|
|
ctx = withMethod(ctx, "SyncCreateRef")
|
|
n.CreateRef(ctx, pusher, repo, refFullName, refID)
|
|
}
|
|
|
|
func (n *actionsNotifier) SyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
|
ctx = withMethod(ctx, "SyncDeleteRef")
|
|
n.DeleteRef(ctx, pusher, repo, refFullName)
|
|
}
|
|
|
|
func (n *actionsNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) {
|
|
ctx = withMethod(ctx, "NewRelease")
|
|
notifyRelease(ctx, rel.Publisher, rel, api.HookReleasePublished)
|
|
}
|
|
|
|
func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
ctx = withMethod(ctx, "UpdateRelease")
|
|
notifyRelease(ctx, doer, rel, api.HookReleaseUpdated)
|
|
}
|
|
|
|
func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
if rel.IsTag {
|
|
// has sent same action in `PushCommits`, so skip it.
|
|
return
|
|
}
|
|
ctx = withMethod(ctx, "DeleteRelease")
|
|
notifyRelease(ctx, doer, rel, api.HookReleaseDeleted)
|
|
}
|
|
|
|
func (n *actionsNotifier) PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
ctx = withMethod(ctx, "PackageCreate")
|
|
notifyPackage(ctx, doer, pd, api.HookPackageCreated)
|
|
}
|
|
|
|
func (n *actionsNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
ctx = withMethod(ctx, "PackageDelete")
|
|
notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
|
|
}
|
|
|
|
func (n *actionsNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
ctx = withMethod(ctx, "AutoMergePullRequest")
|
|
n.MergePullRequest(ctx, doer, pr)
|
|
}
|
|
|
|
func (n *actionsNotifier) PullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, before, after string) {
|
|
ctx = withMethod(ctx, "PullRequestSynchronized")
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
return
|
|
}
|
|
|
|
newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequestSync).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: api.HookIssueSynchronized,
|
|
Before: before,
|
|
After: after,
|
|
Index: pr.Issue.Index,
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).
|
|
WithPullRequest(pr).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
|
|
ctx = withMethod(ctx, "PullRequestChangeTargetBranch")
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
return
|
|
}
|
|
|
|
permission, _ := access_model.GetIndividualUserRepoPermission(ctx, pr.Issue.Repo, pr.Issue.Poster)
|
|
newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest).
|
|
WithPayload(&api.PullRequestPayload{
|
|
Action: api.HookIssueEdited,
|
|
Index: pr.Issue.Index,
|
|
Changes: &api.ChangesPayload{
|
|
Ref: &api.ChangesFromPayload{
|
|
From: oldBranch,
|
|
},
|
|
},
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).
|
|
WithPullRequest(pr).
|
|
Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) NewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
|
ctx = withMethod(ctx, "NewWikiPage")
|
|
|
|
newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
|
|
Action: api.HookWikiCreated,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Page: page,
|
|
Comment: comment,
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
|
ctx = withMethod(ctx, "EditWikiPage")
|
|
|
|
newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
|
|
Action: api.HookWikiEdited,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Page: page,
|
|
Comment: comment,
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
|
|
ctx = withMethod(ctx, "DeleteWikiPage")
|
|
|
|
newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
|
|
Action: api.HookWikiDeleted,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
Page: page,
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
// MigrateRepository is used to detect workflows after a repository has been migrated
|
|
func (n *actionsNotifier) MigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
|
ctx = withMethod(ctx, "MigrateRepository")
|
|
|
|
newNotifyInput(repo, doer, webhook_module.HookEventRepository).WithPayload(&api.RepositoryPayload{
|
|
Action: api.HookRepoCreated,
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
}).Notify(ctx)
|
|
}
|
|
|
|
func (n *actionsNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
|
|
ctx = withMethod(ctx, "WorkflowRunStatusUpdate")
|
|
|
|
var org *api.Organization
|
|
if repo.Owner.IsOrganization() {
|
|
org = convert.ToOrganization(ctx, organization.OrgFromUser(repo.Owner))
|
|
}
|
|
|
|
status := convert.ToWorkflowRunAction(run.Status)
|
|
|
|
convertedWorkflow, err := convert.ResolveActionWorkflowForRun(ctx, repo, run)
|
|
if err != nil {
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
// The workflow definition is gone (e.g. a scoped source repo/file was deleted, or the file no longer exists at the recorded commit), skip
|
|
log.Debug("WorkflowRunStatusUpdate: workflow %q for run %d not found: %v", run.WorkflowID, run.ID, err)
|
|
return
|
|
}
|
|
log.Error("WorkflowRunStatusUpdate resolve workflow: %v", err)
|
|
return
|
|
}
|
|
|
|
run.Repo = repo
|
|
convertedRun, err := convert.ToActionWorkflowRun(ctx, run, nil, false)
|
|
if err != nil {
|
|
log.Error("ToActionWorkflowRun: %v", err)
|
|
return
|
|
}
|
|
|
|
newNotifyInput(repo, sender, webhook_module.HookEventWorkflowRun).
|
|
WithRef(git.RefNameFromBranch(repo.DefaultBranch).String()).
|
|
WithPayload(&api.WorkflowRunPayload{
|
|
Action: status,
|
|
Workflow: convertedWorkflow,
|
|
WorkflowRun: convertedRun,
|
|
Organization: org,
|
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
|
|
Sender: convert.ToUser(ctx, sender, nil),
|
|
}).Notify(ctx)
|
|
}
|