Skip to content

Commit

Permalink
Fixes Issue-25 (dangoslen#27) (dangoslen#28)
Browse files Browse the repository at this point in the history
* Initial understanding of the problem; tests coming soon

* Tests complete

* Auto-building distribution

* Adding coverage reporting and badge

* Adjusting regex to support spaces at the begginning and dashes

* Auto-building distribution

* Update fetch command

* Auto-building distribution

* Manually packaging for distribution

* Auto-building distribution

* Attempting to use proper fetch command

* Debugging

* Moving args around

* Repackage and adds changelog
  • Loading branch information
dangoslen authored Sep 26, 2020
1 parent 98f6f4b commit 1c6ce65
Show file tree
Hide file tree
Showing 9 changed files with 692 additions and 923 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@ jobs:
- uses: actions/checkout@v1
- run: npm install
- run: npm test
# Generation the new distribution
- run: npm run package
- name: package-and-commit
uses: EndBug/add-and-commit@v4 # You can change this to use a specific version
with:
# The arguments for the `git add` command (see the paragraph below for more info)
# Default: '.'
add: 'dist'
author_name: 'GitHub Action'
message: 'Auto-building distribution'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# test action works running from the graph
enforce-changelog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./

# test action works running from the graph
enforce-changelog-v2:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [v1.4.0]
### Summary
Please upgrade to use with `actions/checkout@v2`!

### Fixes
- Now works with both `actions/checkout@v1` and `actions/checkout@v2`

### Adds
- Code coverage checks via `jest` and coverage badge via `make-coverage-badge`

## [v1.3.0]
### Security
- `[email protected]` to `[email protected]`
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p align="center">
<img src="https://github.com/dangoslen/changelog-enforcer/workflows/units-test/badge.svg?branch=master" alt="unit tests badge" />
<img src="https://img.shields.io/github/v/release/dangoslen/changelog-enforcer?color=orange&label=Latest&style=plastic" alt="latest version" />
<img src="./coverage/badge.svg" alt="coverage badge" />
</p>

## Changelog Enforcer
Expand All @@ -22,7 +23,7 @@ jobs:
changelog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: dangoslen/[email protected]
with:
changeLogPath: 'CHANGELOG.md'
Expand Down
76 changes: 58 additions & 18 deletions __tests__/changelog-enforcer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('the changelog-enforcer', () => {
})
})

