Skip to content

Commit

Permalink
PWA-1781: Implemented changelog build script and adapted makefile to …
Browse files Browse the repository at this point in the history
…trigger it in stable deployments
  • Loading branch information
SG-Noxoreos committed Apr 3, 2019
1 parent 605d4b1 commit 4749d6f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 23 deletions.
32 changes: 9 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ publish-to-github:
git push origin "releases/$(RELEASE_NAME)";
ifeq ("$(STABLE)","true")
# STABLE RELEASE
@# $ (call build-changelog)
$(call build-changelog)
endif
ifeq ("$(STABLE)-$(UPDATE_MASTER)","true-true")
# UPDATING MASTER FOR STABLE RELEASE
Expand All @@ -374,29 +374,15 @@ define build-changelog
@echo "======================================================================"
@echo "| Creating changelog ..."
@echo "======================================================================"
# Update develop branch, first
git checkout origin/develop && git checkout -b develop;
git merge "releases/$(RELEASE_NAME)" --no-commit;
git status;
-git add . && git commit -m "Updating `develop` branch with stable release '$(RELEASE_NAME)'";
-git push origin develop;
git status;
git reset --hard;
git checkout "releases/$(RELEASE_NAME)";
git status;
# Create a dummy tag for the changelog creation tool
git tag "$(RELEASE_NAME)" && git push origin "releases/$(RELEASE_NAME)" --tags;
github_changelog_generator shopgate/pwa --token $(GITHUB_AUTH_TOKEN) --header-label "# Changelog" --exclude-tags-regex ".*\b(alpha|beta|rc)\b\.+\d{1,}" --bugs-label ":bug: **Fixed bugs:**" --pr-label ":nail_care: **Others:**" --enhancement-label ":rocket: **Enhancements:**" --release-branch "develop" --no-unreleased --no-compare-link --issue-line-labels "All" --since-tag "v2.8.1";
# Remove the dummy tag again, so it can be properly created with the changelog file inside
git push -d origin "refs/tags/$(RELEASE_NAME)";
git tag -d "$(RELEASE_NAME)";
git fetch origin;
touch CHANGELOG.md;
GITHUB_AUTH=$(GITHUB_AUTH_TOKEN) node ./scripts/build-changelog.js --next="$(RELEASE_VERSION)" > CHANGELOG_NEW.md;
mv CHANGELOG_NEW.md CHANGELOG.md;
$(foreach theme, $(THEMES), cp CHANGELOG.md themes/$(theme)/CHANGELOG.md;)
# Push the new changelog to GitHub (into the STABLE release branch)
git add "CHANGELOG.md";
git commit -m "Created changelog for version '$(RELEASE_NAME)'.";
git push origin "releases/$(RELEASE_NAME)" --tags;
# Recreate the tag with the changelog inside and push it to remote (origin)
git tag "$(RELEASE_NAME)" && git push origin "refs/tags/$(RELEASE_NAME)";
git add CHANGELOG.md;
$(foreach theme, $(THEMES), git add themes/$(theme)/CHANGELOG.md;)
-git commit -m "Created changelog for version '$(RELEASE_NAME)'.";
-git push origin "releases/$(RELEASE_NAME)" --tags;

endef

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-runtime": "^6.26.0",
"compare-versions": "^3.4.0",
"react-hot-loader": "4.8.0",
"react-transform-catch-errors": "^1.0.2",
"redbox-react": "^1.5.0"
Expand Down
144 changes: 144 additions & 0 deletions scripts/build-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/usr/bin/env node
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { argv } = require('yargs');
const fs = require('fs');
const compareVersions = require('compare-versions');
const { Changelog: LernaChangelog } = require('lerna-changelog');
const lernaConfiguration = require('lerna-changelog/lib/configuration');
const logger = require('./logger');

/**
* Parses a version into its components without prerelease information.
* @param {string} v The version to be parsed
* @return {{sub: number, major: number, minor: number}}
*/
function parseVersion(v) {
const [
, // Full match of no interest.
major = null,
sub = null,
minor = null,
] = /^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-.*)?$/.exec(v);
if (major === null || sub === null || minor === null) {
const err = new Error(`Invalid version string (${v})!`);
logger.error(err);
throw err;
}

return {
major,
sub,
minor,
};
}

if (!argv.next || argv.next.length === 0) {
const err = new Error('Required param "next" for next version was not specified!');
logger.error(err);
throw err;
}

/**
* Extends lerna-changelog's "Changelog" class to customize its behavior.
*/
class Changelog extends LernaChangelog {
/* eslint-disable class-methods-use-this */
/**
* Overrides original method to avoid printing a list of committers at the end of each release
* @override
* @return {Promise<null>}
*/
async getCommitters() {
return null;
}
/* eslint-enable class-methods-use-this */

/**
* @override
* @param {Object[]} commits The commits to process.
* @return {Object[]}
*/
toCommitInfos(commits) {
// Filter ot prerelease tags from commits
const ci = LernaChangelog.prototype.toCommitInfos.call(this, commits);
return ci.map(commit => ({
...commit,
tags: commit.tags ? commit.tags.filter(tag => !tag.includes('-')) : undefined,
}));
}

/**
* Filters out all releases that appear below the "from" release.
* @param {string} from Minimum version tag
* @param {string} to Maximum version tag
* @return {Promise<Object[]>}
*/
async listReleases(from, to) {
const min = parseVersion(from);
const releases = await LernaChangelog.prototype.listReleases.call(this, from, to);
return releases.filter((release) => {
// Always treat unreleased as "higher"
if (release.name === '___unreleased___') {
return true;
}

// Keep higher and equal versions
const cur = parseVersion(release.name);
return compareVersions(
`${cur.major}.${cur.sub}.${cur.minor}`,
`${min.major}.${min.sub}.${min.minor}`
) >= 0;
});
}
}

/**
* Main async method
*/
async function run() {
try {
// Find last release: Get tags, filter out wrong tags and pre-releases, then take last one.
const { stdout } = await exec("git tag | grep 'v' | grep -Ev '-' | tail -1");
const prevVersion = stdout.trim();
const nextVersion = parseVersion(argv.next);
const nextVersionString = `v${nextVersion.major}.${nextVersion.sub}.${nextVersion.minor}`;

// Read previous changelog to extend it (remove ending line feeds -> added back in later)
const changelogContent = fs.readFileSync('CHANGELOG.md', { encoding: 'utf8' }).trimRight();

const config = lernaConfiguration.load();

// This causes the "Unreleased" title to be replaced by a version that links to a github diff.
config.nextVersion = `[${
nextVersionString
}](https://github.com/shopgate/theme-gmd/compare/${prevVersion}...${nextVersionString})`;

// Skip creating if the "nextVersion" is already filled out.
if (changelogContent.includes(config.nextVersion)) {
// Output the already existing data when already is there already.
logger.log(changelogContent);
return;
}

const changelog = new Changelog(config);

const latestChanges = await changelog.createMarkdown({
tagFrom: prevVersion,
tagTo: nextVersionString,
});

// Add changes to the top of the main changelog
const newChangelog = changelogContent.replace(
'# Changelog\n',
`# Changelog\n\n${latestChanges.trim()}\n\n`
);

// Print the output for the bash/makefile to read
logger.log(newChangelog);
} catch (error) {
throw error;
}
}

run();

0 comments on commit 4749d6f

Please sign in to comment.