Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changelog generation script #4386

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/fragments/1234.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fix:
- This is a sample fixed

feat:
- Introduces a new feature
5 changes: 5 additions & 0 deletions changelogs/fragments/5678.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
feat:
- Introduces a new feature pt 2

doc:
- Document a feature
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"docs:acceptApiChanges": "scripts/use_node --max-old-space-size=6144 scripts/check_published_api_changes.js --accept",
"osd:bootstrap": "scripts/use_node scripts/build_ts_refs && scripts/use_node scripts/register_git_hook",
"spec_to_console": "scripts/use_node scripts/spec_to_console",
"pkg-version": "scripts/use_node -e \"console.log(require('./package.json').version)\""
"pkg-version": "scripts/use_node -e \"console.log(require('./package.json').version)\"",
"changelog:generate": "scripts/use_node scripts/generate_changelog"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions scripts/generate_changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

require('../src/setup_node_env');
require('../src/dev/generate_changelog');
83 changes: 83 additions & 0 deletions src/dev/generate_changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { resolve } from 'path';
import { readFileSync, writeFileSync, readdirSync } from 'fs';
import { load as loadYaml } from 'js-yaml';
import { version as pkgVersion } from '../../package.json';

const CHANGELOG_HEADER = `# CHANGELOG

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

const SECTION_MAPPING = {
breaking: '💥 Breaking Changes',
deprecate: 'Deprecations',
feat: '📈 Features/Enhancements',
fix: '🐛 Bug Fixes',
infra: '🚞 Infrastructure',
doc: '📝 Documentation',
chore: '🛠 Maintenance',
refactor: '🪛 Refactoring',
test: '🔩 Tests',
};

const SECTION_KEYS = Object.keys(SECTION_MAPPING);
type SectionKey = keyof typeof SECTION_MAPPING;
type Changelog = Record<SectionKey, string[]>;

const sections: Partial<Changelog> = {};

const fragmentDirPath = resolve(__dirname, '..', '..', 'changelogs', 'fragments');
const fragmentPaths = readdirSync(fragmentDirPath).filter(
(path) => path.endsWith('.yml') || path.endsWith('.yaml')
);

for (const fragmentFilename of fragmentPaths) {
const fragmentPath = resolve(fragmentDirPath, fragmentFilename);
const fragmentContents = readFileSync(fragmentPath, { encoding: 'utf-8' });
const fragmentYaml = loadYaml(fragmentContents) as Changelog;

const prNumber = fragmentFilename.split('.').slice(0, -1).join('.');

for (const [sectionKey, entries] of Object.entries(fragmentYaml)) {
if (!SECTION_KEYS.includes(sectionKey)) {
// eslint-disable-next-line no-console
console.warn(`Unknown section ${sectionKey}. Ignoring`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF we encounter an unknown section I thin id like to be warned more explicitly. We dont want to miss something from the changelog because we missed a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it should blow up with errors and exit without actually modifying the changelog?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah it can modify, we have git histories to revet the change if necessary.

continue;
}

const section = sections[sectionKey as SectionKey] || (sections[sectionKey as SectionKey] = []);
section.push(
...entries.map(
(entry) =>
`${entry} ([#${prNumber}](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/${prNumber}))`
)
);
}
}

const changelogSections = [];

for (const [sectionKey, entries] of Object.entries(sections)) {
if (!entries) {
continue;
}

const sectionName = SECTION_MAPPING[sectionKey as SectionKey];

changelogSections.push(`### ${sectionName}

${entries.map((entry) => ` - ${entry}`).join('\n')}`);
}

const changelog = `${CHANGELOG_HEADER}

## [${pkgVersion}](https://github.com/opensearch-project/OpenSearch-Dashboards/releases/tag/${pkgVersion})

${changelogSections.join('\n\n')}
`;

writeFileSync(resolve(__dirname, '..', '..', 'CHANGELOG.md'), changelog);