mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-23 22:05:51 +02:00
feat: bots_service.CreateCommitStatus
This commit is contained in:
parent
8f2efdaf60
commit
564937a657
@ -11,8 +11,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
bots_model "code.gitea.io/gitea/models/bots"
|
bots_model "code.gitea.io/gitea/models/bots"
|
||||||
git_model "code.gitea.io/gitea/models/git"
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
|
||||||
"code.gitea.io/gitea/models/webhook"
|
"code.gitea.io/gitea/models/webhook"
|
||||||
"code.gitea.io/gitea/modules/bots"
|
"code.gitea.io/gitea/modules/bots"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
@ -20,6 +18,7 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
bot_service "code.gitea.io/gitea/services/bots"
|
bot_service "code.gitea.io/gitea/services/bots"
|
||||||
|
bots_service "code.gitea.io/gitea/services/bots"
|
||||||
secret_service "code.gitea.io/gitea/services/secrets"
|
secret_service "code.gitea.io/gitea/services/secrets"
|
||||||
|
|
||||||
runnerv1 "code.gitea.io/bots-proto-go/runner/v1"
|
runnerv1 "code.gitea.io/bots-proto-go/runner/v1"
|
||||||
@ -177,32 +176,9 @@ func (s *Service) UpdateTask(
|
|||||||
return nil, status.Errorf(codes.Internal, "load run: %v", err)
|
return nil, status.Errorf(codes.Internal, "load run: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if task.Job.Run.Event == webhook.HookEventPush {
|
if err := bots_service.CreateCommitStatus(ctx, task); err != nil {
|
||||||
payload, err := task.Job.Run.GetPushEventPayload()
|
log.Error("Update commit status failed: %v", err)
|
||||||
if err != nil {
|
// go on
|
||||||
return nil, status.Errorf(codes.Internal, "GetPushEventPayload: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
creator, err := user_model.GetUserByID(payload.Pusher.ID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, status.Errorf(codes.Internal, "GetUserByID: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := git_model.NewCommitStatus(git_model.NewCommitStatusOptions{
|
|
||||||
Repo: task.Job.Run.Repo,
|
|
||||||
SHA: payload.HeadCommit.ID,
|
|
||||||
Creator: creator,
|
|
||||||
CommitStatus: &git_model.CommitStatus{
|
|
||||||
SHA: payload.HeadCommit.ID,
|
|
||||||
TargetURL: task.Job.Run.HTMLURL(),
|
|
||||||
Description: "",
|
|
||||||
Context: task.Job.Name,
|
|
||||||
CreatorID: payload.Pusher.ID,
|
|
||||||
State: toCommitStatus(task.Job.Status),
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
log.Error("Update commit status failed: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Msg.State.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
if req.Msg.State.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
||||||
|
@ -10,11 +10,15 @@ import (
|
|||||||
|
|
||||||
bots_model "code.gitea.io/gitea/models/bots"
|
bots_model "code.gitea.io/gitea/models/bots"
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
git_model "code.gitea.io/gitea/models/git"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/models/webhook"
|
||||||
bots_module "code.gitea.io/gitea/modules/bots"
|
bots_module "code.gitea.io/gitea/modules/bots"
|
||||||
"code.gitea.io/gitea/modules/graceful"
|
"code.gitea.io/gitea/modules/graceful"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/queue"
|
"code.gitea.io/gitea/modules/queue"
|
||||||
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
@ -61,3 +65,77 @@ func DeleteResourceOfRepository(ctx context.Context, repo *repo_model.Repository
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateCommitStatus(ctx context.Context, task *bots_model.BotTask) error {
|
||||||
|
if err := task.LoadJob(ctx); err != nil {
|
||||||
|
return fmt.Errorf("load job: %w", err)
|
||||||
|
}
|
||||||
|
if err := task.Job.LoadAttributes(ctx); err != nil {
|
||||||
|
return fmt.Errorf("load run: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if task.Job.Run.Event != webhook.HookEventPush {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := task.Job.Run.GetPushEventPayload()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("GetPushEventPayload: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
creator, err := user_model.GetUserByID(payload.Pusher.ID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("GetUserByID: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo := task.Job.Run.Repo
|
||||||
|
sha := payload.HeadCommit.ID
|
||||||
|
ctxname := task.Job.Name
|
||||||
|
state := toCommitStatus(task.Job.Status)
|
||||||
|
|
||||||
|
if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}); err != nil {
|
||||||
|
return fmt.Errorf("GetLatestCommitStatus: %w", err)
|
||||||
|
} else {
|
||||||
|
for _, status := range statuses {
|
||||||
|
if status.Context == ctxname {
|
||||||
|
if status.State == state {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := git_model.NewCommitStatus(git_model.NewCommitStatusOptions{
|
||||||
|
Repo: repo,
|
||||||
|
SHA: payload.HeadCommit.ID,
|
||||||
|
Creator: creator,
|
||||||
|
CommitStatus: &git_model.CommitStatus{
|
||||||
|
SHA: sha,
|
||||||
|
TargetURL: task.Job.Run.HTMLURL(),
|
||||||
|
Description: "",
|
||||||
|
Context: ctxname,
|
||||||
|
CreatorID: payload.Pusher.ID,
|
||||||
|
State: state,
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("NewCommitStatus: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toCommitStatus(status bots_model.Status) api.CommitStatusState {
|
||||||
|
switch status {
|
||||||
|
case bots_model.StatusSuccess:
|
||||||
|
return api.CommitStatusSuccess
|
||||||
|
case bots_model.StatusFailure, bots_model.StatusCancelled, bots_model.StatusSkipped:
|
||||||
|
return api.CommitStatusFailure
|
||||||
|
case bots_model.StatusWaiting, bots_model.StatusBlocked:
|
||||||
|
return api.CommitStatusPending
|
||||||
|
case bots_model.StatusRunning:
|
||||||
|
return api.CommitStatusRunning
|
||||||
|
default:
|
||||||
|
return api.CommitStatusError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user