Move some Actions related functions from routers
to services
(#33280)
Move the main logic of `generateTaskContext` and `findTaskNeeds` to the `services` layer. This is a part of #32751, since we need the git context and `needs` to parse the concurrency expressions. --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> (cherry picked from commit d0962ce3da424f0a1df2acf595b20066d6e55128) Conflicts: routers/api/actions/runner/main_test.go routers/api/actions/runner/utils.go services/actions/context_test.go services/actions/init_test.go tests/integration/actions_job_test.go simple conflicts related to ref_type": string(refName.RefType()), // string, The type of ref that triggered the workflow run. Valid values are branch or tag. Use env GITEA_RUNNER_REGISTRATION_TOKEN as global runner token (#32946)
This commit is contained in:
parent
6ffa454979
commit
06dfcc1a45
4 changed files with 271 additions and 136 deletions
|
@ -4,22 +4,28 @@
|
|||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestJobWithNeeds(t *testing.T) {
|
||||
|
@ -354,6 +360,95 @@ jobs:
|
|||
})
|
||||
}
|
||||
|
||||
func TestActionsGiteaContext(t *testing.T) {
|
||||
if !setting.Database.Type.IsSQLite3() {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
user2Session := loginUser(t, user2.Name)
|
||||
user2Token := getTokenForLoggedInUser(t, user2Session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
apiBaseRepo := createActionsTestRepo(t, user2Token, "actions-gitea-context", false)
|
||||
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiBaseRepo.ID})
|
||||
user2APICtx := NewAPITestContext(t, baseRepo.OwnerName, baseRepo.Name, auth_model.AccessTokenScopeWriteRepository)
|
||||
|
||||
runner := newMockRunner()
|
||||
runner.registerAsRepoRunner(t, baseRepo.OwnerName, baseRepo.Name, "mock-runner", []string{"ubuntu-latest"})
|
||||
|
||||
// init the workflow
|
||||
wfTreePath := ".gitea/workflows/pull.yml"
|
||||
wfFileContent := `name: Pull Request
|
||||
on: pull_request
|
||||
jobs:
|
||||
wf1-job:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo 'test the pull'
|
||||
`
|
||||
opts := getWorkflowCreateFileOptions(user2, baseRepo.DefaultBranch, fmt.Sprintf("create %s", wfTreePath), wfFileContent)
|
||||
createWorkflowFile(t, user2Token, baseRepo.OwnerName, baseRepo.Name, wfTreePath, opts)
|
||||
// user2 creates a pull request
|
||||
doAPICreateFile(user2APICtx, "user2-patch.txt", &api.CreateFileOptions{
|
||||
FileOptions: api.FileOptions{
|
||||
NewBranchName: "user2/patch-1",
|
||||
Message: "create user2-patch.txt",
|
||||
Author: api.Identity{
|
||||
Name: user2.Name,
|
||||
Email: user2.Email,
|
||||
},
|
||||
Committer: api.Identity{
|
||||
Name: user2.Name,
|
||||
Email: user2.Email,
|
||||
},
|
||||
Dates: api.CommitDateOptions{
|
||||
Author: time.Now(),
|
||||
Committer: time.Now(),
|
||||
},
|
||||
},
|
||||
ContentBase64: base64.StdEncoding.EncodeToString([]byte("user2-fix")),
|
||||
})(t)
|
||||
apiPull, err := doAPICreatePullRequest(user2APICtx, baseRepo.OwnerName, baseRepo.Name, baseRepo.DefaultBranch, "user2/patch-1")(t)
|
||||
require.NoError(t, err)
|
||||
task := runner.fetchTask(t)
|
||||
gtCtx := task.Context.GetFields()
|
||||
actionTask := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: task.Id})
|
||||
actionRunJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: actionTask.JobID})
|
||||
actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: actionRunJob.RunID})
|
||||
require.NoError(t, actionRun.LoadAttributes(context.Background()))
|
||||
|
||||
assert.Equal(t, user2.Name, gtCtx["actor"].GetStringValue())
|
||||
assert.Equal(t, setting.AppURL+"api/v1", gtCtx["api_url"].GetStringValue())
|
||||
assert.Equal(t, apiPull.Base.Ref, gtCtx["base_ref"].GetStringValue())
|
||||
runEvent := map[string]any{}
|
||||
require.NoError(t, json.Unmarshal([]byte(actionRun.EventPayload), &runEvent))
|
||||
assert.True(t, reflect.DeepEqual(gtCtx["event"].GetStructValue().AsMap(), runEvent))
|
||||
assert.Equal(t, actionRun.TriggerEvent, gtCtx["event_name"].GetStringValue())
|
||||
assert.Equal(t, apiPull.Head.Ref, gtCtx["head_ref"].GetStringValue())
|
||||
assert.Equal(t, actionRunJob.JobID, gtCtx["job"].GetStringValue())
|
||||
assert.Equal(t, actionRun.Ref, gtCtx["ref"].GetStringValue())
|
||||
assert.Equal(t, (git.RefName(actionRun.Ref)).ShortName(), gtCtx["ref_name"].GetStringValue())
|
||||
assert.False(t, gtCtx["ref_protected"].GetBoolValue())
|
||||
assert.Equal(t, (git.RefName(actionRun.Ref)).RefType(), gtCtx["ref_type"].GetStringValue())
|
||||
assert.Equal(t, actionRun.Repo.OwnerName+"/"+actionRun.Repo.Name, gtCtx["repository"].GetStringValue())
|
||||
assert.Equal(t, actionRun.Repo.OwnerName, gtCtx["repository_owner"].GetStringValue())
|
||||
assert.Equal(t, actionRun.Repo.HTMLURL(), gtCtx["repositoryUrl"].GetStringValue())
|
||||
assert.Equal(t, fmt.Sprint(actionRunJob.RunID), gtCtx["run_id"].GetStringValue())
|
||||
assert.Equal(t, fmt.Sprint(actionRun.Index), gtCtx["run_number"].GetStringValue())
|
||||
assert.Equal(t, fmt.Sprint(actionRunJob.Attempt), gtCtx["run_attempt"].GetStringValue())
|
||||
assert.Equal(t, "Actions", gtCtx["secret_source"].GetStringValue())
|
||||
assert.Equal(t, setting.AppURL, gtCtx["server_url"].GetStringValue())
|
||||
assert.Equal(t, actionRun.CommitSHA, gtCtx["sha"].GetStringValue())
|
||||
assert.Equal(t, actionRun.WorkflowID, gtCtx["workflow"].GetStringValue())
|
||||
assert.Equal(t, setting.Actions.DefaultActionsURL.URL(), gtCtx["gitea_default_actions_url"].GetStringValue())
|
||||
token := gtCtx["token"].GetStringValue()
|
||||
assert.Equal(t, actionTask.TokenLastEight, token[len(token)-8:])
|
||||
|
||||
doAPIDeleteRepository(user2APICtx)(t)
|
||||
})
|
||||
}
|
||||
|
||||
func createActionsTestRepo(t *testing.T, authToken, repoName string, isPrivate bool) *api.Repository {
|
||||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{
|
||||
Name: repoName,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue