mirror of
				https://gitcode.com/gitea/gitea.git
				synced 2025-10-25 12:26:40 +08:00 
			
		
		
		
	Fix missing commit status in PR which from forked repo (#23351)
close: #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.

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.

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:
8cadd51bf2/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



### Other 
In this PR, I also fix the problem of missing icon which represents
running in PRs list.


			
			
This commit is contained in:
		| @ -128,6 +128,17 @@ func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) { | |||||||
| 	return nil, fmt.Errorf("event %s is not a push event", run.Event) | 	return nil, fmt.Errorf("event %s is not a push event", run.Event) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (run *ActionRun) GetPullRequestEventPayload() (*api.PullRequestPayload, error) { | ||||||
|  | 	if run.Event == webhook_module.HookEventPullRequest { | ||||||
|  | 		var payload api.PullRequestPayload | ||||||
|  | 		if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 		return &payload, nil | ||||||
|  | 	} | ||||||
|  | 	return nil, fmt.Errorf("event %s is not a pull request event", run.Event) | ||||||
|  | } | ||||||
|  |  | ||||||
| func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) error { | func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) error { | ||||||
| 	_, err := db.GetEngine(ctx).ID(repo.ID). | 	_, err := db.GetEngine(ctx).ID(repo.ID). | ||||||
| 		SetExpr("num_action_runs", | 		SetExpr("num_action_runs", | ||||||
|  | |||||||
| @ -21,35 +21,60 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	run := job.Run | 	run := job.Run | ||||||
| 	if run.Event != webhook_module.HookEventPush { | 	var ( | ||||||
|  | 		sha       string | ||||||
|  | 		creatorID int64 | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	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") | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		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 | 		return nil | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	payload, err := run.GetPushEventPayload() | 	repo := run.Repo | ||||||
| 	if err != nil { | 	ctxname := job.Name | ||||||
| 		return fmt.Errorf("GetPushEventPayload: %w", err) | 	state := toCommitStatus(job.Status) | ||||||
| 	} | 	creator, err := user_model.GetUserByID(ctx, creatorID) | ||||||
|  |  | ||||||
| 	// 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 { | 	if err != nil { | ||||||
| 		return fmt.Errorf("GetUserByID: %w", err) | 		return fmt.Errorf("GetUserByID: %w", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	repo := run.Repo |  | ||||||
| 	sha := payload.HeadCommit.ID |  | ||||||
| 	ctxname := job.Name |  | ||||||
| 	state := toCommitStatus(job.Status) |  | ||||||
|  |  | ||||||
| 	if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}); err == nil { | 	if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}); err == nil { | ||||||
| 		for _, v := range statuses { | 		for _, v := range statuses { | ||||||
| 			if v.Context == ctxname { | 			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{ | 	if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{ | ||||||
| 		Repo:    repo, | 		Repo:    repo, | ||||||
| 		SHA:     payload.HeadCommit.ID, | 		SHA:     sha, | ||||||
| 		Creator: creator, | 		Creator: creator, | ||||||
| 		CommitStatus: &git_model.CommitStatus{ | 		CommitStatus: &git_model.CommitStatus{ | ||||||
| 			SHA:         sha, | 			SHA:         sha, | ||||||
| 			TargetURL:   run.Link(), | 			TargetURL:   run.Link(), | ||||||
| 			Description: "", | 			Description: "", | ||||||
| 			Context:     ctxname, | 			Context:     ctxname, | ||||||
| 			CreatorID:   payload.Pusher.ID, | 			CreatorID:   creatorID, | ||||||
| 			State:       state, | 			State:       state, | ||||||
| 		}, | 		}, | ||||||
| 	}); err != nil { | 	}); err != nil { | ||||||
|  | |||||||
| @ -1,6 +1,9 @@ | |||||||
| {{if eq .State "pending"}} | {{if eq .State "pending"}} | ||||||
| 	{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}} | 	{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}} | ||||||
| {{end}} | {{end}} | ||||||
|  | {{if eq .State "running"}} | ||||||
|  | 	{{svg "octicon-dot-fill" 18 "commit-status icon text yellow"}} | ||||||
|  | {{end}} | ||||||
| {{if eq .State "success"}} | {{if eq .State "success"}} | ||||||
| 	{{svg "octicon-check" 18 "commit-status icon text green"}} | 	{{svg "octicon-check" 18 "commit-status icon text green"}} | ||||||
| {{end}} | {{end}} | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 sillyguodong
					sillyguodong