gitea源码

status.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "slices"
  6. "code.gitea.io/gitea/modules/translation"
  7. runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
  8. )
  9. // Status represents the status of ActionRun, ActionRunJob, ActionTask, or ActionTaskStep
  10. type Status int
  11. const (
  12. StatusUnknown Status = iota // 0, consistent with runnerv1.Result_RESULT_UNSPECIFIED
  13. StatusSuccess // 1, consistent with runnerv1.Result_RESULT_SUCCESS
  14. StatusFailure // 2, consistent with runnerv1.Result_RESULT_FAILURE
  15. StatusCancelled // 3, consistent with runnerv1.Result_RESULT_CANCELLED
  16. StatusSkipped // 4, consistent with runnerv1.Result_RESULT_SKIPPED
  17. StatusWaiting // 5, isn't a runnerv1.Result
  18. StatusRunning // 6, isn't a runnerv1.Result
  19. StatusBlocked // 7, isn't a runnerv1.Result
  20. )
  21. var statusNames = map[Status]string{
  22. StatusUnknown: "unknown",
  23. StatusWaiting: "waiting",
  24. StatusRunning: "running",
  25. StatusSuccess: "success",
  26. StatusFailure: "failure",
  27. StatusCancelled: "cancelled",
  28. StatusSkipped: "skipped",
  29. StatusBlocked: "blocked",
  30. }
  31. // String returns the string name of the Status
  32. func (s Status) String() string {
  33. return statusNames[s]
  34. }
  35. // LocaleString returns the locale string name of the Status
  36. func (s Status) LocaleString(lang translation.Locale) string {
  37. return lang.TrString("actions.status." + s.String())
  38. }
  39. // IsDone returns whether the Status is final
  40. func (s Status) IsDone() bool {
  41. return s.In(StatusSuccess, StatusFailure, StatusCancelled, StatusSkipped)
  42. }
  43. // HasRun returns whether the Status is a result of running
  44. func (s Status) HasRun() bool {
  45. return s.In(StatusSuccess, StatusFailure)
  46. }
  47. func (s Status) IsUnknown() bool {
  48. return s == StatusUnknown
  49. }
  50. func (s Status) IsSuccess() bool {
  51. return s == StatusSuccess
  52. }
  53. func (s Status) IsFailure() bool {
  54. return s == StatusFailure
  55. }
  56. func (s Status) IsCancelled() bool {
  57. return s == StatusCancelled
  58. }
  59. func (s Status) IsSkipped() bool {
  60. return s == StatusSkipped
  61. }
  62. func (s Status) IsWaiting() bool {
  63. return s == StatusWaiting
  64. }
  65. func (s Status) IsRunning() bool {
  66. return s == StatusRunning
  67. }
  68. func (s Status) IsBlocked() bool {
  69. return s == StatusBlocked
  70. }
  71. // In returns whether s is one of the given statuses
  72. func (s Status) In(statuses ...Status) bool {
  73. return slices.Contains(statuses, s)
  74. }
  75. func (s Status) AsResult() runnerv1.Result {
  76. if s.IsDone() {
  77. return runnerv1.Result(s)
  78. }
  79. return runnerv1.Result_RESULT_UNSPECIFIED
  80. }