|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + |
| 12 | + actions_model "code.gitea.io/gitea/models/actions" |
| 13 | + auth_model "code.gitea.io/gitea/models/auth" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + "code.gitea.io/gitea/models/unittest" |
| 16 | + user_model "code.gitea.io/gitea/models/user" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +func TestWorkflowWithInputsContext(t *testing.T) { |
| 22 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 23 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 24 | + session := loginUser(t, user2.Name) |
| 25 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 26 | + |
| 27 | + apiRepo := createActionsTestRepo(t, token, "actions-inputs-context", false) |
| 28 | + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID}) |
| 29 | + httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository) |
| 30 | + defer doAPIDeleteRepository(httpContext)(t) |
| 31 | + |
| 32 | + wRunner := newMockRunner() |
| 33 | + wRunner.registerAsRepoRunner(t, user2.Name, repo.Name, "windows-runner", []string{"windows-runner"}, false) |
| 34 | + lRunner := newMockRunner() |
| 35 | + lRunner.registerAsRepoRunner(t, user2.Name, repo.Name, "linux-runner", []string{"linux-runner"}, false) |
| 36 | + |
| 37 | + wf1TreePath := ".gitea/workflows/test-inputs-context.yml" |
| 38 | + wf1FileContent := `name: Test Inputs Context |
| 39 | +on: |
| 40 | + workflow_dispatch: |
| 41 | + inputs: |
| 42 | + os: |
| 43 | + description: 'OS' |
| 44 | + required: true |
| 45 | + type: choice |
| 46 | + options: |
| 47 | + - linux |
| 48 | + - windows |
| 49 | +
|
| 50 | +run-name: Build APP on ${{ inputs.os }} |
| 51 | +
|
| 52 | +jobs: |
| 53 | + build: |
| 54 | + runs-on: ${{ inputs.os }}-runner |
| 55 | + steps: |
| 56 | + - run: echo 'Start building APP' |
| 57 | +` |
| 58 | + |
| 59 | + opts1 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, "create %s"+wf1TreePath, wf1FileContent) |
| 60 | + createWorkflowFile(t, token, user2.Name, repo.Name, wf1TreePath, opts1) |
| 61 | + |
| 62 | + // run the workflow with os=windows |
| 63 | + urlStr := fmt.Sprintf("/%s/%s/actions/run?workflow=%s", user2.Name, repo.Name, "test-inputs-context.yml") |
| 64 | + req := NewRequestWithValues(t, "POST", urlStr, map[string]string{ |
| 65 | + "_csrf": GetUserCSRFToken(t, session), |
| 66 | + "ref": "refs/heads/main", |
| 67 | + "os": "windows", |
| 68 | + }) |
| 69 | + session.MakeRequest(t, req, http.StatusSeeOther) |
| 70 | + |
| 71 | + // linux-runner cannot fetch the task |
| 72 | + lRunner.fetchNoTask(t) |
| 73 | + |
| 74 | + task := wRunner.fetchTask(t) |
| 75 | + _, _, run := getTaskAndJobAndRunByTaskID(t, task.Id) |
| 76 | + assert.Equal(t, "Build APP on windows", run.Title) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func getTaskAndJobAndRunByTaskID(t *testing.T, taskID int64) (*actions_model.ActionTask, *actions_model.ActionRunJob, *actions_model.ActionRun) { |
| 81 | + actionTask := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: taskID}) |
| 82 | + actionRunJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: actionTask.JobID}) |
| 83 | + actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: actionRunJob.RunID}) |
| 84 | + return actionTask, actionRunJob, actionRun |
| 85 | +} |
0 commit comments