Skip to content

Commit 88827db

Browse files
authored
Refactor: Bring everything up to speed with new deps and node 20 (#80)
* Refactor: Bring everything up to speed with new deps and node 20 * Based on works of John McBride (@jpmcb) * Update dependencies: + Bump: + @typescript-eslint/eslint-plugin: ^6.7.0 + @typescript-eslint/parser: ^6.7.0 + eslint: ^8.49.0 + typescript: ^5.2.2 + Add: + eslint-import-resolver-typescript: ^3.6.0 Required for eslint to work with typescript import resolver + msw: ^1.3.1 Introduced to replace nock + Replace deprecated: + Replace @zeit/ncc with @vercel/ncc, upstream suggestion Build error: digital envelope routines::unsupported + Drop: + nock due to the issue [1] related to the fetch experimental feature of nodejs * Refactor code: + TypeScript compile errors + ESLint errors and warnings + Tests reimplement GitHub API responses mockup using msw/node instead of nock * Declare the action runs using node20 --- [1] nock/nock#2397 Signed-off-by: Neutron Soutmun <neutron.s@linecorp.com> * chore: cosmetic apply with prettier Signed-off-by: Neutron Soutmun <neutron.s@linecorp.com> --------- Signed-off-by: Neutron Soutmun <neutron.s@linecorp.com>
1 parent 0c8443f commit 88827db

50 files changed

Lines changed: 2863 additions & 1501 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@
5858
},
5959
"settings": {
6060
"import/resolver": {
61-
"node": {
62-
"extensions": [".js", ".jsx", ".ts", ".tsx"]
63-
}
61+
"typescript": true,
62+
"node": true
6463
}
6564
}
6665
}
Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import nock from 'nock'
1+
import {setupServer} from 'msw/node'
2+
import {rest} from 'msw'
23

34
import {handleCronJobs} from '../../src/cronJobs/handleCronJob'
45
import * as utils from '../testUtils'
@@ -9,11 +10,35 @@ import labelFileContents from '../fixtures/labels/labelFileContentsResp.json'
910
import prListFiles from '../fixtures/pullReq/pullReqListFiles.json'
1011
import prListTestFiles from '../fixtures/pullReq/pullReqListTestFiles.json'
1112

12-
nock.disableNetConnect()
13+
const server = setupServer(
14+
// /repos/Codertocat/Hello-World/pulls?page={1,2}
15+
rest.get(
16+
`${utils.api}/repos/Codertocat/Hello-World/pulls`,
17+
(req, res, ctx) => {
18+
const page = req.url.searchParams.get('page')
19+
20+
if (page == '1') {
21+
return res(ctx.status(200), ctx.json(listPullReqs))
22+
} else {
23+
return res(ctx.status(200), ctx.json([]))
24+
}
25+
}
26+
),
27+
rest.get(
28+
`${utils.api}/repos/Codertocat/Hello-World/contents/.github%2Flabels.yaml`,
29+
utils.mockResponse(200, labelFileContents)
30+
)
31+
)
32+
beforeAll(() =>
33+
server.listen({
34+
onUnhandledRequest: 'warn'
35+
})
36+
)
37+
afterEach(() => server.resetHandlers())
38+
afterAll(() => server.close())
1339