it('should skip enforcing when label is present', () => {
it('should skip enforcing when label is present', (done) => {
// Mock getInput
inputs['skipLabel'] = 'Skip-Changelog'
inputs['changeLogPath'] = 'CHANGELOG.md'
Expand All @@ -34,64 +34,104 @@ describe('the changelog-enforcer', () => {
expect(infoSpy.mock.calls.length).toBe(2)
expect(execSpy).not.toHaveBeenCalled()
expect(failureSpy).not.toHaveBeenCalled()

done()
})
})

it('should enforce when label is not present; changelog is not present', () => {
it('should enforce when label is not present; changelog is not present; branch checked out', (done) => {
// Mock getInput
inputs['skipLabel'] = 'A different label'
inputs['changeLogPath'] = 'CHANGELOG.md'

const infoSpy = jest.spyOn(core, 'info').mockImplementation(jest.fn())
const failureSpy = jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())
const execSpy = jest.spyOn(exec, 'exec').mockImplementation((command, args, options) => {
const stdout =
let stdout = ''
if (args[0] == 'diff') {
stdout =
`M .env.js
A an_added_changed_file.js`

}
if (args[0] == 'branch') {
stdout =
` * (HEAD detached at pull/27/merge) 6a67f6e Merge
remotes/origin/master somecommithash
remotes/origin/changes someotherhash`
}
options.listeners.stdout(stdout)
return 0
})

changelogEnforcer.enforce()
.then(() => {
expect(infoSpy.mock.calls.length).toBe(2)
expect(execSpy).toHaveBeenCalled()
expect(execSpy.mock.calls.length).toBe(2)
expect(failureSpy).toHaveBeenCalled()

const command = execSpy.mock.calls[0][0]
const commandArgs = execSpy.mock.calls[0][1].join(' ')
expect(command).toBe('git')
expect(commandArgs).toBe('diff origin/master --name-status --diff-filter=AM')
const command_branch = execSpy.mock.calls[0][0]
const command_branch_args = execSpy.mock.calls[0][1].join(' ')
expect(command_branch).toBe('git')
expect(command_branch_args).toBe('branch --verbose --all')

const command_diff = execSpy.mock.calls[1][0]
const command_diff_args = execSpy.mock.calls[1][1].join(' ')
expect(command_diff).toBe('git')
expect(command_diff_args).toBe('diff origin/master --name-status --diff-filter=AM')

done()
})
})

it('should enforce when label is not present; changelog is present', () => {
it('should enforce when label is not present; changelog is present; branch not checked out', (done) => {
// Mock getInput
inputs['skipLabel'] = 'A different label'
inputs['changeLogPath'] = 'CHANGELOG.md'

const infoSpy = jest.spyOn(core, 'info').mockImplementation(jest.fn())
const failureSpy = jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())
const execSpy = jest.spyOn(exec, 'exec').mockImplementation((command, args, options) => {
const stdout =
if (args[2] == 'fetch') {
return 0
}

let stdout = ''
if (args[0] == 'diff') {
stdout =
`M .env.js
M CHANGELOG.md`

}
if (args[0] == 'branch') {
stdout =
` * (HEAD detached at pull/27/merge) 6a67f6e Merge
pull/27/merge 6a67f6f`
}
options.listeners.stdout(stdout)
return 0
})

changelogEnforcer.enforce()
.then(() => {
expect(infoSpy.mock.calls.length).toBe(2)
expect(execSpy).toHaveBeenCalled()
expect(execSpy.mock.calls.length).toBe(3)
expect(failureSpy).not.toHaveBeenCalled()

const command = execSpy.mock.calls[0][0]
const commandArgs = execSpy.mock.calls[0][1].join(' ')
expect(command).toBe('git')
expect(commandArgs).toBe('diff origin/master --name-status --diff-filter=AM')
const command_branch = execSpy.mock.calls[0][0]
const command_branch_args = execSpy.mock.calls[0][1].join(' ')
expect(command_branch).toBe('git')
expect(command_branch_args).toBe('branch --verbose --all')

const command_fetch = execSpy.mock.calls[1][0]
const command_fetch_args = execSpy.mock.calls[1][1].join(' ')
expect(command_fetch).toBe('git')
expect(command_fetch_args).toBe('-c protocol.version=2 fetch --depth=1 origin master')

const command_diff = execSpy.mock.calls[2][0]
const command_diff_args = execSpy.mock.calls[2][1].join(' ')
expect(command_diff).toBe('git')
expect(command_diff_args).toBe('diff origin/master --name-status --diff-filter=AM')

done()
})
})
})
59 changes: 42 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23034,37 +23034,62 @@ module.exports.enforce = async function() {
const baseRef = pullRequest.base.ref

if (!labelNames.includes(skipLabel)) {
let output = ''
const options = {}
options.listeners = {
stdout: (data) => {
output += data.toString();
}
}

await exec.exec('git', ['diff', `origin/${baseRef}`, '--name-status', '--diff-filter=AM'], options)

const fileNames = generateUpdatedFileList(output)
if (!fileNames.includes(changeLogPath)) {
throw new Error(`No update to ${changeLogPath} found!`)
}
await ensureBranchExists(baseRef)
await checkChangeLog(baseRef, changeLogPath)
}
} catch(error) {
core.setFailed(error.message);
}
};

function generateUpdatedFileList(output) {
async function ensureBranchExists(baseRef) {
let output = ''
const options = {}
options.listeners = {
stdout: (data) => {
output += data.toString();
}
}

await exec.exec('git', ['branch', '--verbose', '--all'], options)

const branches = output.split(/\r?\n/)
let branchNames = []
branches.map(change => {
const branchName = change.replace(/(^\s*[\.\w+/-]*)(\s*)([\w+].*)\n?$/g, '$1').trim()
branchNames.push(branchName)
})

if (!branchNames.includes(`remotes/origin/${baseRef}`)) {
await exec.exec('git', ['-c', 'protocol.version=2', 'fetch', '--depth=1', 'origin', `${baseRef}`], {})
}
}

async function checkChangeLog(baseRef, changeLogPath) {
let output = ''
const options = {}
options.listeners = {
stdout: (data) => {
output += data.toString();
}
}

await exec.exec('git', ['diff', `origin/${baseRef}`, '--name-status', '--diff-filter=AM'], options)

const changes = output.split(/\r?\n/)
let fileNames = []
changes.map(change => {
const fileName = change.replace(/(^[A-Z])(\s*)(.*)(\n)?$/g, '$3')
fileNames.push(fileName)
})
return fileNames;

if (!fileNames.includes(changeLogPath)) {
throw new Error(`No update to ${changeLogPath} found!`)
}
}



/***/ }),

/***/ 682:
Expand Down Expand Up @@ -26297,7 +26322,7 @@ function wrappy (fn, cb) {
/***/ 954:
/***/ (function(module) {

module.exports = {"_args":[["@octokit/[email protected]","/home/runner/work/changelog-enforcer/changelog-enforcer"]],"_from":"@octokit/[email protected]","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{"@octokit/types":"2.12.1","deprecation":"2.3.1","once":"1.4.0"},"_requested":{"type":"version","registry":true,"raw":"@octokit/[email protected]","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/home/runner/work/changelog-enforcer/changelog-enforcer","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};
module.exports = {"_args":[["@octokit/[email protected]","/Users/dgoslen/Development/changelog-enforcer"]],"_from":"@octokit/[email protected]","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{"@octokit/types":"2.12.1","deprecation":"2.3.1","once":"1.4.0"},"_requested":{"type":"version","registry":true,"raw":"@octokit/[email protected]","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/Users/dgoslen/Development/changelog-enforcer","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};

/***/ }),

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
setupFiles: ['./__tests__/env.js'],
testMatch: ['**/*.test.js'],
coverageReporters: ["json-summary"]
};
Loading

0 comments on commit 1c6ce65

Please sign in to comment.