2020-12-18 03:33:32 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-18 03:33:32 +00:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-12-03 02:48:26 +00:00
|
|
|
"context"
|
|
|
|
|
2022-06-12 15:51:54 +00:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-12-18 03:33:32 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
2022-06-12 15:51:54 +00:00
|
|
|
// ToCommitStatus converts git_model.CommitStatus to api.CommitStatus
|
2022-12-03 02:48:26 +00:00
|
|
|
func ToCommitStatus(ctx context.Context, status *git_model.CommitStatus) *api.CommitStatus {
|
2020-12-18 03:33:32 +00:00
|
|
|
apiStatus := &api.CommitStatus{
|
|
|
|
Created: status.CreatedUnix.AsTime(),
|
|
|
|
Updated: status.CreatedUnix.AsTime(),
|
|
|
|
State: status.State,
|
|
|
|
TargetURL: status.TargetURL,
|
|
|
|
Description: status.Description,
|
|
|
|
ID: status.Index,
|
2023-01-09 03:50:54 +00:00
|
|
|
URL: status.APIURL(ctx),
|
2020-12-18 03:33:32 +00:00
|
|
|
Context: status.Context,
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.CreatorID != 0 {
|
2022-12-03 02:48:26 +00:00
|
|
|
creator, _ := user_model.GetUserByID(ctx, status.CreatorID)
|
2021-03-27 16:45:26 +00:00
|
|
|
apiStatus.Creator = ToUser(creator, nil)
|
2020-12-18 03:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToCombinedStatus converts List of CommitStatus to a CombinedStatus
|
2022-12-03 02:48:26 +00:00
|
|
|
func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, repo *api.Repository) *api.CombinedStatus {
|
2020-12-18 03:33:32 +00:00
|
|
|
if len(statuses) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retStatus := &api.CombinedStatus{
|
|
|
|
SHA: statuses[0].SHA,
|
|
|
|
TotalCount: len(statuses),
|
|
|
|
Repository: repo,
|
|
|
|
URL: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
|
|
|
|
for _, status := range statuses {
|
2022-12-03 02:48:26 +00:00
|
|
|
retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
|
2020-12-18 03:33:32 +00:00
|
|
|
if status.State.NoBetterThan(retStatus.State) {
|
|
|
|
retStatus.State = status.State
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retStatus
|
|
|
|
}
|