Skip to content

Commit 7c75f1d

Browse files
sillyguodongGiteaBot
authored andcommitted
Fix missing commit status in PR which from forked repo (go-gitea#23351)
close: go-gitea#23347 ### Reference and Inference According to Github REST API [doc](https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#list-commit-statuses-for-a-reference): 1. The `Drone CI` that can create some commit status by [API](https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#create-a-commit-status) is enabled in `go-gitea/gitea`. So I tried to call the API to get a commit status list of a PR which is commited to upstream repo(`go-gitea/gitea`). As a result, the API returned a array of commit status. ![image](https://user-images.githubusercontent.com/33891828/223913371-313d047a-5e2e-484c-b13e-dcd38748703e.png) 2. Then I tried to call the API to get commit status list of the reference which of the `SHA` is the same as step 1 in the repo which is forked from `go-gitea/gitea`. But I got a empty array. ![image](https://user-images.githubusercontent.com/33891828/223930827-17a64d3c-f466-4980-897c-77fe386c4d3b.png) So, I believe it that: 1. The commit status is not shared between upstream repo and forked repo. 2. The coomit status is bound to a repo that performs actions. (Gitea's logic is the same) ### Cause During debugging, I found it that commit status are not stored in the DB as expected. So, I located the following code: https://github.com/go-gitea/gitea/blob/8cadd51bf295e6ff36ac36efed68cc5de34c9382/services/actions/commit_status.go#L18-L26 When I create a PR, the type of `event` is `pull request`, not `push`. So the code return function directly. ### Screenshot ![image](https://user-images.githubusercontent.com/33891828/223939339-dadf539c-1fdd-40c4-96e9-2e4fa733f531.png) ![image](https://user-images.githubusercontent.com/33891828/223939519-edb02bf0-2478-4ea5-9366-be85468f02db.png) ![image](https://user-images.githubusercontent.com/33891828/223939557-ec6f1375-5536-400e-8987-fb7d2fd452fa.png) ### Other In this PR, I also fix the problem of missing icon which represents running in PRs list. ![image](https://user-images.githubusercontent.com/33891828/223939898-2a0339e4-713f-4c7b-9d99-2250a43f3457.png) ![image](https://user-images.githubusercontent.com/33891828/223939979-037a975f-5ced-480c-bac7-0ee00ebfff4b.png)
1 parent c3c0710 commit 7c75f1d

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

models/actions/run.go

+11
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {
126126
return nil, fmt.Errorf("event %s is not a push event", run.Event)
127127
}
128128

129+
func (run *ActionRun) GetPullRequestEventPayload() (*api.PullRequestPayload, error) {
130+
if run.Event == webhook_module.HookEventPullRequest {
131+
var payload api.PullRequestPayload
132+
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
133+
return nil, err
134+
}
135+
return &payload, nil
136+
}
137+
return nil, fmt.Errorf("event %s is not a pull request event", run.Event)
138+
}
139+
129140
func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) error {
130141
_, err := db.GetEngine(ctx).ID(repo.ID).
131142
SetExpr("num_action_runs",

services/actions/commit_status.go

+48-23
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,60 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
2121
}
2222

2323
run := job.Run
24-
if run.Event != webhook_module.HookEventPush {
25-
return nil
26-
}
24+
var (
25+
sha string
26+
creatorID int64
27+
)
2728

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+
}
3235

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+
}
4245

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
4669
}
4770

4871
repo := run.Repo
49-
sha := payload.HeadCommit.ID
5072
ctxname := job.Name
5173
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+
}
5378
if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}); err == nil {
5479
for _, v := range statuses {
5580
if v.Context == ctxname {
@@ -65,14 +90,14 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
6590

6691
if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{
6792
Repo: repo,
68-
SHA: payload.HeadCommit.ID,
93+
SHA: sha,
6994
Creator: creator,
7095
CommitStatus: &git_model.CommitStatus{
7196
SHA: sha,
7297
TargetURL: run.Link(),
7398
Description: "",
7499
Context: ctxname,
75-
CreatorID: payload.Pusher.ID,
100+
CreatorID: creatorID,
76101
State: state,
77102
},
78103
}); err != nil {

templates/repo/commit_status.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{{if eq .State "pending"}}
22
{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}}
33
{{end}}
4+
{{if eq .State "running"}}
5+
{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}}
6+
{{end}}
47
{{if eq .State "success"}}
58
{{svg "octicon-check" 18 "commit-status icon text green"}}
69
{{end}}

0 commit comments

Comments
 (0)