gitea源码

workflow_run_api_check_test.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. auth_model "code.gitea.io/gitea/models/auth"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/tests"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIWorkflowRun(t *testing.T) {
  15. t.Run("AdminRuns", func(t *testing.T) {
  16. testAPIWorkflowRunBasic(t, "/api/v1/admin/actions", "User1", 802, auth_model.AccessTokenScopeReadAdmin, auth_model.AccessTokenScopeReadRepository)
  17. })
  18. t.Run("UserRuns", func(t *testing.T) {
  19. testAPIWorkflowRunBasic(t, "/api/v1/user/actions", "User2", 803, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadRepository)
  20. })
  21. t.Run("OrgRuns", func(t *testing.T) {
  22. testAPIWorkflowRunBasic(t, "/api/v1/orgs/org3/actions", "User1", 802, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadRepository)
  23. })
  24. t.Run("RepoRuns", func(t *testing.T) {
  25. testAPIWorkflowRunBasic(t, "/api/v1/repos/org3/repo5/actions", "User2", 802, auth_model.AccessTokenScopeReadRepository)
  26. })
  27. }
  28. func testAPIWorkflowRunBasic(t *testing.T, apiRootURL, userUsername string, runID int64, scope ...auth_model.AccessTokenScope) {
  29. defer tests.PrepareTestEnv(t)()
  30. token := getUserToken(t, userUsername, scope...)
  31. apiRunsURL := fmt.Sprintf("%s/%s", apiRootURL, "runs")
  32. req := NewRequest(t, "GET", apiRunsURL).AddTokenAuth(token)
  33. runnerListResp := MakeRequest(t, req, http.StatusOK)
  34. runnerList := api.ActionWorkflowRunsResponse{}
  35. DecodeJSON(t, runnerListResp, &runnerList)
  36. foundRun := false
  37. for _, run := range runnerList.Entries {
  38. // Verify filtering works
  39. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", run.Status, "", "", "", "")
  40. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, run.Conclusion, "", "", "", "", "")
  41. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", run.HeadBranch, "", "")
  42. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", run.Event, "", "", "")
  43. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", "", run.TriggerActor.UserName, "")
  44. verifyWorkflowRunCanbeFoundWithStatusFilter(t, apiRunsURL, token, run.ID, "", "", "", "", run.TriggerActor.UserName, run.HeadSha)
  45. // Verify run url works
  46. req := NewRequest(t, "GET", run.URL).AddTokenAuth(token)
  47. runResp := MakeRequest(t, req, http.StatusOK)
  48. apiRun := api.ActionWorkflowRun{}
  49. DecodeJSON(t, runResp, &apiRun)
  50. assert.Equal(t, run.ID, apiRun.ID)
  51. assert.Equal(t, run.Status, apiRun.Status)
  52. assert.Equal(t, run.Conclusion, apiRun.Conclusion)
  53. assert.Equal(t, run.Event, apiRun.Event)
  54. // Verify jobs list works
  55. req = NewRequest(t, "GET", fmt.Sprintf("%s/%s", run.URL, "jobs")).AddTokenAuth(token)
  56. jobsResp := MakeRequest(t, req, http.StatusOK)
  57. jobList := api.ActionWorkflowJobsResponse{}
  58. DecodeJSON(t, jobsResp, &jobList)
  59. if run.ID == runID {
  60. foundRun = true
  61. assert.Len(t, jobList.Entries, 1)
  62. for _, job := range jobList.Entries {
  63. // Check the jobs list of the run
  64. verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", run.URL, "jobs"), token, job.ID, "", job.Status)
  65. verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", run.URL, "jobs"), token, job.ID, job.Conclusion, "")
  66. // Check the run independent job list
  67. verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", apiRootURL, "jobs"), token, job.ID, "", job.Status)
  68. verifyWorkflowJobCanbeFoundWithStatusFilter(t, fmt.Sprintf("%s/%s", apiRootURL, "jobs"), token, job.ID, job.Conclusion, "")
  69. // Verify job url works
  70. req := NewRequest(t, "GET", job.URL).AddTokenAuth(token)
  71. jobsResp := MakeRequest(t, req, http.StatusOK)
  72. apiJob := api.ActionWorkflowJob{}
  73. DecodeJSON(t, jobsResp, &apiJob)
  74. assert.Equal(t, job.ID, apiJob.ID)
  75. assert.Equal(t, job.RunID, apiJob.RunID)
  76. assert.Equal(t, job.Status, apiJob.Status)
  77. assert.Equal(t, job.Conclusion, apiJob.Conclusion)
  78. }
  79. }
  80. }
  81. assert.True(t, foundRun, "Expected to find run with ID %d", runID)
  82. }
  83. func verifyWorkflowRunCanbeFoundWithStatusFilter(t *testing.T, runAPIURL, token string, id int64, conclusion, status, event, branch, actor, headSHA string) {
  84. filter := url.Values{}
  85. if conclusion != "" {
  86. filter.Add("status", conclusion)
  87. }
  88. if status != "" {
  89. filter.Add("status", status)
  90. }
  91. if event != "" {
  92. filter.Set("event", event)
  93. }
  94. if branch != "" {
  95. filter.Set("branch", branch)
  96. }
  97. if actor != "" {
  98. filter.Set("actor", actor)
  99. }
  100. if headSHA != "" {
  101. filter.Set("head_sha", headSHA)
  102. }
  103. req := NewRequest(t, "GET", runAPIURL+"?"+filter.Encode()).AddTokenAuth(token)
  104. runResp := MakeRequest(t, req, http.StatusOK)
  105. runList := api.ActionWorkflowRunsResponse{}
  106. DecodeJSON(t, runResp, &runList)
  107. found := false
  108. for _, run := range runList.Entries {
  109. if conclusion != "" {
  110. assert.Equal(t, conclusion, run.Conclusion)
  111. }
  112. if status != "" {
  113. assert.Equal(t, status, run.Status)
  114. }
  115. if event != "" {
  116. assert.Equal(t, event, run.Event)
  117. }
  118. if branch != "" {
  119. assert.Equal(t, branch, run.HeadBranch)
  120. }
  121. if actor != "" {
  122. assert.Equal(t, actor, run.Actor.UserName)
  123. }
  124. found = found || run.ID == id
  125. }
  126. assert.True(t, found, "Expected to find run with ID %d", id)
  127. }
  128. func verifyWorkflowJobCanbeFoundWithStatusFilter(t *testing.T, runAPIURL, token string, id int64, conclusion, status string) {
  129. filter := conclusion
  130. if filter == "" {
  131. filter = status
  132. }
  133. if filter == "" {
  134. return
  135. }
  136. req := NewRequest(t, "GET", runAPIURL+"?status="+filter).AddTokenAuth(token)
  137. jobListResp := MakeRequest(t, req, http.StatusOK)
  138. jobList := api.ActionWorkflowJobsResponse{}
  139. DecodeJSON(t, jobListResp, &jobList)
  140. found := false
  141. for _, job := range jobList.Entries {
  142. if conclusion != "" {
  143. assert.Equal(t, conclusion, job.Conclusion)
  144. } else {
  145. assert.Equal(t, status, job.Status)
  146. }
  147. found = found || job.ID == id
  148. }
  149. assert.True(t, found, "Expected to find job with ID %d", id)
  150. }