-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve semantic detection and add a real test suite (#16)
* remove old tests from WIP-bot * update deps and test config * ignore jest test coverage * start writing real tests * make the readme more better * remove unused deps and update package description * update package lock * tests are passing yay * remove unneeded request headers from POST mock * just one mock * add more tests * nothing
- Loading branch information
Showing
9 changed files
with
2,270 additions
and
1,350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
.npmrc | ||
coverage | ||
node_modules | ||
private-key.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const handlePullRequestChange = require('../lib/handle-pull-request-change') | ||
const nock = require('nock') | ||
const github = require('@octokit/rest')() | ||
|
||
// prevent all network activity to ensure mocks are used | ||
nock.disableNetConnect() | ||
|
||
describe('handlePullRequestChange', () => { | ||
test('it is a function', () => { | ||
expect(typeof handlePullRequestChange).toBe('function') | ||
}) | ||
|
||
test('sets `pending` status if PR has no semantic commits and no semantic title', async () => { | ||
const context = buildContext() | ||
context.payload.pull_request.title = 'do a thing' | ||
const commits = [ | ||
{commit: {message: 'fix something'}}, | ||
{commit: {message: 'fix something else'}} | ||
] | ||
const expectedBody = { | ||
state: 'pending', | ||
target_url: 'https://github.com/probot/semantic-pull-requests', | ||
description: 'add semantic commit or PR title', | ||
context: 'Semantic Pull Request' | ||
} | ||
|
||
const mock = nock('https://api.github.com') | ||
.get('/repos/sally/project-x/pulls/123/commits') | ||
.reply(200, commits) | ||
.post('/repos/sally/project-x/statuses/abcdefg', expectedBody) | ||
.reply(200) | ||
|
||
await handlePullRequestChange(context) | ||
expect(mock.isDone()).toBe(true) | ||
}) | ||
|
||
test('sets `success` status if PR has a semantic title', async () => { | ||
const context = buildContext() | ||
context.payload.pull_request.title = 'fix: bananas' | ||
const expectedBody = { | ||
state: 'success', | ||
description: 'good to go', | ||
target_url: 'https://github.com/probot/semantic-pull-requests', | ||
context: 'Semantic Pull Request' | ||
} | ||
|
||
// since the title is semantic, no GET request for commits is needed | ||
const mock = nock('https://api.github.com') | ||
.post('/repos/sally/project-x/statuses/abcdefg', expectedBody) | ||
.reply(200) | ||
|
||
await handlePullRequestChange(context) | ||
expect(mock.isDone()).toBe(true) | ||
}) | ||
|
||
test('allows `build:` as a prefix', async () => { | ||
const context = buildContext() | ||
context.payload.pull_request.title = 'build: publish to npm' | ||
const expectedBody = { | ||
state: 'success', | ||
description: 'good to go', | ||
target_url: 'https://github.com/probot/semantic-pull-requests', | ||
context: 'Semantic Pull Request' | ||
} | ||
|
||
// since the title is semantic, no GET request for commits is needed | ||
const mock = nock('https://api.github.com') | ||
.post('/repos/sally/project-x/statuses/abcdefg', expectedBody) | ||
.reply(200) | ||
|
||
await handlePullRequestChange(context) | ||
expect(mock.isDone()).toBe(true) | ||
}) | ||
}) | ||
|
||
function buildContext (overrides) { | ||
const defaults = { | ||
log: () => { /* no-op */ }, | ||
|
||
// an instantiated GitHub client like the one probot provides | ||
github: github, | ||
|
||
// context.repo() is a probot convenience function | ||
repo: (obj = {}) => { | ||
return Object.assign({owner: 'sally', repo: 'project-x'}, obj) | ||
}, | ||
|
||
payload: { | ||
pull_request: { | ||
number: 123, | ||
title: 'do a thing', | ||
head: { | ||
sha: 'abcdefg' | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Object.assign({}, defaults, overrides) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const isSemanticMessage = require('../lib/is-semantic-message') | ||
|
||
test('returns true when messages are semantic', () => { | ||
expect(isSemanticMessage('fix: something')).toBe(true) | ||
}) | ||
|
||
test('allows parenthetical scope following the type', () => { | ||
expect(isSemanticMessage('fix(subsystem): something')).toBe(true) | ||
}) | ||
|
||
test('returns false on bad input', () => { | ||
expect(isSemanticMessage('')).toBe(false) | ||
expect(isSemanticMessage(null)).toBe(false) | ||
expect(isSemanticMessage('non-semantic commit message')).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,28 @@ | ||
module.exports = handlePullRequestChange | ||
|
||
const simpleCommitMessage = require('simple-commit-message') | ||
|
||
async function handlePullRequestChange (context) { | ||
const {title, html_url: htmlUrl, head} = context.payload.pull_request | ||
const isSemantic = isSemanticMessage(title) || await commitsAreSemantic(context) | ||
const status = isSemantic ? 'success' : 'pending' | ||
|
||
console.log(`Updating PR "${title}" (${htmlUrl}): ${status}`) | ||
|
||
context.github.repos.createStatus(context.repo({ | ||
sha: head.sha, | ||
state: status, | ||
target_url: 'https://github.com/probot/semantic-pull-requests', | ||
description: isSemantic ? 'good to go' : 'add semantic commit or PR title' , | ||
context: 'Semantic Pull Request' | ||
})) | ||
} | ||
|
||
function isSemanticMessage(message) { | ||
return simpleCommitMessage.validate(message) | ||
} | ||
const isSemanticMessage = require('./is-semantic-message') | ||
|
||
async function commitsAreSemantic (context) { | ||
const commits = await context.github.pullRequests.getCommits(context.repo({ | ||
number: context.payload.pull_request.number | ||
})) | ||
|
||
console.log('commits[0]', commits[0]) | ||
return commits.data | ||
.map(element => element.commit) | ||
.some(commit => isSemanticMessage(commit.message)) | ||
} | ||
|
||
return commits.data.map(element => element.commit.message).some(isSemanticMessage) | ||
async function handlePullRequestChange (context) { | ||
const {title, head} = context.payload.pull_request | ||
const isSemantic = isSemanticMessage(title) || await commitsAreSemantic(context) | ||
const state = isSemantic ? 'success' : 'pending' | ||
const status = { | ||
sha: head.sha, | ||
state, | ||
target_url: 'https://github.com/probot/semantic-pull-requests', | ||
description: isSemantic ? 'good to go' : 'add semantic commit or PR title', | ||
context: 'Semantic Pull Request' | ||
} | ||
const result = await context.github.repos.createStatus(context.repo(status)) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const commitTypes = Object.keys(require('conventional-commit-types').types) | ||
const parseCommitMessage = require('parse-commit-message').parse | ||
|
||
module.exports = function isSemanticMessage (message) { | ||
try { | ||
const {type} = parseCommitMessage(message) | ||
return commitTypes.includes(type) | ||
} catch (e) { | ||
return false | ||
} | ||
} |
Oops, something went wrong.