|
4 | 4 | package integration
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "context" |
7 | 8 | "encoding/base64"
|
8 | 9 | "fmt"
|
9 | 10 | "net/http"
|
10 | 11 | "net/url"
|
| 12 | + "reflect" |
11 | 13 | "testing"
|
12 | 14 | "time"
|
13 | 15 |
|
14 | 16 | actions_model "code.gitea.io/gitea/models/actions"
|
15 | 17 | auth_model "code.gitea.io/gitea/models/auth"
|
| 18 | + repo_model "code.gitea.io/gitea/models/repo" |
16 | 19 | "code.gitea.io/gitea/models/unittest"
|
17 | 20 | user_model "code.gitea.io/gitea/models/user"
|
| 21 | + "code.gitea.io/gitea/modules/git" |
| 22 | + "code.gitea.io/gitea/modules/json" |
| 23 | + "code.gitea.io/gitea/modules/setting" |
18 | 24 | api "code.gitea.io/gitea/modules/structs"
|
19 | 25 |
|
20 | 26 | runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
@@ -347,6 +353,91 @@ jobs:
|
347 | 353 | })
|
348 | 354 | }
|
349 | 355 |
|
| 356 | +func TestActionsGiteaContext(t *testing.T) { |
| 357 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 358 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 359 | + user2Session := loginUser(t, user2.Name) |
| 360 | + user2Token := getTokenForLoggedInUser(t, user2Session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 361 | + |
| 362 | + apiBaseRepo := createActionsTestRepo(t, user2Token, "actions-gitea-context", false) |
| 363 | + baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiBaseRepo.ID}) |
| 364 | + user2APICtx := NewAPITestContext(t, baseRepo.OwnerName, baseRepo.Name, auth_model.AccessTokenScopeWriteRepository) |
| 365 | + |
| 366 | + runner := newMockRunner() |
| 367 | + runner.registerAsRepoRunner(t, baseRepo.OwnerName, baseRepo.Name, "mock-runner", []string{"ubuntu-latest"}) |
| 368 | + |
| 369 | + // init the workflow |
| 370 | + wfTreePath := ".gitea/workflows/pull.yml" |
| 371 | + wfFileContent := `name: Pull Request |
| 372 | +on: pull_request |
| 373 | +jobs: |
| 374 | + wf1-job: |
| 375 | + runs-on: ubuntu-latest |
| 376 | + steps: |
| 377 | + - run: echo 'test the pull' |
| 378 | +` |
| 379 | + opts := getWorkflowCreateFileOptions(user2, baseRepo.DefaultBranch, fmt.Sprintf("create %s", wfTreePath), wfFileContent) |
| 380 | + createWorkflowFile(t, user2Token, baseRepo.OwnerName, baseRepo.Name, wfTreePath, opts) |
| 381 | + // user2 creates a pull request |
| 382 | + doAPICreateFile(user2APICtx, "user2-patch.txt", &api.CreateFileOptions{ |
| 383 | + FileOptions: api.FileOptions{ |
| 384 | + NewBranchName: "user2/patch-1", |
| 385 | + Message: "create user2-patch.txt", |
| 386 | + Author: api.Identity{ |
| 387 | + Name: user2.Name, |
| 388 | + Email: user2.Email, |
| 389 | + }, |
| 390 | + Committer: api.Identity{ |
| 391 | + Name: user2.Name, |
| 392 | + Email: user2.Email, |
| 393 | + }, |
| 394 | + Dates: api.CommitDateOptions{ |
| 395 | + Author: time.Now(), |
| 396 | + Committer: time.Now(), |
| 397 | + }, |
| 398 | + }, |
| 399 | + ContentBase64: base64.StdEncoding.EncodeToString([]byte("user2-fix")), |
| 400 | + })(t) |
| 401 | + apiPull, err := doAPICreatePullRequest(user2APICtx, baseRepo.OwnerName, baseRepo.Name, baseRepo.DefaultBranch, "user2/patch-1")(t) |
| 402 | + assert.NoError(t, err) |
| 403 | + task := runner.fetchTask(t) |
| 404 | + gtCtx := task.Context.GetFields() |
| 405 | + actionTask := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: task.Id}) |
| 406 | + actionRunJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: actionTask.JobID}) |
| 407 | + actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: actionRunJob.RunID}) |
| 408 | + assert.NoError(t, actionRun.LoadAttributes(context.Background())) |
| 409 | + |
| 410 | + assert.Equal(t, user2.Name, gtCtx["actor"].GetStringValue()) |
| 411 | + assert.Equal(t, setting.AppURL+"api/v1", gtCtx["api_url"].GetStringValue()) |
| 412 | + assert.Equal(t, apiPull.Base.Ref, gtCtx["base_ref"].GetStringValue()) |
| 413 | + runEvent := map[string]any{} |
| 414 | + assert.NoError(t, json.Unmarshal([]byte(actionRun.EventPayload), &runEvent)) |
| 415 | + assert.True(t, reflect.DeepEqual(gtCtx["event"].GetStructValue().AsMap(), runEvent)) |
| 416 | + assert.Equal(t, actionRun.TriggerEvent, gtCtx["event_name"].GetStringValue()) |
| 417 | + assert.Equal(t, apiPull.Head.Ref, gtCtx["head_ref"].GetStringValue()) |
| 418 | + assert.Equal(t, actionRunJob.JobID, gtCtx["job"].GetStringValue()) |
| 419 | + assert.Equal(t, actionRun.Ref, gtCtx["ref"].GetStringValue()) |
| 420 | + assert.Equal(t, (git.RefName(actionRun.Ref)).ShortName(), gtCtx["ref_name"].GetStringValue()) |
| 421 | + assert.Equal(t, false, gtCtx["ref_protected"].GetBoolValue()) |
| 422 | + assert.Equal(t, string((git.RefName(actionRun.Ref)).RefType()), gtCtx["ref_type"].GetStringValue()) |
| 423 | + assert.Equal(t, actionRun.Repo.OwnerName+"/"+actionRun.Repo.Name, gtCtx["repository"].GetStringValue()) |
| 424 | + assert.Equal(t, actionRun.Repo.OwnerName, gtCtx["repository_owner"].GetStringValue()) |
| 425 | + assert.Equal(t, actionRun.Repo.HTMLURL(), gtCtx["repositoryUrl"].GetStringValue()) |
| 426 | + assert.Equal(t, fmt.Sprint(actionRunJob.RunID), gtCtx["run_id"].GetStringValue()) |
| 427 | + assert.Equal(t, fmt.Sprint(actionRun.Index), gtCtx["run_number"].GetStringValue()) |
| 428 | + assert.Equal(t, fmt.Sprint(actionRunJob.Attempt), gtCtx["run_attempt"].GetStringValue()) |
| 429 | + assert.Equal(t, "Actions", gtCtx["secret_source"].GetStringValue()) |
| 430 | + assert.Equal(t, setting.AppURL, gtCtx["server_url"].GetStringValue()) |
| 431 | + assert.Equal(t, actionRun.CommitSHA, gtCtx["sha"].GetStringValue()) |
| 432 | + assert.Equal(t, actionRun.WorkflowID, gtCtx["workflow"].GetStringValue()) |
| 433 | + assert.Equal(t, setting.Actions.DefaultActionsURL.URL(), gtCtx["gitea_default_actions_url"].GetStringValue()) |
| 434 | + token := gtCtx["token"].GetStringValue() |
| 435 | + assert.Equal(t, actionTask.TokenLastEight, token[len(token)-8:]) |
| 436 | + |
| 437 | + doAPIDeleteRepository(user2APICtx)(t) |
| 438 | + }) |
| 439 | +} |
| 440 | + |
350 | 441 | func createActionsTestRepo(t *testing.T, authToken, repoName string, isPrivate bool) *api.Repository {
|
351 | 442 | req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{
|
352 | 443 | Name: repoName,
|
|
0 commit comments