You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

status.go 2.0KB

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