Skip to content

Commit

Permalink
Fabo/release PR now adds PENDING.md to CHANGELOG.md (#2242)
Browse files Browse the repository at this point in the history
* release PR now adds PENDING.md to CHANGELOG.md

* tests

* comments

* tests

* fixed tests

* linted

* ignore main coverage
  • Loading branch information
faboweb authored and jbibla committed Mar 12, 2019
1 parent f4d5b95 commit d0d446d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 45 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ jobs:
# - store_artifacts:
# path: builds/Gaia/linux_amd64/*

changelogUpdated:
pendingUpdated:
executor: node
steps:
- checkout
- run: tasks/changelog-changed-check.sh
- run: tasks/pending-changed-check.sh

lint:
executor: node
Expand Down Expand Up @@ -258,7 +258,7 @@ workflows:
build-and-deploy:
jobs:
# Static checks before
- changelogUpdated:
- pendingUpdated:
filters:
branches:
ignore: release
Expand Down Expand Up @@ -313,7 +313,7 @@ workflows:

- publish:
requires:
- changelogUpdated
- pendingUpdated
- build
- testUnit
- testE2e
Expand Down
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changed

- [#\2210](https://github.com/cosmos/voyager/pull/2210) Faucet give money only to non-existent addresses @sabau
- release PR now adds PENDING.md to CHANGELOG.md @faboweb
- [#\2202](https://github.com/cosmos/voyager/pull/2202) Fix circle configuration for publishing @sabau
- [#\2236](https://github.com/cosmos/voyager/pull/2236) ES lint no tabs @sabau
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test:coverage": "http-server test/unit/coverage/lcov-report",
"release": "node ./tasks/releasePullRequest.js",
"precommit": "yarn lint:fix",
"prepush": "bash ./tasks/changelog-changed-check.sh && yarn lint",
"prepush": "bash ./tasks/pending-changed-check.sh && yarn lint",
"postcheckout": "yarn",
"watch": "tasks/watch.sh",
"nodes": "node ./tasks/local-testnet/start.js",
Expand Down
88 changes: 53 additions & 35 deletions tasks/createReleasePR.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { cli, shell } = require(`@nodeguy/cli`)
const fs = require(`fs`)
const { join } = require(`path`)
const octokit = require(`@octokit/rest`)()

function bumpVersion(versionString) {
Expand All @@ -12,24 +13,24 @@ function bumpVersion(versionString) {
return versionElements.join(`.`)
}

function updateChangeLog(changeLog, newVersion, now) {
function updateChangeLog(changeLog, pending, newVersion, now) {
const today = now.toISOString().slice(0, 10)

return changeLog.replace(
`## [Unreleased]`,
`## [Unreleased]\n\n## [${newVersion}] - ${today}`
`## [Unreleased]\n\n## [${newVersion}] - ${today}\n\n${pending}`
)
}

const updatePackageJson = (packageJson, version) =>
Object.assign({}, packageJson, { version })

const pushCommit = ({ token, branch }) =>
const pushCommit = (shell, { token, branch }) =>
shell(`
set -o verbose
git config --local user.name "Voyager Bot"
git config --local user.email "[email protected]"
git add CHANGELOG.md package.json
git add CHANGELOG.md PENDING.md package.json
git commit --message="Bump version for release."
git tag --force release-candidate
git remote add bot https://${token}@github.com/cosmos/voyager.git
Expand All @@ -39,7 +40,7 @@ git push --force --tags bot HEAD:${branch}
const recentChanges = changeLog =>
changeLog.match(/.+?## .+?\n## .+?\n\n(.+?)\n## /s)[1]

const createPullRequest = async ({ changeLog, token, tag, head }) => {
const createPullRequest = async (octokit, { changeLog, token, tag, head }) => {
octokit.authenticate({
type: `token`,
token
Expand All @@ -56,41 +57,58 @@ const createPullRequest = async ({ changeLog, token, tag, head }) => {
})
}

async function main({ octokit, shell, fs }, changeLog, pending, packageJson) {
// only update if sth changed
if (pending.trim() === ``) return

console.log(`Making release...`)

const oldVersion = packageJson.version
const newVersion = bumpVersion(oldVersion)
console.log(`New version:`, newVersion)
const newChangeLog = updateChangeLog(
changeLog,
pending,
newVersion,
new Date()
)
const newPackageJson = updatePackageJson(packageJson, newVersion)

fs.writeFileSync(join(__dirname, `..`, `PENDING.md`), ``, `utf8`)
fs.writeFileSync(join(__dirname, `..`, `CHANGELOG.md`), newChangeLog, `utf8`)
fs.writeFileSync(
join(__dirname, `..`, `package.json`),
JSON.stringify(newPackageJson, null, 2) + `\n`,
`utf8`
)

console.log(`--- Committing release changes ---`)

const tag = `v${newVersion}`
const branch = `release-candidate/${tag}`
await pushCommit(shell, { token: process.env.GIT_BOT_TOKEN, branch })

await createPullRequest(octokit, {
changeLog: newChangeLog,
token: process.env.GIT_BOT_TOKEN,
tag,
head: branch
})
}

if (require.main === module) {
cli({}, async () => {
console.log(`Making release...`)
const changeLog = fs.readFileSync(__dirname + `/../CHANGELOG.md`, `utf8`)
const packageJson = require(__dirname + `/../package.json`)
const oldVersion = packageJson.version
const newVersion = bumpVersion(oldVersion)
console.log(`New version:`, newVersion)
const newChangeLog = updateChangeLog(changeLog, newVersion, new Date())
const newPackageJson = updatePackageJson(packageJson, newVersion)

fs.writeFileSync(__dirname + `/../CHANGELOG.md`, newChangeLog, `utf8`)

fs.writeFileSync(
__dirname + `/../package.json`,
JSON.stringify(newPackageJson, null, 2) + `\n`,
`utf8`
)

console.log(`--- Committing release changes ---`)

const tag = `v${newVersion}`
const branch = `release-candidate/${tag}`
await pushCommit({ token: process.env.GIT_BOT_TOKEN, branch })

await createPullRequest({
changeLog: newChangeLog,
token: process.env.GIT_BOT_TOKEN,
tag,
head: branch
})
/* istanbul ignore next */
cli({}, () => {
const changeLog = fs.readFileSync(join(__dirname, `..`, `CHANGELOG.md`), `utf8`)
const pending = fs.readFileSync(join(__dirname, `..`, `PENDING.md`), `utf8`)
const packageJson = require(join(__dirname, `..`, `package.json`))

main({ octokit, shell, fs }, changeLog, pending, packageJson)
})
}

module.exports = {
main,
bumpVersion,
updateChangeLog,
updatePackageJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

currentBranch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$currentBranch" = "master" ] || [ "$currentBranch" = "release" ] || [ "$currentBranch" = "develop" ]; then
echo "This branch is the develop branch. Checks on updating the changelog are omitted."
echo "This branch is the develop branch. Checks on updating the PENDING log are omitted."
exit 0;
fi
if [ "$(git diff --name-only origin/develop | grep -c PENDING.md)" -ge 1 ]; then
echo "CHANGELOG updated"
echo "PENDING updated"
exit 0;
else
echo "!! CHANGELOG not updated !!"
echo "!! PENDING not updated !!"
exit 1;
fi
52 changes: 50 additions & 2 deletions test/unit/specs/tasks/createReleasePR.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
## [0.6.1] - 2018-05-24
`

const pending = `### Added
- xxx @faboweb
### Changed
- yyy @fedekunze`

const newVersion = `0.6.2`
const now = new Date(`2018-05-25`)

Expand All @@ -34,9 +42,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.6.2] - 2018-05-25
### Added
- xxx @faboweb
### Changed
- yyy @fedekunze
## [0.6.1] - 2018-05-24
`

expect(release.updateChangeLog(previous, newVersion, now)).toEqual(updated)
expect(
release.updateChangeLog(previous, pending, newVersion, now)
).toEqual(updated)
})

it(`updates package.json`, () => {
Expand All @@ -56,3 +74,33 @@ it(`updates package.json`, () => {

expect(release.updatePackageJson(previous, newVersion)).toEqual(updated)
})

it(`creates release PR`, async () => {
const octokit = {
authenticate: () => {},
pullRequests: {
create: jest.fn()
}
}
const shell = () => {}
const fs = {
writeFileSync: () => {}
}
await expect(release.main({ octokit, shell, fs }, `\n\n## [Unreleased]\n\n## [0.6.1] - 2018-05-24`, `XXX`, { version: `0.0.1` })).resolved
expect(octokit.pullRequests.create).toHaveBeenCalled()
})

it(`don't create PR if nothing changed`, () => {
const octokit = {
authenticate: () => {},
pullRequests: {
create: jest.fn()
}
}
const shell = () => {}
const fs = {
writeFileSync: () => {}
}
expect(release.main({ octokit, shell, fs }, `\n\n## [Unreleased]\n\n## [0.6.1] - 2018-05-24`, ``, { version: `0.0.1` })).resolved
expect(octokit.pullRequests.create).not.toHaveBeenCalled()
})

0 comments on commit d0d446d

Please sign in to comment.