@@ -2,14 +2,17 @@ import { WebApi } from "azure-devops-node-api";
2
2
import uuid from "uuid/v4" ;
3
3
import { Config } from "../../config" ;
4
4
import { disableVerboseLogging , enableVerboseLogging } from "../../logger" ;
5
+ import { AzureDevOpsOpts } from "../git" ;
5
6
import * as gitutils from "../gitutils" ;
6
7
import {
7
8
createPullRequest ,
8
9
generatePRUrl ,
9
10
getGitOrigin ,
10
11
GitAPI ,
12
+ repositoryHasFile ,
13
+ validateRepository ,
11
14
} from "./azure" ;
12
-
15
+ import * as azure from "./azure" ;
13
16
jest . mock ( "azure-devops-node-api" ) ;
14
17
jest . mock ( "../../config" ) ;
15
18
@@ -205,3 +208,91 @@ describe("test generatePRUrl function", async () => {
205
208
) . rejects . toThrow ( ) ;
206
209
} ) ;
207
210
} ) ;
211
+
212
+ describe ( "validateRepository" , ( ) => {
213
+ test ( "repository exists" , async ( ) => {
214
+ const getRepositoryFunc = jest . spyOn ( azure , "GitAPI" ) ;
215
+ getRepositoryFunc . mockResolvedValueOnce (
216
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
217
+ { getRepository : ( ) => ( { id : "3839fjfkj" } ) } as any
218
+ ) ;
219
+
220
+ const getItemFunc = jest . spyOn ( azure , "GitAPI" ) ;
221
+ getItemFunc . mockResolvedValueOnce (
222
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
223
+ { getItem : ( ) => ( { commitId : "3839fjfkj" } ) } as any
224
+ ) ;
225
+
226
+ const accessOpts : AzureDevOpsOpts = {
227
+ orgName : "testOrg" ,
228
+ personalAccessToken : "mytoken" ,
229
+ project : "testProject" ,
230
+ } ;
231
+
232
+ await expect (
233
+ validateRepository (
234
+ "my-project" ,
235
+ "myFile" ,
236
+ "master" ,
237
+ "my-repo" ,
238
+ accessOpts
239
+ )
240
+ ) . resolves . not . toThrow ( ) ;
241
+ } ) ;
242
+ test ( "repository does not exist" , async ( ) => {
243
+ const createPullRequestFunc = jest . spyOn ( azure , "GitAPI" ) ;
244
+ createPullRequestFunc . mockResolvedValueOnce (
245
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
246
+ { getRepository : ( ) => null } as any
247
+ ) ;
248
+
249
+ const accessOpts : AzureDevOpsOpts = {
250
+ orgName : "testOrg" ,
251
+ personalAccessToken : "mytoken" ,
252
+ project : "testProject" ,
253
+ } ;
254
+ await expect (
255
+ validateRepository (
256
+ "my-project" ,
257
+ "myFile" ,
258
+ "master" ,
259
+ "my-repo" ,
260
+ accessOpts
261
+ )
262
+ ) . rejects . toThrow ( ) ;
263
+ } ) ;
264
+ } ) ;
265
+
266
+ describe ( "repositoryHasFile" , ( ) => {
267
+ test ( "repository contains the given file" , async ( ) => {
268
+ const createPullRequestFunc = jest . spyOn ( azure , "GitAPI" ) ;
269
+ createPullRequestFunc . mockResolvedValueOnce (
270
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
271
+ { getItem : ( ) => ( { commitId : "3839fjfkj" } ) } as any
272
+ ) ;
273
+
274
+ const accessOpts : AzureDevOpsOpts = {
275
+ orgName : "testOrg" ,
276
+ personalAccessToken : "mytoken" ,
277
+ project : "testProject" ,
278
+ } ;
279
+ await expect (
280
+ repositoryHasFile ( "testFile.txt" , "master" , "test-repo" , accessOpts )
281
+ ) . resolves . not . toThrow ( ) ;
282
+ } ) ;
283
+ test ( "repository does not contain the given file" , async ( ) => {
284
+ jest . spyOn ( azure , "GitAPI" ) . mockResolvedValueOnce (
285
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
+ { getItem : ( ) => null } as any
287
+ ) ;
288
+ const accessOpts : AzureDevOpsOpts = {
289
+ orgName : "testOrg" ,
290
+ personalAccessToken : "mytoken" ,
291
+ project : "testProject" ,
292
+ } ;
293
+
294
+ await expect (
295
+ repositoryHasFile ( "testFile2.txt" , "master" , "test-repo" , accessOpts )
296
+ ) . rejects . toThrow ( ) ;
297
+ } ) ;
298
+ } ) ;
0 commit comments