mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00:00 
			
		
		
		
	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>
This commit is contained in:
		| @@ -4,17 +4,23 @@ | ||||
| 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" | ||||
| @@ -347,6 +353,91 @@ jobs: | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestActionsGiteaContext(t *testing.T) { | ||||
| 	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) | ||||
| 		assert.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}) | ||||
| 		assert.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{} | ||||
| 		assert.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, string((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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user