Skip to content

Commit

Permalink
docs: add generated changelog to the documentation site (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorrusakov authored May 6, 2022
1 parent c054541 commit 5de31d9
Show file tree
Hide file tree
Showing 9 changed files with 3,159 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"*.config.js",
"*.config.*.js",
"**/*.config.js",
"component-generator/**"
"component-generator/**",
"generate-changelog.js"
]
}
],
Expand Down
9 changes: 8 additions & 1 deletion .releaserc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"branches": ["master", "next"]
"branches": ["master", "next"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git"
]
}
2,797 changes: 2,797 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions generate-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { program } = require('commander');
const { execSync } = require('child_process');
const fs = require('fs');

program
.requiredOption('--repoUrl <name>', 'Provide a git repo url', 'https://github.com/openedx/paragon')
.action((options) => {
if (fs.existsSync('CHANGELOG.md')) {
throw new Error('CHANGELOG file already exists.');
}

const { repoUrl } = options;
const cmd = 'git log --pretty=format:"%h|%H|%ad|%s|%d|%b||" --date=short';
const commitLog = execSync(cmd, { encoding: 'utf8' }).split('||\n');

// store information of the current release while we go over all
// commits between this release and previous one
let bugFixes = '';
let features = '';
let header = '';
let breakingChanges = '';
let currentTag;

commitLog.forEach(entry => {
const [shaShort, shaFull, date, commitSubject, refNames, body] = entry.split('|');

if (!header) {
header = `## %RELEASE_LINK% (${date})\n\n`;
}

// check if commit has a tag attached which would indicate a new release
let prevTag = refNames && refNames.match(/tag: (.*?)(,|\))/g);
if (prevTag) {
prevTag = prevTag[0].split(':')[1].trim().slice(0, -1);
}

// we found a previous release! time to add an entry to CHANGELOG.md
// with all the collected changes between releases
if (prevTag && currentTag) {
fs.appendFileSync('./CHANGELOG.md', header.replace('%RELEASE_LINK%', `[${currentTag}](${repoUrl}/compare/${prevTag}...${currentTag})`));
if (features) {
fs.appendFileSync('./CHANGELOG.md', `### Features\n${features}\n\n`);
features = '';
}
if (bugFixes) {
fs.appendFileSync('./CHANGELOG.md', `### Bug Fixes\n${bugFixes}\n\n`);
bugFixes = '';
}
if (breakingChanges) {
fs.appendFileSync('./CHANGELOG.md', `### BREAKING CHANGES\n${breakingChanges}\n\n`);
breakingChanges = '';
}
fs.appendFileSync('./CHANGELOG.md', '\n\n');
header = `## %RELEASE_LINK% (${date})\n\n`;
}

if (commitSubject) {
let commitMsgToAdd = commitSubject.trim();
const issue = commitSubject && commitSubject.match(/\(#\d+\)$/g);

if (issue && issue.length > 0) {
const issueId = issue[0].trim().slice(1, -1);
commitMsgToAdd = commitMsgToAdd.replace(issue[0], `([${issueId}](${repoUrl}/issues/${issueId.slice(1)}))`);
}

// add link to the commit
commitMsgToAdd = `${commitMsgToAdd} ([${shaShort}](${repoUrl}/commit/${shaFull}))`;

if (commitMsgToAdd.startsWith('feat:')) {
features += `\n* ${commitMsgToAdd.slice(5)}`;
} else if (commitMsgToAdd.startsWith('fix:')) {
bugFixes += `\n* ${commitMsgToAdd.slice(4)} `;
}
}

if (body) {
breakingChanges = body.includes('BREAKING CHANGE') && body.split('BREAKING CHANGE').pop().slice(2);
}

if (prevTag) {
currentTag = prevTag;
}
});
});

program.parse(process.argv);
Loading

0 comments on commit 5de31d9

Please sign in to comment.