Skip to content

Commit 7fe95d5

Browse files
committed
Add support for GH Actions
1 parent 40e43ae commit 7fe95d5

File tree

2 files changed

+142
-35
lines changed

2 files changed

+142
-35
lines changed

index.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,27 @@ function diff() {
1717

1818
function transform(tree, file, next) {
1919
var base = file.dirname
20-
var commitRange = process.env.TRAVIS_COMMIT_RANGE
21-
var range = (commitRange || '').split(/\.{3}/)
20+
var commitRange
21+
var range
22+
23+
// Looks like Travis.
24+
if (process.env.TRAVIS_COMMIT_RANGE) {
25+
commitRange = process.env.TRAVIS_COMMIT_RANGE
26+
range = commitRange.split(/\.{3}/)
27+
}
28+
// Looks like GH Actions.
29+
else if (process.env.GITHUB_SHA) {
30+
range =
31+
// This is a PR: check the whole PR.
32+
// Refs take the form `refs/heads/main`.
33+
process.env.GITHUB_BASE_REF && process.env.GITHUB_HEAD_REF
34+
? [
35+
process.env.GITHUB_BASE_REF.split('/').pop(),
36+
process.env.GITHUB_HEAD_REF.split('/').pop()
37+
]
38+
: [process.env.GITHUB_SHA + '^1', process.env.GITHUB_SHA]
39+
commitRange = range.join('...')
40+
}
2241

2342
if (!base || !commitRange || range.length !== 2) {
2443
return next()

test/index.js

+121-33
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ var processor = require('./processor')()
1010

1111
var exec = promisify(cp.exec)
1212

13-
test('diff()', function (t) {
14-
var current = process.cwd()
15-
var range = process.env.TRAVIS_COMMIT_RANGE
13+
var range = process.env.TRAVIS_COMMIT_RANGE
14+
var sha = process.env.GITHUB_SHA
15+
var base = process.env.GITHUB_BASE_REF
16+
var head = process.env.GITHUB_HEAD_REF
17+
18+
// Remove potential variables that we’re testing on CIs.
19+
delete process.env.TRAVIS_COMMIT_RANGE
20+
delete process.env.GITHUB_SHA
21+
delete process.env.GITHUB_BASE_REF
22+
delete process.env.GITHUB_HEAD_REF
23+
24+
var current = process.cwd()
25+
26+
process.chdir(path.join(current, 'test'))
27+
28+
test('diff() (travis)', function (t) {
1629
var stepOne = [
1730
'Lorem ipsum dolor sit amet.',
1831
'',
@@ -25,27 +38,11 @@ test('diff()', function (t) {
2538
var initial
2639
var final
2740

28-
delete process.env.TRAVIS_COMMIT_RANGE
29-
3041
t.plan(7)
3142

32-
process.chdir(path.join(current, 'test'))
33-
3443
exec('git init')
3544
// Set up.
36-
.then(() => {
37-
return exec('git config --global user.email').catch(oncatch)
38-
39-
function oncatch() {
40-
return exec('git config --global user.email [email protected]').then(
41-
onemail
42-
)
43-
}
44-
45-
function onemail() {
46-
return exec('git config --global user.name Ex Ample')
47-
}
48-
})
45+
.then(() => exec('git config --global user.email').catch(setEmailAndName))
4946
// Add initial file.
5047
.then(() =>
5148
processor.process(vfile({path: 'example.txt', contents: stepOne}))
@@ -64,16 +61,15 @@ test('diff()', function (t) {
6461
.then(() => exec('git rev-parse HEAD'))
6562
.then((result) => {
6663
initial = result.stdout.trim()
67-
return vfile.write({path: 'example.txt', contents: stepTwo})
6864
})
69-
// Changed files.
65+
// Change files.
66+
.then(() => vfile.write({path: 'example.txt', contents: stepTwo}))
7067
.then(() => exec('git add example.txt'))
7168
.then(() => exec('git commit -m two'))
7269
.then(() => exec('git rev-parse HEAD'))
7370
.then((result) => {
7471
final = result.stdout.trim()
7572
process.env.TRAVIS_COMMIT_RANGE = [initial, final].join('...')
76-
7773
return processor.process(vfile({path: 'example.txt', contents: stepTwo}))
7874
})
7975
.then((file) => {
@@ -82,24 +78,22 @@ test('diff()', function (t) {
8278
['example.txt:5:1-5:6: No lorem!'],
8379
'should show only messages for changed lines'
8480
)
85-
86-
return file
8781
})
8882
// Again!
89-
.then(() => {
90-
return processor.process(vfile({path: 'example.txt', contents: stepTwo}))
91-
})
83+
.then(() =>
84+
processor.process(vfile({path: 'example.txt', contents: stepTwo}))
85+
)
9286
.then((file) => {
9387
t.deepEqual(
9488
file.messages.map(String),
9589
['example.txt:5:1-5:6: No lorem!'],
9690
'should not recheck (coverage for optimisations)'
9791
)
9892
})
99-
// Unstages files.
100-
.then(() => {
101-
return processor.process(vfile({path: 'missing.txt', contents: other}))
102-
})
93+
// Unstaged files.
94+
.then(() =>
95+
processor.process(vfile({path: 'missing.txt', contents: other}))
96+
)
10397
.then((file) => {
10498
t.deepEqual(file.messages.map(String), [], 'should ignore unstaged files')
10599
})
@@ -136,17 +130,111 @@ test('diff()', function (t) {
136130

137131
return processor.process(vfile({path: 'new.txt', contents: other}))
138132
})
133+
.then(
134+
() => t.pass('should pass'),
135+
(error) => t.ifErr(error, 'should not fail')
136+
)
139137
// Restore
140138
.then(restore, restore)
139+
140+
function restore() {
141+
delete process.env.TRAVIS_COMMIT_RANGE
142+
return rimraf('.git')
143+
.then(() => rimraf('new.txt'))
144+
.then(() => rimraf('example.txt'))
145+
}
146+
})
147+
148+
test('diff() (GitHub Actions)', function (t) {
149+
var stepOne = [
150+
'Lorem ipsum dolor sit amet.',
151+
'',
152+
'Lorem ipsum. Dolor sit amet.',
153+
''
154+
].join('\n')
155+
var stepTwo = stepOne + '\nLorem ipsum.\n'
156+
var stepThree = 'Lorem.\n\n' + stepOne + '\nAlpha bravo.\n'
157+
var stepFour = stepThree + '\nIpsum lorem.\n'
158+
var main
159+
160+
t.plan(3)
161+
162+
exec('git init')
163+
// Set up.
164+
.then(() => exec('git config --global user.email').catch(setEmailAndName))
165+
// Add initial file.
166+
.then(() => vfile.write({path: 'example.txt', contents: stepOne}))
167+
.then(() => exec('git add example.txt'))
168+
.then(() => exec('git commit -m one'))
169+
// Change file.
170+
.then(() => vfile.write({path: 'example.txt', contents: stepTwo}))
171+
.then(() => exec('git add example.txt'))
172+
.then(() => exec('git commit -m two'))
173+
.then(() => exec('git rev-parse HEAD'))
174+
.then((result) => {
175+
process.env.GITHUB_SHA = result.stdout.trim()
176+
return processor.process(vfile({path: 'example.txt', contents: stepTwo}))
177+
})
178+
.then((file) => {
179+
t.deepEqual(
180+
file.messages.map(String),
181+
['example.txt:5:1-5:6: No lorem!'],
182+
'should show only messages for this commit'
183+
)
184+
})
185+
// A PR.
186+
.then(() => exec('git branch --show-current'))
187+
.then((result) => {
188+
main = result.stdout.trim()
189+
exec('git checkout -b other-branch')
190+
})
191+
// Change file.
192+
.then(() => vfile.write({path: 'example.txt', contents: stepThree}))
193+
.then(() => exec('git add example.txt'))
194+
.then(() => exec('git commit -m three'))
195+
.then(() => vfile.write({path: 'example.txt', contents: stepFour}))
196+
.then(() => exec('git add example.txt'))
197+
.then(() => exec('git commit -m four'))
198+
.then(() => exec('git rev-parse HEAD'))
199+
.then((result) => {
200+
process.env.GITHUB_SHA = result.stdout.trim()
201+
process.env.GITHUB_BASE_REF = 'refs/heads/' + main
202+
process.env.GITHUB_HEAD_REF = 'refs/heads/other-branch'
203+
return processor.process(vfile({path: 'example.txt', contents: stepFour}))
204+
})
205+
.then((file) => {
206+
t.deepEqual(
207+
file.messages.map(String),
208+
['example.txt:1:1-1:6: No lorem!', 'example.txt:9:7-9:12: No lorem!'],
209+
'should deal with PRs'
210+
)
211+
})
212+
// Restore
141213
.then(
142214
() => t.pass('should pass'),
143215
(error) => t.ifErr(error, 'should not fail')
144216
)
217+
.then(restore, restore)
145218

146219
function restore() {
147-
process.env.TRAVIS_COMMIT_RANGE = range
148220
return rimraf('.git')
149221
.then(() => rimraf('new.txt'))
150222
.then(() => rimraf('example.txt'))
151223
}
152224
})
225+
226+
process.on('exit', function () {
227+
process.env.TRAVIS_COMMIT_RANGE = range
228+
process.env.GITHUB_SHA = sha
229+
process.env.GITHUB_BASE_REF = base
230+
process.env.GITHUB_HEAD_REF = head
231+
process.chdir(path.join(current))
232+
})
233+
234+
function setEmailAndName() {
235+
return exec('git config --global user.email [email protected]').then(setEmail)
236+
}
237+
238+
function setEmail() {
239+
return exec('git config --global user.name Ex Ample')
240+
}

0 commit comments

Comments
 (0)