Skip to content

Commit d46c783

Browse files
authored
fix: scenario for modified|added mergeable config and pull_request_review (mergeability#140)
* fix: scenario for modified|added mergeable config and pull_request_review events to also pull from PR modified files. * chore: upgrade circle to 2 * chore: do not update npm to latest because there is a bug. see npm/npm#16807 (comment)
1 parent 08b3e6a commit d46c783

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

.circleci/config.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: 2 # use CircleCI 2.0
2+
jobs: # a collection of steps
3+
build: # runs not using Workflows must have a `build` job as entry point
4+
docker: # run the steps with Docker
5+
- image: circleci/node:8.8.1 # ...with this image as the primary container; this is where all `steps` will run
6+
steps: # a collection of executable commands
7+
- checkout # special step to check out source code to working directory
8+
# - run:
9+
# name: update-npm
10+
# command: 'sudo npm install -g npm@latest'
11+
- restore_cache: # special step to restore the dependency cache
12+
# Read about caching dependencies: https://circleci.com/docs/2.0/caching/
13+
key: dependency-cache-{{ checksum "package.json" }}
14+
- run:
15+
name: install-npm
16+
command: npm install
17+
- save_cache: # special step to save the dependency cache
18+
key: dependency-cache-{{ checksum "package.json" }}
19+
paths:
20+
- ./node_modules
21+
- run: # run tests
22+
name: test
23+
command: npm test
24+
- store_artifacts: # special step to save test results as as artifact
25+
# Upload test summary for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
26+
path: test-results.xml
27+
prefix: tests
28+
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
29+
path: coverage
30+
prefix: coverage
31+
- store_test_results: # for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/
32+
path: test-results.xml
33+
# See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples

__tests__/configuration/configuration.test.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('config file fetching', () => {
2424
expect(content).toBe(configString)
2525
})
2626

27-
test('fetch from main branch if the event is pull_request and file is not modified', async () => {
27+
test('fetch from main branch if the event is PR relevant and file is not modified or added', async () => {
2828
let configString = `
2929
mergeable:
3030
issues:
@@ -43,14 +43,24 @@ describe('config file fetching', () => {
4343
days: 20
4444
message: Issue test
4545
`
46-
let context = createMockGhConfig(configString, prConfig, { files: ['someFile'] })
46+
let context = createMockGhConfig(
47+
configString,
48+
prConfig,
49+
{ files: [ { filename: 'someFile', status: 'modified' } ] }
50+
)
51+
4752
context.event = 'pull_request'
4853
let file = await Configuration.fetchConfigFile(context)
49-
const content = Buffer.from(file.data.content, 'base64').toString()
54+
let content = Buffer.from(file.data.content, 'base64').toString()
55+
expect(content).toBe(configString)
56+
57+
context.event = 'pull_request_review'
58+
file = await Configuration.fetchConfigFile(context)
59+
content = Buffer.from(file.data.content, 'base64').toString()
5060
expect(content).toBe(configString)
5161
})
5262

53-
test('fetch from head branch if the event is pull_request and file is modified', async () => {
63+
test('fetch from head branch if the event is relevant to PR and file is modified or added', async () => {
5464
let configString = `
5565
mergeable:
5666
issues:
@@ -60,19 +70,35 @@ describe('config file fetching', () => {
6070
pull_requests:
6171
stale:
6272
days: 20
63-
message: PR test
73+
message: from HEAD
6474
`
6575
let prConfig = `
6676
mergeable:
6777
issues:
6878
stale:
6979
days: 20
70-
message: Issue test
80+
message: From PR Config
7181
`
72-
let context = createMockGhConfig(configString, prConfig, { files: ['.github/mergeable.yml'] })
82+
let files = {files: [
83+
{ filename: '.github/mergeable.yml', status: 'modified' }
84+
]}
85+
let context = createMockGhConfig(configString, prConfig, files)
7386
context.event = 'pull_request'
7487
let file = await Configuration.fetchConfigFile(context)
75-
const content = Buffer.from(file.data.content, 'base64').toString()
88+
let content = Buffer.from(file.data.content, 'base64').toString()
89+
expect(content).toBe(prConfig)
90+
91+
context.event = 'pull_request_review'
92+
file = await Configuration.fetchConfigFile(context)
93+
content = Buffer.from(file.data.content, 'base64').toString()
94+
expect(content).toBe(prConfig)
95+
96+
files = {files: [
97+
{ file: '.github/mergeable.yml', status: 'added' }
98+
]}
99+
context = createMockGhConfig(configString, prConfig, files)
100+
context.event = 'pull_request'
101+
content = Buffer.from(file.data.content, 'base64').toString()
76102
expect(content).toBe(prConfig)
77103
})
78104
})
@@ -340,7 +366,7 @@ const createMockGhConfig = (json, prConfig, options) => {
340366
},
341367
pullRequests: {
342368
getFiles: () => {
343-
return { data: options.files && options.files.map(file => ({filename: file, status: 'modified'})) }
369+
return { data: options.files }
344370
}
345371
}
346372
}

circle.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/configuration/configuration.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ class Configuration {
111111
let github = context.github
112112
let repo = context.repo()
113113

114-
if (context.event === 'pull_request') {
114+
if (['pull_request', 'pull_request_review'].includes(context.event)) {
115115
// get modified file list
116-
let result = await context.github.pullRequests.getFiles(context.repo({number: context.payload[context.event].number}))
117-
let modifiedFiles = result.data.filter(file => file.status === 'modified').map(file => file.filename)
116+
let result = await context.github.pullRequests.getFiles(context.repo({number: context.payload.pull_request.number}))
117+
let modifiedFiles = result.data
118+
.filter(file => ['modified', 'added'].includes(file.status))
119+
.map(file => file.filename)
118120

119121
// check if config file is in that list
120122
if (modifiedFiles.includes(Configuration.FILE_NAME)) {

0 commit comments

Comments
 (0)