gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "time"
  10. actions_model "code.gitea.io/gitea/models/actions"
  11. auth_model "code.gitea.io/gitea/models/auth"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/json"
  15. "code.gitea.io/gitea/routers/web/repo/actions"
  16. runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
  17. "github.com/stretchr/testify/assert"
  18. "google.golang.org/protobuf/types/known/timestamppb"
  19. )
  20. func TestActionsDeleteRun(t *testing.T) {
  21. now := time.Now()
  22. testCase := struct {
  23. treePath string
  24. fileContent string
  25. outcomes map[string]*mockTaskOutcome
  26. expectedStatuses map[string]string
  27. }{
  28. treePath: ".gitea/workflows/test1.yml",
  29. fileContent: `name: test1
  30. on:
  31. push:
  32. paths:
  33. - .gitea/workflows/test1.yml
  34. jobs:
  35. job1:
  36. runs-on: ubuntu-latest
  37. steps:
  38. - run: echo job1
  39. job2:
  40. runs-on: ubuntu-latest
  41. steps:
  42. - run: echo job2
  43. job3:
  44. runs-on: ubuntu-latest
  45. steps:
  46. - run: echo job3
  47. `,
  48. outcomes: map[string]*mockTaskOutcome{
  49. "job1": {
  50. result: runnerv1.Result_RESULT_SUCCESS,
  51. logRows: []*runnerv1.LogRow{
  52. {
  53. Time: timestamppb.New(now.Add(4 * time.Second)),
  54. Content: " \U0001F433 docker create image",
  55. },
  56. {
  57. Time: timestamppb.New(now.Add(5 * time.Second)),
  58. Content: "job1",
  59. },
  60. {
  61. Time: timestamppb.New(now.Add(6 * time.Second)),
  62. Content: "\U0001F3C1 Job succeeded",
  63. },
  64. },
  65. },
  66. "job2": {
  67. result: runnerv1.Result_RESULT_SUCCESS,
  68. logRows: []*runnerv1.LogRow{
  69. {
  70. Time: timestamppb.New(now.Add(4 * time.Second)),
  71. Content: " \U0001F433 docker create image",
  72. },
  73. {
  74. Time: timestamppb.New(now.Add(5 * time.Second)),
  75. Content: "job2",
  76. },
  77. {
  78. Time: timestamppb.New(now.Add(6 * time.Second)),
  79. Content: "\U0001F3C1 Job succeeded",
  80. },
  81. },
  82. },
  83. "job3": {
  84. result: runnerv1.Result_RESULT_SUCCESS,
  85. logRows: []*runnerv1.LogRow{
  86. {
  87. Time: timestamppb.New(now.Add(4 * time.Second)),
  88. Content: " \U0001F433 docker create image",
  89. },
  90. {
  91. Time: timestamppb.New(now.Add(5 * time.Second)),
  92. Content: "job3",
  93. },
  94. {
  95. Time: timestamppb.New(now.Add(6 * time.Second)),
  96. Content: "\U0001F3C1 Job succeeded",
  97. },
  98. },
  99. },
  100. },
  101. expectedStatuses: map[string]string{
  102. "job1": actions_model.StatusSuccess.String(),
  103. "job2": actions_model.StatusSuccess.String(),
  104. "job3": actions_model.StatusSuccess.String(),
  105. },
  106. }
  107. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  108. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  109. session := loginUser(t, user2.Name)
  110. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
  111. apiRepo := createActionsTestRepo(t, token, "actions-delete-run-test", false)
  112. runner := newMockRunner()
  113. runner.registerAsRepoRunner(t, user2.Name, apiRepo.Name, "mock-runner", []string{"ubuntu-latest"}, false)
  114. opts := getWorkflowCreateFileOptions(user2, apiRepo.DefaultBranch, "create "+testCase.treePath, testCase.fileContent)
  115. createWorkflowFile(t, token, user2.Name, apiRepo.Name, testCase.treePath, opts)
  116. runIndex := ""
  117. for i := 0; i < len(testCase.outcomes); i++ {
  118. task := runner.fetchTask(t)
  119. jobName := getTaskJobNameByTaskID(t, token, user2.Name, apiRepo.Name, task.Id)
  120. outcome := testCase.outcomes[jobName]
  121. assert.NotNil(t, outcome)
  122. runner.execTask(t, task, outcome)
  123. runIndex = task.Context.GetFields()["run_number"].GetStringValue()
  124. assert.Equal(t, "1", runIndex)
  125. }
  126. for i := 0; i < len(testCase.outcomes); i++ {
  127. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d", user2.Name, apiRepo.Name, runIndex, i), map[string]string{
  128. "_csrf": GetUserCSRFToken(t, session),
  129. })
  130. resp := session.MakeRequest(t, req, http.StatusOK)
  131. var listResp actions.ViewResponse
  132. err := json.Unmarshal(resp.Body.Bytes(), &listResp)
  133. assert.NoError(t, err)
  134. assert.Len(t, listResp.State.Run.Jobs, 3)
  135. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d/logs", user2.Name, apiRepo.Name, runIndex, i)).
  136. AddTokenAuth(token)
  137. MakeRequest(t, req, http.StatusOK)
  138. }
  139. req := NewRequestWithValues(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s", user2.Name, apiRepo.Name, runIndex), map[string]string{
  140. "_csrf": GetUserCSRFToken(t, session),
  141. })
  142. session.MakeRequest(t, req, http.StatusOK)
  143. req = NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/delete", user2.Name, apiRepo.Name, runIndex), map[string]string{
  144. "_csrf": GetUserCSRFToken(t, session),
  145. })
  146. session.MakeRequest(t, req, http.StatusOK)
  147. req = NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/delete", user2.Name, apiRepo.Name, runIndex), map[string]string{
  148. "_csrf": GetUserCSRFToken(t, session),
  149. })
  150. session.MakeRequest(t, req, http.StatusNotFound)
  151. req = NewRequestWithValues(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s", user2.Name, apiRepo.Name, runIndex), map[string]string{
  152. "_csrf": GetUserCSRFToken(t, session),
  153. })
  154. session.MakeRequest(t, req, http.StatusNotFound)
  155. for i := 0; i < len(testCase.outcomes); i++ {
  156. req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d", user2.Name, apiRepo.Name, runIndex, i), map[string]string{
  157. "_csrf": GetUserCSRFToken(t, session),
  158. })
  159. session.MakeRequest(t, req, http.StatusNotFound)
  160. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d/logs", user2.Name, apiRepo.Name, runIndex, i)).
  161. AddTokenAuth(token)
  162. MakeRequest(t, req, http.StatusNotFound)
  163. }
  164. })
  165. }