/
index.spec.ts
1512 lines (1284 loc) · 45.2 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
BranchStatusConfig,
CommitFilesConfig,
File,
Platform,
RepoConfig,
RepoParams,
} from '..';
import { partial } from '../../../test/util';
import {
REPOSITORY_ACCESS_FORBIDDEN,
REPOSITORY_ARCHIVED,
REPOSITORY_BLOCKED,
REPOSITORY_CHANGED,
REPOSITORY_DISABLED,
REPOSITORY_EMPTY,
REPOSITORY_MIRRORED,
} from '../../constants/error-messages';
import { logger as _logger } from '../../logger';
import { BranchStatus } from '../../types';
import { setBaseUrl } from '../../util/http/gitea';
import * as ght from './gitea-helper';
describe('platform/gitea', () => {
let gitea: Platform;
let helper: jest.Mocked<typeof import('./gitea-helper')>;
let logger: jest.Mocked<typeof _logger>;
let GitStorage: jest.Mocked<typeof import('../git/storage').Storage> &
jest.Mock;
const mockCommitHash = '0d9c7726c3d628b7e28af234595cfd20febdbf8e';
const mockUser: ght.User = {
id: 1,
username: 'renovate',
full_name: 'Renovate Bot',
email: 'renovate@example.com',
};
const mockRepo = partial<ght.Repo>({
allow_rebase: true,
clone_url: 'https://gitea.renovatebot.com/some/repo.git',
default_branch: 'master',
full_name: 'some/repo',
permissions: {
pull: true,
push: true,
admin: false,
},
});
const mockRepos: ght.Repo[] = [
partial<ght.Repo>({ full_name: 'a/b' }),
partial<ght.Repo>({ full_name: 'c/d' }),
];
const mockPRs: ght.PR[] = [
partial<ght.PR>({
number: 1,
title: 'Some PR',
body: 'some random pull request',
state: 'open',
diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/1.diff',
created_at: '2015-03-22T20:36:16Z',
closed_at: null,
mergeable: true,
base: { ref: 'some-base-branch' },
head: {
label: 'some-head-branch',
sha: 'some-head-sha',
repo: partial<ght.Repo>({ full_name: mockRepo.full_name }),
},
}),
partial<ght.PR>({
number: 2,
title: 'Other PR',
body: 'other random pull request',
state: 'closed',
diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/2.diff',
created_at: '2011-08-18T22:30:38Z',
closed_at: '2016-01-09T10:03:21Z',
mergeable: true,
base: { ref: 'other-base-branch' },
head: {
label: 'other-head-branch',
sha: 'other-head-sha',
repo: partial<ght.Repo>({ full_name: mockRepo.full_name }),
},
}),
];
const mockIssues: ght.Issue[] = [
{
number: 1,
title: 'open-issue',
state: 'open',
body: 'some-content',
assignees: [],
},
{
number: 2,
title: 'closed-issue',
state: 'closed',
body: 'other-content',
assignees: [],
},
{
number: 3,
title: 'duplicate-issue',
state: 'open',
body: 'duplicate-content',
assignees: [],
},
{
number: 4,
title: 'duplicate-issue',
state: 'open',
body: 'duplicate-content',
assignees: [],
},
{
number: 5,
title: 'duplicate-issue',
state: 'open',
body: 'duplicate-content',
assignees: [],
},
];
const mockComments: ght.Comment[] = [
{ id: 1, body: 'some-body' },
{ id: 2, body: 'other-body' },
{ id: 3, body: '### some-topic\n\nsome-content' },
];
const mockRepoLabels: ght.Label[] = [
{ id: 1, name: 'some-label', description: 'its a me', color: '#000000' },
{ id: 2, name: 'other-label', description: 'labelario', color: '#ffffff' },
];
const mockOrgLabels: ght.Label[] = [
{
id: 3,
name: 'some-org-label',
description: 'its a org me',
color: '#0000aa',
},
{
id: 4,
name: 'other-org-label',
description: 'org labelario',
color: '#ffffaa',
},
];
const gsmInitRepo = jest.fn();
const gsmCleanRepo = jest.fn();
const gsmSetBaseBranch = jest.fn();
const gsmGetCommitMessages = jest.fn();
const gsmGetAllRenovateBranches = jest.fn();
const gsmGetFileList = jest.fn();
const gsmGetRepoStatus = jest.fn();
const gsmGetFile = jest.fn();
const gsmGetBranchLastCommitTime = jest.fn();
const gsmMergeBranch = jest.fn();
const gsmBranchExists = jest.fn();
const gsmSetBranchPrefix = jest.fn();
const gsmCommitFilesToBranch = jest.fn();
const gsmDeleteBranch = jest.fn();
const gsmIsBranchStale = jest.fn(() => false);
const gsmGetBranchCommit = jest.fn(() => mockCommitHash);
beforeEach(async () => {
jest.resetModules();
jest.clearAllMocks();
jest.mock('./gitea-helper');
jest.mock('../git/storage');
jest.mock('../../logger');
gitea = await import('.');
helper = (await import('./gitea-helper')) as any;
logger = (await import('../../logger')).logger as any;
GitStorage = (await import('../git/storage')).Storage as any;
GitStorage.mockImplementation(() => ({
initRepo: gsmInitRepo,
cleanRepo: gsmCleanRepo,
setBaseBranch: gsmSetBaseBranch,
getCommitMessages: gsmGetCommitMessages,
getAllRenovateBranches: gsmGetAllRenovateBranches,
getFileList: gsmGetFileList,
getRepoStatus: gsmGetRepoStatus,
getFile: gsmGetFile,
getBranchLastCommitTime: gsmGetBranchLastCommitTime,
mergeBranch: gsmMergeBranch,
branchExists: gsmBranchExists,
setBranchPrefix: gsmSetBranchPrefix,
isBranchStale: gsmIsBranchStale,
getBranchCommit: gsmGetBranchCommit,
commitFiles: gsmCommitFilesToBranch,
deleteBranch: gsmDeleteBranch,
}));
global.gitAuthor = { name: 'Renovate', email: 'renovate@example.com' };
setBaseUrl('https://gitea.renovatebot.com/api/v1');
});
function initFakeRepo(
repo?: Partial<ght.Repo>,
config?: Partial<RepoParams>
): Promise<RepoConfig> {
helper.getRepo.mockResolvedValueOnce({ ...mockRepo, ...repo });
return gitea.initRepo({
repository: mockRepo.full_name,
localDir: '',
optimizeForDisabled: false,
...config,
});
}
describe('initPlatform()', () => {
it('should throw if no token', async () => {
await expect(gitea.initPlatform({})).rejects.toThrow();
});
it('should throw if auth fails', async () => {
helper.getCurrentUser.mockRejectedValueOnce(new Error());
await expect(
gitea.initPlatform({ token: 'some-token' })
).rejects.toThrow();
});
it('should support default endpoint', async () => {
helper.getCurrentUser.mockResolvedValueOnce(mockUser);
expect(
await gitea.initPlatform({ token: 'some-token' })
).toMatchSnapshot();
});
it('should support custom endpoint', async () => {
helper.getCurrentUser.mockResolvedValueOnce(mockUser);
expect(
await gitea.initPlatform({
token: 'some-token',
endpoint: 'https://gitea.renovatebot.com',
})
).toMatchSnapshot();
});
it('should use username as author name if full name is missing', async () => {
helper.getCurrentUser.mockResolvedValueOnce({
...mockUser,
full_name: undefined,
});
expect(
await gitea.initPlatform({ token: 'some-token' })
).toMatchSnapshot();
});
});
describe('getRepos', () => {
it('should propagate any other errors', async () => {
helper.searchRepos.mockRejectedValueOnce(new Error('searchRepos()'));
await expect(gitea.getRepos()).rejects.toThrow('searchRepos()');
});
it('should return an array of repos', async () => {
helper.searchRepos.mockResolvedValueOnce(mockRepos);
const repos = await gitea.getRepos();
expect(repos).toMatchSnapshot();
});
});
describe('initRepo', () => {
const initRepoCfg: RepoParams = {
repository: mockRepo.full_name,
localDir: '',
optimizeForDisabled: false,
};
it('should propagate API errors', async () => {
helper.getRepo.mockRejectedValueOnce(new Error('getRepo()'));
await expect(gitea.initRepo(initRepoCfg)).rejects.toThrow('getRepo()');
});
it('should abort when disabled and optimizeForDisabled is enabled', async () => {
helper.getRepoContents.mockResolvedValueOnce(
partial<ght.RepoContents>({
contentString: JSON.stringify({ enabled: false }),
})
);
await expect(
initFakeRepo({}, { optimizeForDisabled: true })
).rejects.toThrow(REPOSITORY_DISABLED);
});
it('should abort when repo is archived', async () => {
await expect(initFakeRepo({ archived: true })).rejects.toThrow(
REPOSITORY_ARCHIVED
);
});
it('should abort when repo is mirrored', async () => {
await expect(initFakeRepo({ mirror: true })).rejects.toThrow(
REPOSITORY_MIRRORED
);
});
it('should abort when repo is empty', async () => {
await expect(initFakeRepo({ empty: true })).rejects.toThrow(
REPOSITORY_EMPTY
);
});
it('should abort when repo has insufficient permissions', async () => {
await expect(
initFakeRepo({
permissions: {
pull: false,
push: false,
admin: false,
},
})
).rejects.toThrow(REPOSITORY_ACCESS_FORBIDDEN);
});
it('should abort when repo has no available merge methods', async () => {
await expect(initFakeRepo({ allow_rebase: false })).rejects.toThrow(
REPOSITORY_BLOCKED
);
});
it('should fall back to merge method "rebase-merge"', async () => {
expect(
await initFakeRepo({ allow_rebase: false, allow_rebase_explicit: true })
).toMatchSnapshot();
});
it('should fall back to merge method "squash"', async () => {
expect(
await initFakeRepo({ allow_rebase: false, allow_squash_merge: true })
).toMatchSnapshot();
});
it('should fall back to merge method "merge"', async () => {
expect(
await initFakeRepo({
allow_rebase: false,
allow_merge_commits: true,
})
).toMatchSnapshot();
});
});
describe('cleanRepo', () => {
it('does not throw an error with uninitialized repo', async () => {
await gitea.cleanRepo();
expect(gsmCleanRepo).not.toHaveBeenCalled();
});
it('propagates call to storage class with initialized repo', async () => {
await initFakeRepo();
await gitea.cleanRepo();
expect(gsmCleanRepo).toHaveBeenCalledTimes(1);
});
});
describe('setBranchStatus', () => {
const setBranchStatus = async (bsc?: Partial<BranchStatusConfig>) => {
await initFakeRepo();
await gitea.setBranchStatus({
branchName: 'some-branch',
state: BranchStatus.green,
context: 'some-context',
description: 'some-description',
...bsc,
});
};
it('should create a new commit status', async () => {
await setBranchStatus();
expect(helper.createCommitStatus).toHaveBeenCalledTimes(1);
expect(helper.createCommitStatus).toHaveBeenCalledWith(
mockRepo.full_name,
mockCommitHash,
{
state: 'success',
context: 'some-context',
description: 'some-description',
}
);
});
it('should default to pending state', async () => {
await setBranchStatus({ state: undefined });
expect(helper.createCommitStatus).toHaveBeenCalledTimes(1);
expect(helper.createCommitStatus).toHaveBeenCalledWith(
mockRepo.full_name,
mockCommitHash,
{
state: 'pending',
context: 'some-context',
description: 'some-description',
}
);
});
it('should include url if specified', async () => {
await setBranchStatus({ url: 'some-url' });
expect(helper.createCommitStatus).toHaveBeenCalledTimes(1);
expect(helper.createCommitStatus).toHaveBeenCalledWith(
mockRepo.full_name,
mockCommitHash,
{
state: 'success',
context: 'some-context',
description: 'some-description',
target_url: 'some-url',
}
);
});
it('should gracefully fail with warning', async () => {
helper.createCommitStatus.mockRejectedValueOnce(new Error());
await setBranchStatus();
expect(logger.warn).toHaveBeenCalledTimes(1);
});
it('should set default base branch', async () => {
await initFakeRepo();
await gitea.setBaseBranch();
expect(gsmSetBaseBranch).toHaveBeenCalledTimes(1);
expect(gsmSetBaseBranch).toHaveBeenCalledWith(mockRepo.default_branch);
});
it('should set custom base branch', async () => {
await initFakeRepo();
await gitea.setBaseBranch('devel');
expect(gsmSetBaseBranch).toHaveBeenCalledTimes(1);
expect(gsmSetBaseBranch).toHaveBeenCalledWith('devel');
});
});
describe('getBranchStatus', () => {
const getBranchStatus = async (state: string): Promise<BranchStatus> => {
await initFakeRepo();
helper.getCombinedCommitStatus.mockResolvedValueOnce(
partial<ght.CombinedCommitStatus>({
worstStatus: state as ght.CommitStatusType,
})
);
return gitea.getBranchStatus('some-branch', []);
};
it('should return success if requiredStatusChecks null', async () => {
expect(await gitea.getBranchStatus('some-branch', null)).toEqual(
BranchStatus.green
);
});
it('should return failed if unsupported requiredStatusChecks', async () => {
expect(await gitea.getBranchStatus('some-branch', ['foo'])).toEqual(
BranchStatus.red
);
});
it('should return yellow for unknown result', async () => {
expect(await getBranchStatus('unknown')).toEqual(BranchStatus.yellow);
});
it('should return pending state for pending result', async () => {
expect(await getBranchStatus('pending')).toEqual(BranchStatus.yellow);
});
it('should return success state for success result', async () => {
expect(await getBranchStatus('success')).toEqual(BranchStatus.green);
});
it('should return null for all other results', async () => {
expect(await getBranchStatus('invalid')).toEqual(BranchStatus.yellow);
});
it('should abort when branch status returns 404', async () => {
helper.getCombinedCommitStatus.mockRejectedValueOnce({ statusCode: 404 });
await expect(gitea.getBranchStatus('some-branch', [])).rejects.toThrow(
REPOSITORY_CHANGED
);
});
it('should propagate any other errors', async () => {
helper.getCombinedCommitStatus.mockRejectedValueOnce(
new Error('getCombinedCommitStatus()')
);
await expect(gitea.getBranchStatus('some-branch', [])).rejects.toThrow(
'getCombinedCommitStatus()'
);
});
});
describe('getBranchStatusCheck', () => {
it('should return null with no results', async () => {
helper.getCombinedCommitStatus.mockResolvedValueOnce(
partial<ght.CombinedCommitStatus>({
statuses: [],
})
);
expect(
await gitea.getBranchStatusCheck('some-branch', 'some-context')
).toBeNull();
});
it('should return null with no matching results', async () => {
helper.getCombinedCommitStatus.mockResolvedValueOnce(
partial<ght.CombinedCommitStatus>({
statuses: [partial<ght.CommitStatus>({ context: 'other-context' })],
})
);
expect(
await gitea.getBranchStatusCheck('some-branch', 'some-context')
).toBeNull();
});
it('should return yellow with unknown status', async () => {
helper.getCombinedCommitStatus.mockResolvedValueOnce(
partial<ght.CombinedCommitStatus>({
statuses: [
partial<ght.CommitStatus>({
context: 'some-context',
}),
],
})
);
expect(
await gitea.getBranchStatusCheck('some-branch', 'some-context')
).toEqual(BranchStatus.yellow);
});
it('should return green of matching result', async () => {
helper.getCombinedCommitStatus.mockResolvedValueOnce(
partial<ght.CombinedCommitStatus>({
statuses: [
partial<ght.CommitStatus>({
status: 'success',
context: 'some-context',
}),
],
})
);
expect(
await gitea.getBranchStatusCheck('some-branch', 'some-context')
).toEqual(BranchStatus.green);
});
});
describe('getPrList', () => {
it('should return list of pull requests', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res = await gitea.getPrList();
expect(res).toHaveLength(mockPRs.length);
expect(res).toMatchSnapshot();
});
it('should cache results after first query', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res1 = await gitea.getPrList();
const res2 = await gitea.getPrList();
expect(res1).toEqual(res2);
expect(helper.searchPRs).toHaveBeenCalledTimes(1);
});
});
describe('getPr', () => {
it('should return enriched pull request which exists if open', async () => {
const mockPR = mockPRs[0];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
helper.getBranch.mockResolvedValueOnce(
partial<ght.Branch>({
commit: {
id: mockCommitHash,
author: partial<ght.CommitUser>({ email: global.gitAuthor.email }),
},
})
);
await initFakeRepo();
const res = await gitea.getPr(mockPR.number);
expect(res).toHaveProperty('number', mockPR.number);
expect(res).toMatchSnapshot();
});
it('should fallback to direct fetching if cache fails', async () => {
const mockPR = mockPRs[0];
helper.searchPRs.mockResolvedValueOnce([]);
helper.getPR.mockResolvedValueOnce(mockPR);
await initFakeRepo();
const res = await gitea.getPr(mockPR.number);
expect(res).toHaveProperty('number', mockPR.number);
expect(res).toMatchSnapshot();
expect(helper.getPR).toHaveBeenCalledTimes(1);
});
it('should return null for missing pull request', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
expect(await gitea.getPr(42)).toBeNull();
});
it('should block modified pull request for rebasing', async () => {
const mockPR = mockPRs[0];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
helper.getBranch.mockResolvedValueOnce(
partial<ght.Branch>({
commit: {
id: mockCommitHash,
author: partial<ght.CommitUser>({
email: 'not-a-robot@renovatebot.com',
}),
},
})
);
await initFakeRepo();
const res = await gitea.getPr(mockPR.number);
expect(res).toHaveProperty('number', mockPR.number);
expect(res).toHaveProperty('isModified', true);
});
});
describe('findPr', () => {
it('should find pull request without title or state', async () => {
const mockPR = mockPRs[0];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res = await gitea.findPr({ branchName: mockPR.head.label });
expect(res).toHaveProperty('branchName', mockPR.head.label);
});
it('should find pull request with title', async () => {
const mockPR = mockPRs[0];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res = await gitea.findPr({
branchName: mockPR.head.label,
prTitle: mockPR.title,
});
expect(res).toHaveProperty('branchName', mockPR.head.label);
expect(res).toHaveProperty('title', mockPR.title);
});
it('should find pull request with state', async () => {
const mockPR = mockPRs[1];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res = await gitea.findPr({
branchName: mockPR.head.label,
state: mockPR.state,
});
expect(res).toHaveProperty('branchName', mockPR.head.label);
expect(res).toHaveProperty('state', mockPR.state);
});
it('should not find pull request with inverted state', async () => {
const mockPR = mockPRs[1];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
expect(
await gitea.findPr({
branchName: mockPR.head.label,
state: `!${mockPR.state}` as ght.PRState,
})
).toBeNull();
});
it('should find pull request with title and state', async () => {
const mockPR = mockPRs[1];
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
const res = await gitea.findPr({
branchName: mockPR.head.label,
prTitle: mockPR.title,
state: mockPR.state,
});
expect(res).toHaveProperty('branchName', mockPR.head.label);
expect(res).toHaveProperty('title', mockPR.title);
expect(res).toHaveProperty('state', mockPR.state);
});
it('should return null for missing pull request', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
expect(await gitea.findPr({ branchName: 'missing' })).toBeNull();
});
});
describe('createPr', () => {
const mockNewPR: ght.PR = {
number: 42,
state: 'open',
head: {
label: 'pr-branch',
sha: mockCommitHash,
repo: partial<ght.Repo>({ full_name: mockRepo.full_name }),
},
base: {
ref: mockRepo.default_branch,
},
diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/42.diff',
title: 'pr-title',
body: 'pr-body',
mergeable: true,
created_at: '2014-04-01T05:14:20Z',
closed_at: '2017-12-28T12:17:48Z',
};
it('should use base branch by default', async () => {
helper.createPR.mockResolvedValueOnce({
...mockNewPR,
base: { ref: 'devel' },
});
await initFakeRepo();
await gitea.setBaseBranch('devel');
const res = await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
});
expect(res).toHaveProperty('number', mockNewPR.number);
expect(res).toHaveProperty('targetBranch', 'devel');
expect(res).toMatchSnapshot();
expect(helper.createPR).toHaveBeenCalledTimes(1);
expect(helper.createPR).toHaveBeenCalledWith(mockRepo.full_name, {
base: 'devel',
head: mockNewPR.head.label,
title: mockNewPR.title,
body: mockNewPR.body,
labels: [],
});
});
it('should use default branch if requested', async () => {
helper.createPR.mockResolvedValueOnce(mockNewPR);
await initFakeRepo();
await gitea.setBaseBranch('devel');
const res = await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
useDefaultBranch: true,
});
expect(res).toHaveProperty('number', mockNewPR.number);
expect(res).toHaveProperty('targetBranch', mockNewPR.base.ref);
expect(res).toMatchSnapshot();
expect(helper.createPR).toHaveBeenCalledTimes(1);
expect(helper.createPR).toHaveBeenCalledWith(mockRepo.full_name, {
base: mockNewPR.base.ref,
head: mockNewPR.head.label,
title: mockNewPR.title,
body: mockNewPR.body,
labels: [],
});
});
it('should resolve and apply optional labels to pull request', async () => {
helper.createPR.mockResolvedValueOnce(mockNewPR);
helper.getRepoLabels.mockResolvedValueOnce(mockRepoLabels);
helper.getOrgLabels.mockResolvedValueOnce(mockOrgLabels);
const mockLabels = mockRepoLabels.concat(mockOrgLabels);
await initFakeRepo();
await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
labels: mockLabels.map((l) => l.name),
});
expect(helper.createPR).toHaveBeenCalledTimes(1);
expect(helper.createPR).toHaveBeenCalledWith(mockRepo.full_name, {
base: mockNewPR.base.ref,
head: mockNewPR.head.label,
title: mockNewPR.title,
body: mockNewPR.body,
labels: mockLabels.map((l) => l.id),
});
});
it('should ensure new pull request gets added to cached pull requests', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
helper.createPR.mockResolvedValueOnce(mockNewPR);
await initFakeRepo();
await gitea.getPrList();
await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
useDefaultBranch: true,
});
const res = gitea.getPr(mockNewPR.number);
expect(res).not.toBeNull();
expect(helper.searchPRs).toHaveBeenCalledTimes(1);
});
it('should attempt to resolve 409 conflict error (w/o update)', async () => {
helper.createPR.mockRejectedValueOnce({ statusCode: 409 });
helper.searchPRs.mockResolvedValueOnce([mockNewPR]);
await initFakeRepo();
const res = await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
useDefaultBranch: true,
});
expect(res).toHaveProperty('number', mockNewPR.number);
});
it('should attempt to resolve 409 conflict error (w/ update)', async () => {
helper.createPR.mockRejectedValueOnce({ statusCode: 409 });
helper.searchPRs.mockResolvedValueOnce([mockNewPR]);
await initFakeRepo();
const res = await gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: 'new-title',
prBody: 'new-body',
useDefaultBranch: true,
});
expect(res).toHaveProperty('number', mockNewPR.number);
expect(helper.updatePR).toHaveBeenCalledTimes(1);
expect(helper.updatePR).toHaveBeenCalledWith(
mockRepo.full_name,
mockNewPR.number,
{ title: 'new-title', body: 'new-body' }
);
});
it('should abort when response for created pull request is invalid', async () => {
helper.createPR.mockResolvedValueOnce(partial<ght.PR>({}));
await initFakeRepo();
await expect(
gitea.createPr({
branchName: mockNewPR.head.label,
prTitle: mockNewPR.title,
prBody: mockNewPR.body,
})
).rejects.toThrow();
});
});
describe('updatePr', () => {
it('should update pull request with title', async () => {
await initFakeRepo();
await gitea.updatePr(1, 'New Title');
expect(helper.updatePR).toHaveBeenCalledTimes(1);
expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, {
title: 'New Title',
});
});
it('should update pull request with title and body', async () => {
await initFakeRepo();
await gitea.updatePr(1, 'New Title', 'New Body');
expect(helper.updatePR).toHaveBeenCalledTimes(1);
expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, {
title: 'New Title',
body: 'New Body',
});
});
});
describe('mergePr', () => {
it('should return true when merging succeeds', async () => {
await initFakeRepo();
expect(await gitea.mergePr(1, 'some-branch')).toEqual(true);
expect(helper.mergePR).toHaveBeenCalledTimes(1);
expect(helper.mergePR).toHaveBeenCalledWith(
mockRepo.full_name,
1,
'rebase'
);
});
it('should return false when merging fails', async () => {
helper.mergePR.mockRejectedValueOnce(new Error());
await initFakeRepo();
expect(await gitea.mergePr(1, 'some-branch')).toEqual(false);
});
});
describe('findIssue', () => {
it('should return existing open issue', async () => {
const mockIssue = mockIssues.find((i) => i.title === 'open-issue');
helper.searchIssues.mockResolvedValueOnce(mockIssues);
await initFakeRepo();
expect(await gitea.findIssue(mockIssue.title)).toHaveProperty(
'number',
mockIssue.number
);
});
it('should not return existing closed issue', async () => {
const mockIssue = mockIssues.find((i) => i.title === 'closed-issue');
helper.searchIssues.mockResolvedValueOnce(mockIssues);
await initFakeRepo();
expect(await gitea.findIssue(mockIssue.title)).toBeNull();
});
it('should return null for missing issue', async () => {
helper.searchIssues.mockResolvedValueOnce(mockIssues);
await initFakeRepo();
expect(await gitea.findIssue('missing')).toBeNull();
});
});
describe('ensureIssue', () => {
it('should create issue if not found', async () => {
const mockIssue = {
title: 'new-title',
body: 'new-body',
shouldReOpen: false,
once: false,
};
helper.searchIssues.mockResolvedValueOnce(mockIssues);
helper.createIssue.mockResolvedValueOnce(
partial<ght.Issue>({ number: 42 })
);
await initFakeRepo();
const res = await gitea.ensureIssue(mockIssue);
expect(res).toEqual('created');
expect(helper.createIssue).toHaveBeenCalledTimes(1);
expect(helper.createIssue).toHaveBeenCalledWith(mockRepo.full_name, {
body: mockIssue.body,
title: mockIssue.title,
});
});
it('should not reopen closed issue by default', async () => {
const closedIssue = mockIssues.find((i) => i.title === 'closed-issue');
helper.searchIssues.mockResolvedValueOnce(mockIssues);
await initFakeRepo();
const res = await gitea.ensureIssue({
title: closedIssue.title,
body: closedIssue.body,
shouldReOpen: false,
once: false,