feat: add IsForkPullRequest

This commit is contained in:
Jason Song 2022-11-23 15:12:26 +08:00
parent 6ad8bddabf
commit fdd3c0434e
6 changed files with 186 additions and 172 deletions

View File

@ -24,26 +24,25 @@ import (
// Run represents a run of a workflow file // Run represents a run of a workflow file
type Run struct { type Run struct {
ID int64 ID int64
Title string Title string
RepoID int64 `xorm:"index unique(repo_index)"` RepoID int64 `xorm:"index unique(repo_index)"`
Repo *repo_model.Repository `xorm:"-"` Repo *repo_model.Repository `xorm:"-"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
WorkflowID string `xorm:"index"` // the name of workflow file WorkflowID string `xorm:"index"` // the name of workflow file
Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository
TriggerUserID int64 TriggerUserID int64
TriggerUser *user_model.User `xorm:"-"` TriggerUser *user_model.User `xorm:"-"`
Ref string Ref string
CommitSHA string CommitSHA string
Event webhook.HookEventType IsForkPullRequest bool
Token string // token for this task Event webhook.HookEventType
Grant string // permissions for this task EventPayload string `xorm:"LONGTEXT"`
EventPayload string `xorm:"LONGTEXT"` Status Status `xorm:"index"`
Status Status `xorm:"index"` Started timeutil.TimeStamp
Started timeutil.TimeStamp Stopped timeutil.TimeStamp
Stopped timeutil.TimeStamp Created timeutil.TimeStamp `xorm:"created"`
Created timeutil.TimeStamp `xorm:"created"` Updated timeutil.TimeStamp `xorm:"updated"`
Updated timeutil.TimeStamp `xorm:"updated"`
} }
func init() { func init() {
@ -179,16 +178,17 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
status = StatusBlocked status = StatusBlocked
} }
runJobs = append(runJobs, &RunJob{ runJobs = append(runJobs, &RunJob{
RunID: run.ID, RunID: run.ID,
RepoID: run.RepoID, RepoID: run.RepoID,
OwnerID: run.OwnerID, OwnerID: run.OwnerID,
CommitSHA: run.CommitSHA, CommitSHA: run.CommitSHA,
Name: job.Name, IsForkPullRequest: run.IsForkPullRequest,
WorkflowPayload: payload, Name: job.Name,
JobID: id, WorkflowPayload: payload,
Needs: needs, JobID: id,
RunsOn: job.RunsOn(), Needs: needs,
Status: status, RunsOn: job.RunsOn(),
Status: status,
}) })
} }
if err := db.Insert(ctx, runJobs); err != nil { if err := db.Insert(ctx, runJobs); err != nil {

View File

@ -18,24 +18,25 @@ import (
// RunJob represents a job of a run // RunJob represents a job of a run
type RunJob struct { type RunJob struct {
ID int64 ID int64
RunID int64 `xorm:"index"` RunID int64 `xorm:"index"`
Run *Run `xorm:"-"` Run *Run `xorm:"-"`
RepoID int64 `xorm:"index"` RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"` CommitSHA string `xorm:"index"`
Name string IsForkPullRequest bool
Attempt int64 Name string
WorkflowPayload []byte Attempt int64
JobID string // job id in workflow, not job's id WorkflowPayload []byte
Needs []string `xorm:"JSON TEXT"` JobID string // job id in workflow, not job's id
RunsOn []string `xorm:"JSON TEXT"` Needs []string `xorm:"JSON TEXT"`
TaskID int64 // the latest task of the job RunsOn []string `xorm:"JSON TEXT"`
Status Status `xorm:"index"` TaskID int64 // the latest task of the job
Started timeutil.TimeStamp Status Status `xorm:"index"`
Stopped timeutil.TimeStamp Started timeutil.TimeStamp
Created timeutil.TimeStamp `xorm:"created"` Stopped timeutil.TimeStamp
Updated timeutil.TimeStamp `xorm:"updated index"` Created timeutil.TimeStamp `xorm:"created"`
Updated timeutil.TimeStamp `xorm:"updated index"`
} }
func init() { func init() {

View File

@ -43,9 +43,10 @@ type Task struct {
Started timeutil.TimeStamp `xorm:"index"` Started timeutil.TimeStamp `xorm:"index"`
Stopped timeutil.TimeStamp Stopped timeutil.TimeStamp
RepoID int64 `xorm:"index"` RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"` CommitSHA string `xorm:"index"`
IsForkPullRequest bool
Token string `xorm:"-"` Token string `xorm:"-"`
TokenHash string `xorm:"UNIQUE"` // sha256 of token TokenHash string `xorm:"UNIQUE"` // sha256 of token
@ -337,14 +338,15 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
job.Status = StatusRunning job.Status = StatusRunning
task := &Task{ task := &Task{
JobID: job.ID, JobID: job.ID,
Attempt: job.Attempt, Attempt: job.Attempt,
RunnerID: runner.ID, RunnerID: runner.ID,
Started: now, Started: now,
Status: StatusRunning, Status: StatusRunning,
RepoID: job.RepoID, RepoID: job.RepoID,
OwnerID: job.OwnerID, OwnerID: job.OwnerID,
CommitSHA: job.CommitSHA, CommitSHA: job.CommitSHA,
IsForkPullRequest: job.IsForkPullRequest,
} }
if err := task.GenerateToken(); err != nil { if err := task.GenerateToken(); err != nil {
return nil, false, err return nil, false, err

View File

@ -418,6 +418,11 @@ func (pr *PullRequest) IsAncestor() bool {
return pr.Status == PullRequestStatusAncestor return pr.Status == PullRequestStatusAncestor
} }
// IsFromFork return true if this PR is from a fork.
func (pr *PullRequest) IsFromFork() bool {
return pr.HeadRepoID != pr.BaseRepoID
}
// SetMerged sets a pull request to merged and closes the corresponding issue // SetMerged sets a pull request to merged and closes the corresponding issue
func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) { func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) {
if pr.HasMerged { if pr.HasMerged {

View File

@ -53,44 +53,44 @@ func addBotTables(x *xorm.Engine) error {
} }
type BotsRun struct { type BotsRun struct {
ID int64 ID int64
Title string Title string
RepoID int64 `xorm:"index unique(repo_index)"` RepoID int64 `xorm:"index unique(repo_index)"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
WorkflowID string `xorm:"index"` // the name of workflow file WorkflowID string `xorm:"index"` // the name of workflow file
Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository
TriggerUserID int64 TriggerUserID int64
Ref string Ref string
CommitSHA string CommitSHA string
Event string Event string
Token string // token for this task IsForkPullRequest bool
Grant string // permissions for this task EventPayload string `xorm:"LONGTEXT"`
EventPayload string `xorm:"LONGTEXT"` Status int `xorm:"index"`
Status int `xorm:"index"` Started timeutil.TimeStamp
Started timeutil.TimeStamp Stopped timeutil.TimeStamp
Stopped timeutil.TimeStamp Created timeutil.TimeStamp `xorm:"created"`
Created timeutil.TimeStamp `xorm:"created"` Updated timeutil.TimeStamp `xorm:"updated"`
Updated timeutil.TimeStamp `xorm:"updated"`
} }
type BotsRunJob struct { type BotsRunJob struct {
ID int64 ID int64
RunID int64 `xorm:"index"` RunID int64 `xorm:"index"`
RepoID int64 `xorm:"index"` RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"` CommitSHA string `xorm:"index"`
Name string IsForkPullRequest bool
Attempt int64 Name string
WorkflowPayload []byte Attempt int64
JobID string // job id in workflow, not job's id WorkflowPayload []byte
Needs []string `xorm:"JSON TEXT"` JobID string // job id in workflow, not job's id
RunsOn []string `xorm:"JSON TEXT"` Needs []string `xorm:"JSON TEXT"`
TaskID int64 // the latest task of the job RunsOn []string `xorm:"JSON TEXT"`
Status int `xorm:"index"` TaskID int64 // the latest task of the job
Started timeutil.TimeStamp Status int `xorm:"index"`
Stopped timeutil.TimeStamp Started timeutil.TimeStamp
Created timeutil.TimeStamp `xorm:"created"` Stopped timeutil.TimeStamp
Updated timeutil.TimeStamp `xorm:"updated index"` Created timeutil.TimeStamp `xorm:"created"`
Updated timeutil.TimeStamp `xorm:"updated index"`
} }
type Repository struct { type Repository struct {
@ -109,9 +109,10 @@ func addBotTables(x *xorm.Engine) error {
Started timeutil.TimeStamp `xorm:"index"` Started timeutil.TimeStamp `xorm:"index"`
Stopped timeutil.TimeStamp Stopped timeutil.TimeStamp
RepoID int64 `xorm:"index"` RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"` OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"` CommitSHA string `xorm:"index"`
IsForkPullRequest bool
TokenHash string `xorm:"UNIQUE"` // sha256 of token TokenHash string `xorm:"UNIQUE"` // sha256 of token
TokenSalt string TokenSalt string

View File

@ -45,6 +45,10 @@ func NewNotifier() base.Notifier {
} }
func notify(repo *repo_model.Repository, doer *user_model.User, ref string, evt webhook.HookEventType, payload api.Payloader) error { func notify(repo *repo_model.Repository, doer *user_model.User, ref string, evt webhook.HookEventType, payload api.Payloader) error {
return notifyWithPR(repo, doer, ref, evt, payload, nil)
}
func notifyWithPR(repo *repo_model.Repository, doer *user_model.User, ref string, evt webhook.HookEventType, payload api.Payloader, pr *issues_model.PullRequest) error {
if unit.TypeBots.UnitGlobalDisabled() { if unit.TypeBots.UnitGlobalDisabled() {
return nil return nil
} }
@ -83,16 +87,17 @@ func notify(repo *repo_model.Repository, doer *user_model.User, ref string, evt
for id, content := range workflows { for id, content := range workflows {
run := bots_model.Run{ run := bots_model.Run{
Title: commit.Message(), Title: commit.Message(),
RepoID: repo.ID, RepoID: repo.ID,
OwnerID: repo.OwnerID, OwnerID: repo.OwnerID,
WorkflowID: id, WorkflowID: id,
TriggerUserID: doer.ID, TriggerUserID: doer.ID,
Ref: ref, Ref: ref,
CommitSHA: commit.ID.String(), CommitSHA: commit.ID.String(),
Event: evt, IsForkPullRequest: pr != nil && pr.IsFromFork(),
EventPayload: string(p), Event: evt,
Status: bots_model.StatusWaiting, EventPayload: string(p),
Status: bots_model.StatusWaiting,
} }
if len(run.Title) > 255 { if len(run.Title) > 255 {
run.Title = run.Title[:255] // FIXME: we should use a better method to cut title run.Title = run.Title[:255] // FIXME: we should use a better method to cut title
@ -110,22 +115,22 @@ func notify(repo *repo_model.Repository, doer *user_model.User, ref string, evt
} }
// NotifyNewIssue notifies issue created event // NotifyNewIssue notifies issue created event
func (a *botsNotifier) NotifyNewIssue(issue *issues_model.Issue, mentions []*user_model.User) { func (a *botsNotifier) NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
if err := issue.LoadRepo(db.DefaultContext); err != nil { if err := issue.LoadRepo(ctx); err != nil {
log.Error("issue.LoadRepo: %v", err) log.Error("issue.LoadRepo: %v", err)
return return
} }
if err := issue.LoadPoster(); err != nil { if err := issue.LoadPoster(ctx); err != nil {
log.Error("issue.LoadPoster: %v", err) log.Error("issue.LoadPoster: %v", err)
return return
} }
mode, _ := access_model.AccessLevel(issue.Poster, issue.Repo) mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
if err := notify(issue.Repo, issue.Poster, issue.Repo.DefaultBranch, if err := notify(issue.Repo, issue.Poster, issue.Repo.DefaultBranch,
webhook.HookEventIssues, &api.IssuePayload{ webhook.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueOpened, Action: api.HookIssueOpened,
Index: issue.Index, Index: issue.Index,
Issue: convert.ToAPIIssue(issue), Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode), Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(issue.Poster, nil), Sender: convert.ToUser(issue.Poster, nil),
}); err != nil { }); err != nil {
@ -134,11 +139,11 @@ func (a *botsNotifier) NotifyNewIssue(issue *issues_model.Issue, mentions []*use
} }
// NotifyIssueChangeStatus notifies close or reopen issue to notifiers // NotifyIssueChangeStatus notifies close or reopen issue to notifiers
func (a *botsNotifier) NotifyIssueChangeStatus(doer *user_model.User, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) { func (a *botsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
mode, _ := access_model.AccessLevel(issue.Poster, issue.Repo) mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
var err error var err error
if issue.IsPull { if issue.IsPull {
if err = issue.LoadPullRequest(); err != nil { if err = issue.LoadPullRequest(ctx); err != nil {
log.Error("LoadPullRequest: %v", err) log.Error("LoadPullRequest: %v", err)
return return
} }
@ -158,7 +163,7 @@ func (a *botsNotifier) NotifyIssueChangeStatus(doer *user_model.User, issue *iss
} else { } else {
apiIssue := &api.IssuePayload{ apiIssue := &api.IssuePayload{
Index: issue.Index, Index: issue.Index,
Issue: convert.ToAPIIssue(issue), Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode), Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil), Sender: convert.ToUser(doer, nil),
} }
@ -174,34 +179,34 @@ func (a *botsNotifier) NotifyIssueChangeStatus(doer *user_model.User, issue *iss
} }
} }
func (a *botsNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *issues_model.Issue, func (a *botsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
addedLabels []*issues_model.Label, removedLabels []*issues_model.Label, addedLabels []*issues_model.Label, removedLabels []*issues_model.Label,
) { ) {
var err error var err error
if err = issue.LoadRepo(db.DefaultContext); err != nil { if err = issue.LoadRepo(ctx); err != nil {
log.Error("LoadRepo: %v", err) log.Error("LoadRepo: %v", err)
return return
} }
if err = issue.LoadPoster(); err != nil { if err = issue.LoadPoster(ctx); err != nil {
log.Error("LoadPoster: %v", err) log.Error("LoadPoster: %v", err)
return return
} }
mode, _ := access_model.AccessLevel(issue.Poster, issue.Repo) mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
if issue.IsPull { if issue.IsPull {
if err = issue.LoadPullRequest(); err != nil { if err = issue.LoadPullRequest(ctx); err != nil {
log.Error("loadPullRequest: %v", err) log.Error("loadPullRequest: %v", err)
return return
} }
if err = issue.PullRequest.LoadIssue(); err != nil { if err = issue.PullRequest.LoadIssue(ctx); err != nil {
log.Error("LoadIssue: %v", err) log.Error("LoadIssue: %v", err)
return return
} }
err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{ err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
Action: api.HookIssueLabelUpdated, Action: api.HookIssueLabelUpdated,
Index: issue.Index, Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil), PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, perm.AccessModeNone), Repository: convert.ToRepo(issue.Repo, perm.AccessModeNone),
Sender: convert.ToUser(doer, nil), Sender: convert.ToUser(doer, nil),
}) })
@ -209,7 +214,7 @@ func (a *botsNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *iss
err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventIssueLabel, &api.IssuePayload{ err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventIssueLabel, &api.IssuePayload{
Action: api.HookIssueLabelUpdated, Action: api.HookIssueLabelUpdated,
Index: issue.Index, Index: issue.Index,
Issue: convert.ToAPIIssue(issue), Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode), Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil), Sender: convert.ToUser(doer, nil),
}) })
@ -220,16 +225,16 @@ func (a *botsNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *iss
} }
// NotifyCreateIssueComment notifies comment on an issue to notifiers // NotifyCreateIssueComment notifies comment on an issue to notifiers
func (a *botsNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *repo_model.Repository, func (a *botsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
) { ) {
mode, _ := access_model.AccessLevel(doer, repo) mode, _ := access_model.AccessLevel(ctx, doer, repo)
var err error var err error
if issue.IsPull { if issue.IsPull {
err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventPullRequestComment, &api.IssueCommentPayload{ err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventPullRequestComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated, Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(issue), Issue: convert.ToAPIIssue(ctx, issue),
Comment: convert.ToComment(comment), Comment: convert.ToComment(comment),
Repository: convert.ToRepo(repo, mode), Repository: convert.ToRepo(repo, mode),
Sender: convert.ToUser(doer, nil), Sender: convert.ToUser(doer, nil),
@ -238,7 +243,7 @@ func (a *botsNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *rep
} else { } else {
err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventIssueComment, &api.IssueCommentPayload{ err = notify(issue.Repo, doer, issue.Repo.DefaultBranch, webhook.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated, Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(issue), Issue: convert.ToAPIIssue(ctx, issue),
Comment: convert.ToComment(comment), Comment: convert.ToComment(comment),
Repository: convert.ToRepo(repo, mode), Repository: convert.ToRepo(repo, mode),
Sender: convert.ToUser(doer, nil), Sender: convert.ToUser(doer, nil),
@ -251,39 +256,39 @@ func (a *botsNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *rep
} }
} }
func (a *botsNotifier) NotifyNewPullRequest(pull *issues_model.PullRequest, mentions []*user_model.User) { func (a *botsNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) {
if err := pull.LoadIssue(); err != nil { if err := pull.LoadIssue(ctx); err != nil {
log.Error("pull.LoadIssue: %v", err) log.Error("pull.LoadIssue: %v", err)
return return
} }
if err := pull.Issue.LoadRepo(db.DefaultContext); err != nil { if err := pull.Issue.LoadRepo(ctx); err != nil {
log.Error("pull.Issue.LoadRepo: %v", err) log.Error("pull.Issue.LoadRepo: %v", err)
return return
} }
if err := pull.Issue.LoadPoster(); err != nil { if err := pull.Issue.LoadPoster(ctx); err != nil {
log.Error("pull.Issue.LoadPoster: %v", err) log.Error("pull.Issue.LoadPoster: %v", err)
return return
} }
mode, _ := access_model.AccessLevel(pull.Issue.Poster, pull.Issue.Repo) mode, _ := access_model.AccessLevel(ctx, pull.Issue.Poster, pull.Issue.Repo)
if err := notify(pull.Issue.Repo, pull.Issue.Poster, pull.Issue.Repo.DefaultBranch, webhook.HookEventPullRequest, &api.PullRequestPayload{ if err := notifyWithPR(pull.Issue.Repo, pull.Issue.Poster, pull.Issue.Repo.DefaultBranch, webhook.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueOpened, Action: api.HookIssueOpened,
Index: pull.Issue.Index, Index: pull.Issue.Index,
PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pull, nil), PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
Repository: convert.ToRepo(pull.Issue.Repo, mode), Repository: convert.ToRepo(pull.Issue.Repo, mode),
Sender: convert.ToUser(pull.Issue.Poster, nil), Sender: convert.ToUser(pull.Issue.Poster, nil),
}); err != nil { }, pull); err != nil {
log.Error("PrepareWebhooks: %v", err) log.Error("PrepareWebhooks: %v", err)
} }
} }
func (a *botsNotifier) NotifyRenameRepository(doer *user_model.User, repo *repo_model.Repository, oldRepoName string) { func (a *botsNotifier) NotifyRenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldRepoName string) {
} }
func (a *botsNotifier) NotifyTransferRepository(doer *user_model.User, repo *repo_model.Repository, oldOwnerName string) { func (a *botsNotifier) NotifyTransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldOwnerName string) {
} }
func (a *botsNotifier) NotifyCreateRepository(doer *user_model.User, u *user_model.User, repo *repo_model.Repository) { func (a *botsNotifier) NotifyCreateRepository(ctx context.Context, doer *user_model.User, u *user_model.User, repo *repo_model.Repository) {
if err := notify(repo, doer, repo.DefaultBranch, if err := notify(repo, doer, repo.DefaultBranch,
webhook.HookEventRepository, webhook.HookEventRepository,
&api.RepositoryPayload{ &api.RepositoryPayload{
@ -296,9 +301,9 @@ func (a *botsNotifier) NotifyCreateRepository(doer *user_model.User, u *user_mod
} }
} }
func (a *botsNotifier) NotifyForkRepository(doer *user_model.User, oldRepo, repo *repo_model.Repository) { func (a *botsNotifier) NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
oldMode, _ := access_model.AccessLevel(doer, oldRepo) oldMode, _ := access_model.AccessLevel(ctx, doer, oldRepo)
mode, _ := access_model.AccessLevel(doer, repo) mode, _ := access_model.AccessLevel(ctx, doer, repo)
// forked webhook // forked webhook
if err := notify(oldRepo, doer, oldRepo.DefaultBranch, webhook.HookEventFork, &api.ForkPayload{ if err := notify(oldRepo, doer, oldRepo.DefaultBranch, webhook.HookEventFork, &api.ForkPayload{
@ -309,7 +314,7 @@ func (a *botsNotifier) NotifyForkRepository(doer *user_model.User, oldRepo, repo
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
} }
u := repo.MustOwner() u := repo.MustOwner(ctx)
// Add to hook queue for created repo after session commit. // Add to hook queue for created repo after session commit.
if u.IsOrganization() { if u.IsOrganization() {
@ -324,7 +329,7 @@ func (a *botsNotifier) NotifyForkRepository(doer *user_model.User, oldRepo, repo
} }
} }
func (a *botsNotifier) NotifyPullRequestReview(pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) { func (a *botsNotifier) NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
var reviewHookType webhook.HookEventType var reviewHookType webhook.HookEventType
switch review.Type { switch review.Type {
@ -340,17 +345,17 @@ func (a *botsNotifier) NotifyPullRequestReview(pr *issues_model.PullRequest, rev
return return
} }
if err := pr.LoadIssue(); err != nil { if err := pr.LoadIssue(ctx); err != nil {
log.Error("pr.LoadIssue: %v", err) log.Error("pr.LoadIssue: %v", err)
return return
} }
mode, err := access_model.AccessLevel(review.Issue.Poster, review.Issue.Repo) mode, err := access_model.AccessLevel(ctx, review.Issue.Poster, review.Issue.Repo)
if err != nil { if err != nil {
log.Error("models.AccessLevel: %v", err) log.Error("models.AccessLevel: %v", err)
return return
} }
if err := notify(review.Issue.Repo, review.Reviewer, review.CommitID, reviewHookType, &api.PullRequestPayload{ if err := notifyWithPR(review.Issue.Repo, review.Reviewer, review.CommitID, reviewHookType, &api.PullRequestPayload{
Action: api.HookIssueReviewed, Action: api.HookIssueReviewed,
Index: review.Issue.Index, Index: review.Issue.Index,
PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil),
@ -360,19 +365,19 @@ func (a *botsNotifier) NotifyPullRequestReview(pr *issues_model.PullRequest, rev
Type: string(reviewHookType), Type: string(reviewHookType),
Content: review.Content, Content: review.Content,
}, },
}); err != nil { }, pr); err != nil {
log.Error("PrepareWebhooks: %v", err) log.Error("PrepareWebhooks: %v", err)
} }
} }
func (*botsNotifier) NotifyMergePullRequest(pr *issues_model.PullRequest, doer *user_model.User) { func (*botsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
// Reload pull request information. // Reload pull request information.
if err := pr.LoadAttributes(); err != nil { if err := pr.LoadAttributes(ctx); err != nil {
log.Error("LoadAttributes: %v", err) log.Error("LoadAttributes: %v", err)
return return
} }
if err := pr.LoadIssue(); err != nil { if err := pr.LoadIssue(ctx); err != nil {
log.Error("LoadAttributes: %v", err) log.Error("LoadAttributes: %v", err)
return return
} }
@ -382,7 +387,7 @@ func (*botsNotifier) NotifyMergePullRequest(pr *issues_model.PullRequest, doer *
return return
} }
mode, err := access_model.AccessLevel(doer, pr.Issue.Repo) mode, err := access_model.AccessLevel(ctx, doer, pr.Issue.Repo)
if err != nil { if err != nil {
log.Error("models.AccessLevel: %v", err) log.Error("models.AccessLevel: %v", err)
return return
@ -397,13 +402,13 @@ func (*botsNotifier) NotifyMergePullRequest(pr *issues_model.PullRequest, doer *
Action: api.HookIssueClosed, Action: api.HookIssueClosed,
} }
err = notify(pr.Issue.Repo, doer, pr.MergedCommitID, webhook.HookEventPullRequest, apiPullRequest) err = notifyWithPR(pr.Issue.Repo, doer, pr.MergedCommitID, webhook.HookEventPullRequest, apiPullRequest, pr)
if err != nil { if err != nil {
log.Error("PrepareWebhooks: %v", err) log.Error("PrepareWebhooks: %v", err)
} }
} }
func (a *botsNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { func (a *botsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("botsNofiter.NotifyPushCommits User: %s[%d] in %s[%d]", pusher.Name, pusher.ID, repo.FullName(), repo.ID)) ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("botsNofiter.NotifyPushCommits User: %s[%d] in %s[%d]", pusher.Name, pusher.ID, repo.FullName(), repo.ID))
defer finished() defer finished()
@ -427,7 +432,7 @@ func (a *botsNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_mod
}) })
} }
func (a *botsNotifier) NotifyCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { func (a *botsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
apiPusher := convert.ToUser(pusher, nil) apiPusher := convert.ToUser(pusher, nil)
apiRepo := convert.ToRepo(repo, perm.AccessModeNone) apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
refName := git.RefEndName(refFullName) refName := git.RefEndName(refFullName)
@ -443,7 +448,7 @@ func (a *botsNotifier) NotifyCreateRef(pusher *user_model.User, repo *repo_model
} }
} }
func (a *botsNotifier) NotifyDeleteRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { func (a *botsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
apiPusher := convert.ToUser(pusher, nil) apiPusher := convert.ToUser(pusher, nil)
apiRepo := convert.ToRepo(repo, perm.AccessModeNone) apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
refName := git.RefEndName(refFullName) refName := git.RefEndName(refFullName)
@ -459,7 +464,7 @@ func (a *botsNotifier) NotifyDeleteRef(pusher *user_model.User, repo *repo_model
} }
} }
func (a *botsNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { func (a *botsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
apiPusher := convert.ToUser(pusher, nil) apiPusher := convert.ToUser(pusher, nil)
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL()) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL())
if err != nil { if err != nil {
@ -483,21 +488,21 @@ func (a *botsNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *repo
} }
} }
func (a *botsNotifier) NotifySyncCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { func (a *botsNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
a.NotifyCreateRef(pusher, repo, refType, refFullName, refID) a.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID)
} }
func (a *botsNotifier) NotifySyncDeleteRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { func (a *botsNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
a.NotifyDeleteRef(pusher, repo, refType, refFullName) a.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName)
} }
func sendReleaseNofiter(doer *user_model.User, rel *repo_model.Release, ref string, action api.HookReleaseAction) { func sendReleaseNofiter(ctx context.Context, doer *user_model.User, rel *repo_model.Release, ref string, action api.HookReleaseAction) {
if err := rel.LoadAttributes(); err != nil { if err := rel.LoadAttributes(ctx); err != nil {
log.Error("LoadAttributes: %v", err) log.Error("LoadAttributes: %v", err)
return return
} }
mode, _ := access_model.AccessLevel(doer, rel.Repo) mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo)
if err := notify(rel.Repo, doer, ref, webhook.HookEventRelease, &api.ReleasePayload{ if err := notify(rel.Repo, doer, ref, webhook.HookEventRelease, &api.ReleasePayload{
Action: action, Action: action,
Release: convert.ToRelease(rel), Release: convert.ToRelease(rel),
@ -508,23 +513,23 @@ func sendReleaseNofiter(doer *user_model.User, rel *repo_model.Release, ref stri
} }
} }
func (a *botsNotifier) NotifyNewRelease(rel *repo_model.Release) { func (a *botsNotifier) NotifyNewRelease(ctx context.Context, rel *repo_model.Release) {
sendReleaseNofiter(rel.Publisher, rel, rel.Sha1, api.HookReleasePublished) sendReleaseNofiter(ctx, rel.Publisher, rel, rel.Sha1, api.HookReleasePublished)
} }
func (a *botsNotifier) NotifyUpdateRelease(doer *user_model.User, rel *repo_model.Release) { func (a *botsNotifier) NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
sendReleaseNofiter(doer, rel, rel.Sha1, api.HookReleaseUpdated) sendReleaseNofiter(ctx, doer, rel, rel.Sha1, api.HookReleaseUpdated)
} }
func (a *botsNotifier) NotifyDeleteRelease(doer *user_model.User, rel *repo_model.Release) { func (a *botsNotifier) NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
sendReleaseNofiter(doer, rel, rel.Sha1, api.HookReleaseDeleted) sendReleaseNofiter(ctx, doer, rel, rel.Sha1, api.HookReleaseDeleted)
} }
func (a *botsNotifier) NotifyPackageCreate(doer *user_model.User, pd *packages_model.PackageDescriptor) { func (a *botsNotifier) NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
notifyPackage(doer, pd, api.HookPackageCreated) notifyPackage(doer, pd, api.HookPackageCreated)
} }
func (a *botsNotifier) NotifyPackageDelete(doer *user_model.User, pd *packages_model.PackageDescriptor) { func (a *botsNotifier) NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
notifyPackage(doer, pd, api.HookPackageDeleted) notifyPackage(doer, pd, api.HookPackageDeleted)
} }