@@ -21,35 +21,60 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
21
21
}
22
22
23
23
run := job .Run
24
- if run .Event != webhook_module .HookEventPush {
25
- return nil
26
- }
24
+ var (
25
+ sha string
26
+ creatorID int64
27
+ )
27
28
28
- payload , err := run .GetPushEventPayload ()
29
- if err != nil {
30
- return fmt .Errorf ("GetPushEventPayload: %w" , err )
31
- }
29
+ switch run .Event {
30
+ case webhook_module .HookEventPush :
31
+ payload , err := run .GetPushEventPayload ()
32
+ if err != nil {
33
+ return fmt .Errorf ("GetPushEventPayload: %w" , err )
34
+ }
32
35
33
- // Since the payload comes from json data, we should check if it's broken, or it will cause panic
34
- switch {
35
- case payload .Repo == nil :
36
- return fmt .Errorf ("repo is missing in event payload" )
37
- case payload .Pusher == nil :
38
- return fmt .Errorf ("pusher is missing in event payload" )
39
- case payload .HeadCommit == nil :
40
- return fmt .Errorf ("head commit is missing in event payload" )
41
- }
36
+ // Since the payload comes from json data, we should check if it's broken, or it will cause panic
37
+ switch {
38
+ case payload .Repo == nil :
39
+ return fmt .Errorf ("repo is missing in event payload" )
40
+ case payload .Pusher == nil :
41
+ return fmt .Errorf ("pusher is missing in event payload" )
42
+ case payload .HeadCommit == nil :
43
+ return fmt .Errorf ("head commit is missing in event payload" )
44
+ }
42
45
43
- creator , err := user_model .GetUserByID (ctx , payload .Pusher .ID )
44
- if err != nil {
45
- return fmt .Errorf ("GetUserByID: %w" , err )
46
+ sha = payload .HeadCommit .ID
47
+ creatorID = payload .Pusher .ID
48
+ case webhook_module .HookEventPullRequest :
49
+ payload , err := run .GetPullRequestEventPayload ()
50
+ if err != nil {
51
+ return fmt .Errorf ("GetPullRequestEventPayload: %w" , err )
52
+ }
53
+
54
+ switch {
55
+ case payload .PullRequest == nil :
56
+ return fmt .Errorf ("pull request is missing in event payload" )
57
+ case payload .PullRequest .Head == nil :
58
+ return fmt .Errorf ("head of pull request is missing in event payload" )
59
+ case payload .PullRequest .Head .Repository == nil :
60
+ return fmt .Errorf ("head repository of pull request is missing in event payload" )
61
+ case payload .PullRequest .Head .Repository .Owner == nil :
62
+ return fmt .Errorf ("owner of head repository of pull request is missing in evnt payload" )
63
+ }
64
+
65
+ sha = payload .PullRequest .Head .Sha
66
+ creatorID = payload .PullRequest .Head .Repository .Owner .ID
67
+ default :
68
+ return nil
46
69
}
47
70
48
71
repo := run .Repo
49
- sha := payload .HeadCommit .ID
50
72
ctxname := job .Name
51
73
state := toCommitStatus (job .Status )
52
-
74
+ creator , err := user_model .GetUserByID (ctx , creatorID )
75
+ if err != nil {
76
+ return fmt .Errorf ("GetUserByID: %w" , err )
77
+ }
53
78
if statuses , _ , err := git_model .GetLatestCommitStatus (ctx , repo .ID , sha , db.ListOptions {}); err == nil {
54
79
for _ , v := range statuses {
55
80
if v .Context == ctxname {
@@ -65,14 +90,14 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
65
90
66
91
if err := git_model .NewCommitStatus (ctx , git_model.NewCommitStatusOptions {
67
92
Repo : repo ,
68
- SHA : payload . HeadCommit . ID ,
93
+ SHA : sha ,
69
94
Creator : creator ,
70
95
CommitStatus : & git_model.CommitStatus {
71
96
SHA : sha ,
72
97
TargetURL : run .Link (),
73
98
Description : "" ,
74
99
Context : ctxname ,
75
- CreatorID : payload . Pusher . ID ,
100
+ CreatorID : creatorID ,
76
101
State : state ,
77
102
},
78
103
}); err != nil {
0 commit comments