@@ -121,86 +121,98 @@ jobs:
121121 },
122122 }
123123 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
124- runner := newMockRunner (t )
125- runner .registerAsGlobalRunner (t , "mock-runner" , []string {"ubuntu-latest" })
126-
127124 user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
128125 session := loginUser (t , user2 .Name )
129126 token := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
130127
131- // create the repo
132- req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
133- Name : "actions-jobs-with-needs" ,
134- Description : "test jobs with needs" ,
135- Private : false ,
136- Readme : "Default" ,
137- AutoInit : true ,
138- DefaultBranch : "main" ,
139- }).AddTokenAuth (token )
140- resp := MakeRequest (t , req , http .StatusCreated )
141- var apiRepo api.Repository
142- DecodeJSON (t , resp , & apiRepo )
128+ apiRepo := createActionsTestRepo (t , token , "actions-jobs-with-needs" , false )
129+
130+ runner := newMockRunner ()
131+ runner .registerAsRepoRunner (t , user2 .Name , apiRepo .Name , "mock-runner" , []string {"ubuntu-latest" })
143132
144133 for _ , tc := range testCases {
145- // create the workflow file
146- createFileOptions := & api.CreateFileOptions {
147- FileOptions : api.FileOptions {
148- BranchName : "main" ,
149- Message : fmt .Sprintf ("create %s" , tc .treePath ),
150- Author : api.Identity {
151- Name : user2 .Name ,
152- Email : user2 .Email ,
153- },
154- Committer : api.Identity {
155- Name : user2 .Name ,
156- Email : user2 .Email ,
157- },
158- Dates : api.CommitDateOptions {
159- Author : time .Now (),
160- Committer : time .Now (),
161- },
162- },
163- ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte (tc .fileContent )),
164- }
165- req = NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s" , user2 .Name , apiRepo .Name , tc .treePath ), createFileOptions ).
166- AddTokenAuth (token )
167- var fileResponse api.FileResponse
168- resp = MakeRequest (t , req , http .StatusCreated )
169- DecodeJSON (t , resp , & fileResponse )
134+ t .Run (fmt .Sprintf ("test %s" , tc .treePath ), func (t * testing.T ) {
135+ // create the workflow file
136+ opts := getWorkflowCreateFileOptions (user2 , apiRepo .DefaultBranch , fmt .Sprintf ("create %s" , tc .treePath ), tc .fileContent )
137+ fileResp := createWorkflowFile (t , token , user2 .Name , apiRepo .Name , tc .treePath , opts )
170138
171- // fetch and execute task
172- for i := 0 ; i < len (tc .execPolicies ); i ++ {
173- task := runner .fetchTask (t )
174- assert .NotNil (t , task )
175- jobName := getTaskJobNameByTaskID (t , token , user2 .Name , apiRepo .Name , task .Id )
176- policy := tc .execPolicies [jobName ]
177- assert .NotNil (t , policy )
178- runner .execTask (t , task , policy )
179- }
139+ // fetch and execute task
140+ for i := 0 ; i < len (tc .execPolicies ); i ++ {
141+ task := runner .fetchTask (t )
142+ jobName := getTaskJobNameByTaskID (t , token , user2 .Name , apiRepo .Name , task .Id )
143+ policy := tc .execPolicies [jobName ]
144+ assert .NotNil (t , policy )
145+ runner .execTask (t , task , policy )
146+ }
180147
181- // check result
182- req = NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/actions/tasks" , user2 .Name , apiRepo .Name )).
183- AddTokenAuth (token )
184- resp = MakeRequest (t , req , http .StatusOK )
185- var actionTaskRespAfter api.ActionTaskResponse
186- DecodeJSON (t , resp , & actionTaskRespAfter )
187- for _ , apiTask := range actionTaskRespAfter .Entries {
188- if apiTask .HeadSHA != fileResponse .Commit .SHA {
189- continue
148+ // check result
149+ req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/actions/tasks" , user2 .Name , apiRepo .Name )).
150+ AddTokenAuth (token )
151+ resp := MakeRequest (t , req , http .StatusOK )
152+ var actionTaskRespAfter api.ActionTaskResponse
153+ DecodeJSON (t , resp , & actionTaskRespAfter )
154+ for _ , apiTask := range actionTaskRespAfter .Entries {
155+ if apiTask .HeadSHA != fileResp .Commit .SHA {
156+ continue
157+ }
158+ status := apiTask .Status
159+ assert .Equal (t , status , tc .expectedStatuses [apiTask .Name ])
190160 }
191- status := apiTask .Status
192- assert .Equal (t , status , tc .expectedStatuses [apiTask .Name ])
193- }
161+ })
194162 }
195163 })
196164}
197165
166+ func createActionsTestRepo (t * testing.T , authToken , repoName string , isPrivate bool ) * api.Repository {
167+ req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
168+ Name : repoName ,
169+ Private : isPrivate ,
170+ Readme : "Default" ,
171+ AutoInit : true ,
172+ DefaultBranch : "main" ,
173+ }).AddTokenAuth (authToken )
174+ resp := MakeRequest (t , req , http .StatusCreated )
175+ var apiRepo api.Repository
176+ DecodeJSON (t , resp , & apiRepo )
177+ return & apiRepo
178+ }
179+
180+ func getWorkflowCreateFileOptions (u * user_model.User , branch , msg , content string ) * api.CreateFileOptions {
181+ return & api.CreateFileOptions {
182+ FileOptions : api.FileOptions {
183+ BranchName : branch ,
184+ Message : msg ,
185+ Author : api.Identity {
186+ Name : u .Name ,
187+ Email : u .Email ,
188+ },
189+ Committer : api.Identity {
190+ Name : u .Name ,
191+ Email : u .Email ,
192+ },
193+ Dates : api.CommitDateOptions {
194+ Author : time .Now (),
195+ Committer : time .Now (),
196+ },
197+ },
198+ ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte (content )),
199+ }
200+ }
201+
202+ func createWorkflowFile (t * testing.T , authToken , ownerName , repoName , treePath string , opts * api.CreateFileOptions ) * api.FileResponse {
203+ req := NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s" , ownerName , repoName , treePath ), opts ).
204+ AddTokenAuth (authToken )
205+ resp := MakeRequest (t , req , http .StatusCreated )
206+ var fileResponse api.FileResponse
207+ DecodeJSON (t , resp , & fileResponse )
208+ return & fileResponse
209+ }
210+
198211// getTaskJobNameByTaskID get the job name of the task by task ID
199212// there is currently not an API for querying a task by ID so we have to list all the tasks
200- func getTaskJobNameByTaskID (t * testing.T , authToken , repoOwner , repoName string , taskID int64 ) string {
201- t .Helper ()
213+ func getTaskJobNameByTaskID (t * testing.T , authToken , ownerName , repoName string , taskID int64 ) string {
202214 // FIXME: we may need to query several pages
203- req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/actions/tasks" , repoOwner , repoName )).
215+ req := NewRequest (t , "GET" , fmt .Sprintf ("/api/v1/repos/%s/%s/actions/tasks" , ownerName , repoName )).
204216 AddTokenAuth (authToken )
205217 resp := MakeRequest (t , req , http .StatusOK )
206218 var taskRespBefore api.ActionTaskResponse
0 commit comments