diff options
Diffstat (limited to 'services/actions/commit_status.go')
-rw-r--r-- | services/actions/commit_status.go | 71 |
1 files changed, 48 insertions, 23 deletions
diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index 4f31349352..84de106eec 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -21,35 +21,60 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er } run := job.Run - if run.Event != webhook_module.HookEventPush { - return nil - } + var ( + sha string + creatorID int64 + ) - payload, err := run.GetPushEventPayload() - if err != nil { - return fmt.Errorf("GetPushEventPayload: %w", err) - } + switch run.Event { + case webhook_module.HookEventPush: + payload, err := run.GetPushEventPayload() + if err != nil { + return fmt.Errorf("GetPushEventPayload: %w", err) + } - // Since the payload comes from json data, we should check if it's broken, or it will cause panic - switch { - case payload.Repo == nil: - return fmt.Errorf("repo is missing in event payload") - case payload.Pusher == nil: - return fmt.Errorf("pusher is missing in event payload") - case payload.HeadCommit == nil: - return fmt.Errorf("head commit is missing in event payload") - } + // Since the payload comes from json data, we should check if it's broken, or it will cause panic + switch { + case payload.Repo == nil: + return fmt.Errorf("repo is missing in event payload") + case payload.Pusher == nil: + return fmt.Errorf("pusher is missing in event payload") + case payload.HeadCommit == nil: + return fmt.Errorf("head commit is missing in event payload") + } - creator, err := user_model.GetUserByID(ctx, payload.Pusher.ID) - if err != nil { - return fmt.Errorf("GetUserByID: %w", err) + sha = payload.HeadCommit.ID + creatorID = payload.Pusher.ID + case webhook_module.HookEventPullRequest: + payload, err := run.GetPullRequestEventPayload() + if err != nil { + return fmt.Errorf("GetPullRequestEventPayload: %w", err) + } + + switch { + case payload.PullRequest == nil: + return fmt.Errorf("pull request is missing in event payload") + case payload.PullRequest.Head == nil: + return fmt.Errorf("head of pull request is missing in event payload") + case payload.PullRequest.Head.Repository == nil: + return fmt.Errorf("head repository of pull request is missing in event payload") + case payload.PullRequest.Head.Repository.Owner == nil: + return fmt.Errorf("owner of head repository of pull request is missing in evnt payload") + } + + sha = payload.PullRequest.Head.Sha + creatorID = payload.PullRequest.Head.Repository.Owner.ID + default: + return nil } repo := run.Repo - sha := payload.HeadCommit.ID ctxname := job.Name state := toCommitStatus(job.Status) - + creator, err := user_model.GetUserByID(ctx, creatorID) + if err != nil { + return fmt.Errorf("GetUserByID: %w", err) + } if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}); err == nil { for _, v := range statuses { if v.Context == ctxname { @@ -65,14 +90,14 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{ Repo: repo, - SHA: payload.HeadCommit.ID, + SHA: sha, Creator: creator, CommitStatus: &git_model.CommitStatus{ SHA: sha, TargetURL: run.Link(), Description: "", Context: ctxname, - CreatorID: payload.Pusher.ID, + CreatorID: creatorID, State: state, }, }); err != nil { |