1440
describe('cronLabelPr', () => {
1541
beforeEach(() => {
16-
nock.cleanAll()
1742
utils.setupActionsEnv('/area')
1843
})
1944

@@ -24,35 +49,22 @@ describe('cronLabelPr', () => {
2449
// Instead, we use it to gain the repo owner and url
2550
const context = new utils.mockContext(pullReqOpenedEvent)
2651

27-
nock(utils.api)
28-
.get('/repos/Codertocat/Hello-World/pulls?page=1')
29-
.reply(200, listPullReqs)
30-
31-
nock(utils.api)
32-
.get('/repos/Codertocat/Hello-World/pulls?page=2')
33-
.reply(200, [])
34-
35-
let parsedBody = undefined
36-
const scope = nock(utils.api)
37-
.post('/repos/Codertocat/Hello-World/issues/2/labels', body => {
38-
parsedBody = body
39-
return body
40-
})
41-
.reply(200)
42-
43-
nock(utils.api)
44-
.get('/repos/Codertocat/Hello-World/contents/.github/labels.yaml')
45-
.reply(200, labelFileContents)
46-
47-
nock(utils.api)
48-
.get('/repos/Codertocat/Hello-World/pulls/2/files')
49-
.reply(200, prListFiles)
50-
51-
await handleCronJobs(context)
52-
expect(parsedBody).toEqual({
52+
const observeReq = new utils.observeRequest()
53+
server.use(
54+
rest.post(
55+
`${utils.api}/repos/Codertocat/Hello-World/issues/2/labels`,
56+
utils.mockResponse(200, null, observeReq)
57+
),
58+
rest.get(
59+
`${utils.api}/repos/Codertocat/Hello-World/pulls/2/files`,
60+
utils.mockResponse(200, prListFiles)
61+
)
62+
)
63+
64+
await expect(handleCronJobs(context)).resolves.not.toThrow()
65+
expect(observeReq.body()).toEqual({
5366
labels: ['source']
5467
})
55-
expect(scope.isDone()).toBe(true)
5668
})
5769

5870
it('labels the PR with the test label from glob', async () => {
@@ -62,34 +74,22 @@ describe('cronLabelPr', () => {
6274
// Instead, we use it to gain the repo owner and url
6375
const context = new utils.mockContext(pullReqOpenedEvent)
6476

65-
nock(utils.api)
66-
.get('/repos/Codertocat/Hello-World/pulls?page=1')
67-
.reply(200, listPullReqs)
68-
69-
nock(utils.api)
70-
.get('/repos/Codertocat/Hello-World/pulls?page=2')
71-
.reply(200, [])
72-
73-
let parsedBody = undefined
74-
const scope = nock(utils.api)
75-
.post('/repos/Codertocat/Hello-World/issues/2/labels', body => {
76-
parsedBody = body
77-
return body
78-
})
79-
.reply(200)
80-
81-
nock(utils.api)
82-
.get('/repos/Codertocat/Hello-World/contents/.github/labels.yaml')
83-
.reply(200, labelFileContents)
84-
85-
nock(utils.api)
86-
.get('/repos/Codertocat/Hello-World/pulls/2/files')
87-
.reply(200, prListTestFiles)
88-
89-
await handleCronJobs(context)
90-
expect(parsedBody).toEqual({
77+
const observeReq = new utils.observeRequest()
78+
server.use(
79+
rest.post(
80+
`${utils.api}/repos/Codertocat/Hello-World/issues/2/labels`,
81+
utils.mockResponse(200, null, observeReq)
82+
),
83+
84+
rest.get(
85+
`${utils.api}/repos/Codertocat/Hello-World/pulls/2/files`,
86+
utils.mockResponse(200, prListTestFiles)
87+
)
88+
)
89+
90+
await expect(handleCronJobs(context)).resolves.not.toThrow()
91+
expect(observeReq.body()).toEqual({
9192
labels: ['tests']
9293
})
93-
expect(scope.isDone()).toBe(true)
9494
})
9595
})

__tests__/cronJobTest/lgtm.test.ts

Lines changed: 59 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
import nock from 'nock'
1+
import {setupServer} from 'msw/node'
2+
import {rest} from 'msw'
23

34
import {handleCronJobs} from '../../src/cronJobs/handleCronJob'
45
import * as utils from '../testUtils'
56

67
import pullReqOpenedEvent from '../fixtures/pullReq/pullReqOpenedEvent.json'
78
import listPullReqs from '../fixtures/pullReq/pullReqListPulls.json'
89

9-
nock.disableNetConnect()
10-
11-
describe('cronLgtm', () => {
12-
beforeEach(() => {
13-
nock.cleanAll()
10+
const server = setupServer(
11+
// /repos/Codertocat/Hello-World/pulls?state=open&page={1,2}
12+
rest.get(
13+
`${utils.api}/repos/Codertocat/Hello-World/pulls`,
14+
(req, res, ctx) => {
15+
const page = req.url.searchParams.get('page')
16+
17+
if (page == '1') {
18+
return res(ctx.status(200), ctx.json(listPullReqs))
19+
} else {
20+
return res(ctx.status(200), ctx.json([]))
21+
}
22+
}
23+
)
24+
)
25+
beforeAll(() =>
26+
server.listen({
27+
onUnhandledRequest: 'warn'
1428
})
29+
)
30+
afterEach(() => server.resetHandlers())
31+
afterAll(() => server.close())
1532

33+
describe('cronLgtm', () => {
1634
it('merges the PR if the lgtm label is present', async () => {
1735
utils.setupJobsEnv('lgtm')
1836

@@ -22,27 +40,18 @@ describe('cronLgtm', () => {
2240

2341
listPullReqs[0].labels[0].name = 'lgtm'
2442

25-
nock(utils.api)
26-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=1')
27-
.reply(200, listPullReqs)
43+
const observeReq = new utils.observeRequest()
44+
server.use(
45+
rest.put(
46+
`${utils.api}/repos/Codertocat/Hello-World/pulls/2/merge`,
47+
utils.mockResponse(200, null, observeReq)
48+
)
49+
)
2850

29-
nock(utils.api)
30-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=2')
31-
.reply(200, [])
32-
33-
let parsedBody = undefined
34-
nock(utils.api)
35-
.put('/repos/Codertocat/Hello-World/pulls/2/merge', body => {
36-
parsedBody = body
37-
return body
38-
})
39-
.reply(200)
40-
41-
await handleCronJobs(context)
42-
expect(parsedBody).toEqual({
51+
await expect(handleCronJobs(context)).resolves.not.toThrow()
52+
expect(observeReq.body()).toEqual({
4353
merge_method: 'merge'
4454
})
45-
expect(nock.isDone()).toBe(true)
4655
})
4756

4857
it('merges the PR with squash configured', async () => {
@@ -55,27 +64,18 @@ describe('cronLgtm', () => {
5564

5665
listPullReqs[0].labels[0].name = 'lgtm'
5766

58-
nock(utils.api)
59-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=1')
60-
.reply(200, listPullReqs)
61-
62-
nock(utils.api)
63-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=2')
64-
.reply(200, [])
67+
const observeReq = new utils.observeRequest()
68+
server.use(
69+
rest.put(
70+
`${utils.api}/repos/Codertocat/Hello-World/pulls/2/merge`,
71+
utils.mockResponse(200, null, observeReq)
72+
)
73+
)
6574

66-
let parsedBody = undefined
67-
nock(utils.api)
68-
.put('/repos/Codertocat/Hello-World/pulls/2/merge', body => {
69-
parsedBody = body
70-
return body
71-
})
72-
.reply(200)
73-
74-
await handleCronJobs(context)
75-
expect(parsedBody).toEqual({
75+
await expect(handleCronJobs(context)).resolves.not.toThrow()
76+
expect(observeReq.body()).toEqual({
7677
merge_method: 'squash'
7778
})
78-
expect(nock.isDone()).toBe(true)
7979
})
8080

8181
it('merges the PR with rebase configured', async () => {
@@ -88,27 +88,18 @@ describe('cronLgtm', () => {
8888

8989
listPullReqs[0].labels[0].name = 'lgtm'
9090

91-
nock(utils.api)
92-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=1')
93-
.reply(200, listPullReqs)
94-
95-
nock(utils.api)
96-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=2')
97-
.reply(200, [])
98-
99-
let parsedBody = undefined
100-
nock(utils.api)
101-
.put('/repos/Codertocat/Hello-World/pulls/2/merge', body => {
102-
parsedBody = body
103-
return body
104-
})
105-
.reply(200)
91+
const observeReq = new utils.observeRequest()
92+
server.use(
93+
rest.put(
94+
`${utils.api}/repos/Codertocat/Hello-World/pulls/2/merge`,
95+
utils.mockResponse(200, null, observeReq)
96+
)
97+
)
10698

107-
await handleCronJobs(context)
108-
expect(parsedBody).toEqual({
99+
await expect(handleCronJobs(context)).resolves.not.toThrow()
100+
expect(observeReq.body()).toEqual({
109101
merge_method: 'rebase'
110102
})
111-
expect(nock.isDone()).toBe(true)
112103
})
113104

114105
it('wont merge the PR if the hold label is present', async () => {
@@ -120,24 +111,15 @@ describe('cronLgtm', () => {
120111

121112
listPullReqs[0].labels[0].name = 'lgtm'
122113
listPullReqs[0].labels.push({
123-
"id": 1,
124-
"node_id": "123",
125-
"url": "https://api.github.com/repos/octocat/Hello-World/labels/hold",
126-
"name": "hold",
127-
"description": "looks good to me",
128-
"color": "f29513",
129-
"default": true
114+
id: 1,
115+
node_id: '123',
116+
url: 'https://api.github.com/repos/octocat/Hello-World/labels/hold',
117+
name: 'hold',
118+
description: 'looks good to me',
119+
color: 'f29513',
120+
default: true
130121
})
131122

132-
nock(utils.api)
133-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=1')
134-
.reply(200, listPullReqs)
135-
136-
nock(utils.api)
137-
.get('/repos/Codertocat/Hello-World/pulls?state=open&page=2')
138-
.reply(200, [])
139-
140-
await handleCronJobs(context)
141-
expect(nock.isDone()).toBe(true)
123+
await expect(handleCronJobs(context)).resolves.not.toThrow()
142124
})
143125
})

0 commit comments

Comments
 (0)