code.gitea.io/gitea@v1.21.7/services/convert/status.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package convert 5 6 import ( 7 "context" 8 9 git_model "code.gitea.io/gitea/models/git" 10 user_model "code.gitea.io/gitea/models/user" 11 api "code.gitea.io/gitea/modules/structs" 12 ) 13 14 // ToCommitStatus converts git_model.CommitStatus to api.CommitStatus 15 func ToCommitStatus(ctx context.Context, status *git_model.CommitStatus) *api.CommitStatus { 16 apiStatus := &api.CommitStatus{ 17 Created: status.CreatedUnix.AsTime(), 18 Updated: status.CreatedUnix.AsTime(), 19 State: status.State, 20 TargetURL: status.TargetURL, 21 Description: status.Description, 22 ID: status.Index, 23 URL: status.APIURL(ctx), 24 Context: status.Context, 25 } 26 27 if status.CreatorID != 0 { 28 creator, _ := user_model.GetUserByID(ctx, status.CreatorID) 29 apiStatus.Creator = ToUser(ctx, creator, nil) 30 } 31 32 return apiStatus 33 } 34 35 // ToCombinedStatus converts List of CommitStatus to a CombinedStatus 36 func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, repo *api.Repository) *api.CombinedStatus { 37 if len(statuses) == 0 { 38 return nil 39 } 40 41 retStatus := &api.CombinedStatus{ 42 SHA: statuses[0].SHA, 43 TotalCount: len(statuses), 44 Repository: repo, 45 URL: "", 46 } 47 48 retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses)) 49 for _, status := range statuses { 50 retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status)) 51 if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) { 52 retStatus.State = status.State 53 } 54 } 55 // According to https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#get-the-combined-status-for-a-specific-reference 56 // > Additionally, a combined state is returned. The state is one of: 57 // > failure if any of the contexts report as error or failure 58 // > pending if there are no statuses or a context is pending 59 // > success if the latest status for all contexts is success 60 if retStatus.State.IsError() { 61 retStatus.State = api.CommitStatusFailure 62 } 63 64 return retStatus 65 }