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

Feat/chanelog project #2

Closed
wants to merge 167 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
002e7be
Test commit
JohnathonBowers Nov 14, 2023
a074b34
test commit to project branch from <Riley>
MadaniKK Nov 14, 2023
7d2cc21
creating '/chanelogs/fragments' folder and adding change_set_template…
BigSamu Nov 17, 2023
8045014
creating '/chanelogs/fragments' folder and adding 'change_set_templat…
BigSamu Nov 17, 2023
7b249c9
updating 'change_set_template.yml' file
BigSamu Nov 17, 2023
0488038
updating change set .yml template file according what agreed last mee…
BigSamu Nov 21, 2023
18a6555
Add 'Changelog' section to PR template
JohnathonBowers Nov 22, 2023
c68bd29
adding dummy change set files for work in release notes script
BigSamu Nov 22, 2023
13bb607
adding dummy change set files for work in release notes script
BigSamu Nov 22, 2023
17c0795
creating 'create_change_set_worflow.yml' file - starting version
BigSamu Nov 23, 2023
da50046
commenting Build and Test worflow and correcting name for Create Chan…
BigSamu Nov 23, 2023
8e7dd07
commenting Build and Test worflow and correcting name for Create Chan…
BigSamu Nov 23, 2023
43f1df6
uncommenting Build and Test workflow
BigSamu Nov 23, 2023
bb34299
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
5207ba5
removing dummy changesets
BigSamu Nov 23, 2023
806e427
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
f9497ff
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
e30f4a8
updating pull_request_template.md for changelog entries
BigSamu Nov 23, 2023
8ddbfba
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Nov 23, 2023
5dc2433
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
d0209e9
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
560cff8
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
a9b64cd
removing old changeset with wrong format
BigSamu Nov 23, 2023
caeb6c9
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
c85d009
adding change sets dummy files 1.yml, 3.yml and 4.yml
BigSamu Nov 23, 2023
58cc106
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Nov 23, 2023
8f11b83
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
cab6b50
fix a line in changeset 2.yml file, add scripting for updating the ch…
MadaniKK Nov 23, 2023
932c8cb
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
c2b9ac1
Add changeset for PR #2
github-actions[bot] Nov 23, 2023
205f0a1
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
d5a37dc
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
0b25ce3
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
915ac64
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
a19b527
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
4efcc6d
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
245b559
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
6b74097
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
034b742
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
d82cfca
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
bdaba2f
Add changeset for PR #2
github-actions[bot] Nov 24, 2023
ec41ec0
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
e384081
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
621aa2f
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
f65914a
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
f951e7b
Bump actions/checkout from v2 to v4
JohnathonBowers Nov 25, 2023
e7f10ce
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
JohnathonBowers Nov 25, 2023
2c4c1de
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
5460b25
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
d43c874
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
78aabf6
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
8a1b35f
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
9cb4afb
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
e7fe1bb
Add changeset for PR #2
github-actions[bot] Nov 25, 2023
80eb6f2
release note script refactor + deletes fragment
CMDWillYang Nov 25, 2023
4bd6da8
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
CMDWillYang Nov 25, 2023
7cb63de
Add changeset for PR #2
github-actions[bot] Nov 27, 2023
34cfb78
Add changeset for PR #2
github-actions[bot] Nov 27, 2023
45072e8
Add changeset for PR #2
github-actions[bot] Nov 27, 2023
698398d
Add changeset for PR #2
github-actions[bot] Nov 28, 2023
9cc2a72
Add changeset for PR #2
github-actions[bot] Nov 28, 2023
c0b93e0
Add changeset for PR #2
github-actions[bot] Nov 28, 2023
f715f53
Add changeset for PR #2
github-actions[bot] Nov 28, 2023
5c37391
Update language in 'Changelog' section of PR template
JohnathonBowers Nov 29, 2023
ad76b43
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
3ece4eb
Merge branch 'main' into feat/chanelog_project
BigSamu Nov 29, 2023
04bdb2b
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
d5fe142
Update src/dev/generate_release_note.ts
MadaniKK Nov 29, 2023
894430e
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
70dbb55
Add first draft of changelog process README
JohnathonBowers Nov 29, 2023
5b9b939
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
JohnathonBowers Nov 29, 2023
1211af2
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
9b8a07f
Merge branch 'feat/chanelog_project' of github.com:BigSamu/OpenSearch…
MadaniKK Nov 29, 2023
ef3f61e
changed error handling behaviors.
CMDWillYang Nov 29, 2023
981ef3e
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
c1945a9
Add more details to changelog README
JohnathonBowers Nov 29, 2023
97c2fd6
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
JohnathonBowers Nov 29, 2023
c777b66
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
f65ba39
Merge branch 'feat/chanelog_project' of github.com:BigSamu/OpenSearch…
MadaniKK Nov 29, 2023
4775d6f
added error
MadaniKK Nov 29, 2023
81f1c59
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
89337de
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
7620ffa
Add changeset for PR #2
github-actions[bot] Nov 29, 2023
7a567c3
Add changeset for PR #2
github-actions[bot] Nov 30, 2023
31232db
Add changeset for PR #2
github-actions[bot] Nov 30, 2023
86f10bd
Add changeset for PR #2
github-actions[bot] Dec 3, 2023
c3843c6
Add changeset for PR #2
github-actions[bot] Dec 3, 2023
72c3d89
Add changeset for PR #2
github-actions[bot] Dec 5, 2023
204c363
Add changeset for PR #2
github-actions[bot] Dec 5, 2023
3b0b2f2
Add changeset for PR #2
github-actions[bot] Dec 5, 2023
a875300
Add changeset for PR #2
github-actions[bot] Dec 5, 2023
941dd6c
Add changeset for PR #2
github-actions[bot] Dec 6, 2023
efc3948
Merge branch 'main' into feat/chanelog_project
BigSamu Dec 6, 2023
9433869
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 6, 2023
b80f45f
Add changeset for PR #2
github-actions[bot] Dec 6, 2023
6795bc0
Abbreviate README and link to workflow README
JohnathonBowers Dec 6, 2023
7ba8a70
Add changeset for PR #2
github-actions[bot] Dec 6, 2023
da6ad8e
Add changeset for PR #2
github-actions[bot] Dec 9, 2023
6e9a401
deleting test.txt file used to test if other contributors of project …
BigSamu Dec 9, 2023
efdbf39
Add changeset for PR #2
github-actions[bot] Dec 9, 2023
941bf62
fixing formatting in create_change_set_workflow.yml file
BigSamu Dec 9, 2023
36e3fd5
updating title for README.md in changelogs directory
BigSamu Dec 9, 2023
f918897
updating description of README.md in changelogs directory
BigSamu Dec 9, 2023
afc82b3
removing demo fragments and adding new single one specifiying new fea…
BigSamu Dec 9, 2023
2e82bd2
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 9, 2023
3af0f22
Add changeset for PR #2
github-actions[bot] Dec 9, 2023
a1a3f20
Merge branch 'main' into feat/chanelog_project
BigSamu Dec 9, 2023
73ce52f
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 9, 2023
7d9145a
removing demo fragment 2.yml
BigSamu Dec 9, 2023
e907bc8
Add changeset for PR #2
github-actions[bot] Dec 9, 2023
79813cb
removed fragment thats too long. added helper file, used async functi…
CMDWillYang Dec 11, 2023
db0024e
Add changeset for PR #2
github-actions[bot] Dec 11, 2023
2090f1c
Add changeset for PR #2
github-actions[bot] Dec 12, 2023
b200102
Add changeset for PR #2
github-actions[bot] Dec 12, 2023
c6bcd39
Add changeset for PR #2
github-actions[bot] Dec 12, 2023
3bd3ad6
Merge branch 'main' into feat/chanelog_project
ananzh Dec 12, 2023
48e6b56
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
c24773b
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
616290f
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
2d2a425
adding 5519.yml fragment file and removing 2.yml dummy fragment file
BigSamu Dec 13, 2023
862b72b
updating changeset worflow removing syncronize option
BigSamu Dec 13, 2023
dc9d3c8
Merge branch 'opensearch-project:main' into feat/chanelog_project
BigSamu Dec 13, 2023
4f63874
deleting 5519.yml fragment file (now will be generated by workflow)
BigSamu Dec 13, 2023
aef2861
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
f359dfb
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
c258342
removing 2.yml fragment dummy file
BigSamu Dec 13, 2023
d117bd5
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
996b6ec
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
ced0e10
removing 2.yml fragment dummy file
BigSamu Dec 13, 2023
9387e34
refactored code (deleteFragments, readFragments returns sections and …
CMDWillYang Dec 13, 2023
4ec6548
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
0c4a141
removing 2.yml fragment dummy file
BigSamu Dec 13, 2023
d736d6b
moved deleteFragment to end of script
CMDWillYang Dec 13, 2023
69bb1c6
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
CMDWillYang Dec 13, 2023
bc988e7
removing changelog_verifier.yml
BigSamu Dec 13, 2023
925ee36
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 13, 2023
8d456ff
changed max_length to 100
CMDWillYang Dec 13, 2023
59b7095
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
CMDWillYang Dec 13, 2023
53e5445
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
44958fc
Add changeset for PR #2
github-actions[bot] Dec 13, 2023
53bf365
adding write permissions to for changeset workflow
BigSamu Dec 13, 2023
e4798bb
removing 2.yml fragment dummy file
BigSamu Dec 13, 2023
a7ed3c0
adding write permissions to for changeset workflow
BigSamu Dec 13, 2023
2b61175
Merge branch 'main' into feat/chanelog_project
joshuarrrr Dec 13, 2023
97eb4c9
adding write permissions to for changeset workflow
BigSamu Dec 13, 2023
bc888ec
refactored code/main function, now move fragment to temp folder.
CMDWillYang Dec 13, 2023
49faeef
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
CMDWillYang Dec 13, 2023
1b43e42
Add test.txt
JohnathonBowers Dec 13, 2023
8bb008b
Remove test.txt
JohnathonBowers Dec 13, 2023
0b56774
updating token name for workflow
BigSamu Dec 13, 2023
4f4a642
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 13, 2023
6285dca
update token name
BigSamu Dec 14, 2023
67200d2
Add changeset for PR #2
github-actions[bot] Dec 14, 2023
9f508f5
Add changeset for PR #2
github-actions[bot] Dec 14, 2023
58dbc69
Add changeset for PR #2
github-actions[bot] Dec 14, 2023
cbf5689
Add changeset for PR #2
github-actions[bot] Dec 14, 2023
affdf8d
Add changeset for PR #2
github-actions[bot] Dec 14, 2023
62739b7
updating changeset workflow by giving correct reference for checkout …
BigSamu Dec 14, 2023
b81044c
updating changeset workflow by removing reference for checkout action
BigSamu Dec 14, 2023
0095aea
Merge branch 'opensearch-project:main' into main
BigSamu Dec 14, 2023
bc26ea1
Merge branch 'main' into feat/chanelog_project
BigSamu Dec 14, 2023
83acd9a
Merge branch 'opensearch-project:main' into feat/chanelog_project
BigSamu Dec 14, 2023
51dc42f
Merge branch 'feat/chanelog_project' of https://github.com/BigSamu/Op…
BigSamu Dec 14, 2023
e8f4fbb
updating github action event from pull_request to pull_request_target
BigSamu Dec 14, 2023
8899405
Add changeset for PR #19
github-actions[bot] Dec 14, 2023
8abb3a8
deleting fragment 19.yml
BigSamu Dec 14, 2023
13609ce
adding CHANGESET_TOKEN and related logic
BigSamu Dec 14, 2023
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
14 changes: 14 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
the functionality of your change
-->

## Changelog
<!--
Add each of the changelog entries as a line item in this section. e.g.
- fix: Updates the graph
- feat: Adds a new feature

If this change does not need to added to the changelog, just add a single `skip` line e.g.
- skip

Valid prefixes: breaking, chore, deprecate, doc, feat, fix, infra, refactor, test

Descriptions following the prefixes must be 50 characters or less
-->

### Check List

- [ ] All tests pass
Expand Down
19 changes: 0 additions & 19 deletions .github/workflows/changelog_verifier.yml

This file was deleted.

21 changes: 21 additions & 0 deletions .github/workflows/create_change_set_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Create Change Set

on:
pull_request_target:
types: [opened, edited]
paths-ignore:
- 'changelogs/fragments/**/*'

jobs:
update-changelog:
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Update Changelog
uses: BigSamu/OpenSearch_Change_Set_Create_Action@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
changeset_token: ${{ secrets.CHANGESET_TOKEN }}
changeset_path: changelogs/fragments
5 changes: 5 additions & 0 deletions changelogs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog and Release Notes

For information regarding the changelog and release notes process, please consult the README in the GitHub Actions repository that this process utilizes. To view this README, follow the link below:

[GitHub Actions Workflow README](https://github.com/BigSamu/OpenSearch_Change_Set_Create_Action/blob/main/README.md)
5 changes: 5 additions & 0 deletions changelogs/fragments/2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
feat:
- Add new feature ([#2](https://github.com/BigSamu/OpenSearch-Dashboards/pull/2))

fix:
- Fix bug ([#2](https://github.com/BigSamu/OpenSearch-Dashboards/pull/2))
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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)\"",
"release_note:generate": "scripts/use_node scripts/generate_release_note"
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions scripts/generate_release_note.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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

import { resolve } from 'path';
import { readFileSync, writeFileSync, Dirent, renameSync, rm } from 'fs';
import { load as loadYaml } from 'js-yaml';
import { mkdir, readdir } from 'fs/promises';
import { version as pkgVersion } from '../../package.json';
import {
validateFragment,
getCurrentDateFormatted,
Changelog,
SECTION_MAPPING,
fragmentDirPath,
fragmentTempDirPath,
SectionKey,
releaseNotesDirPath,
filePath,
} from './generate_release_note_helper';

// Function to add content after the 'Unreleased' section in the changelog
function addContentAfterUnreleased(path: string, newContent: string): void {
let fileContent = readFileSync(path, 'utf8');
const targetString = '## [Unreleased]';
const targetIndex = fileContent.indexOf(targetString);

if (targetIndex !== -1) {
const endOfLineIndex = fileContent.indexOf('\n', targetIndex);

if (endOfLineIndex !== -1) {
fileContent =
fileContent.slice(0, endOfLineIndex + 1) +
'\n' +
newContent +
fileContent.slice(endOfLineIndex + 1);
} else {
throw new Error('End of line for "Unreleased" section not found.');
}
} else {
throw new Error("'## [Unreleased]' not found in the file.");
}

writeFileSync(path, fileContent);
}

async function deleteFragments() {
rm(fragmentTempDirPath, { recursive: true }, (err: any) => {
if (err) {
throw err;
}
});
}

// Read fragment files and populate sections
async function readFragments() {
// Initialize sections
const sections: Changelog = (Object.fromEntries(
Object.keys(SECTION_MAPPING).map((key) => [key, []])
) as unknown) as Changelog;

const fragmentPaths = await readdir(fragmentDirPath, { withFileTypes: true });
for (const fragmentFilename of fragmentPaths) {
// skip non yml or yaml files
if (!fragmentFilename.name.endsWith('.yml') && !fragmentFilename.name.endsWith('.yaml')) {
// eslint-disable-next-line no-console
console.warn(`Skipping non yml or yaml file ${fragmentFilename.name}`);
continue;
}

const fragmentPath = resolve(fragmentDirPath, fragmentFilename.name);
const fragmentContents = readFileSync(fragmentPath, { encoding: 'utf-8' });

validateFragment(fragmentContents);

const fragmentYaml = loadYaml(fragmentContents) as Changelog;

for (const [sectionKey, entries] of Object.entries(fragmentYaml)) {
sections[sectionKey as SectionKey].push(...entries);
}
}
return { sections, fragmentPaths };
}

async function moveFragments(fragmentPaths: Dirent[]): Promise<void> {
// create folder for temp fragments at fragmentTempDirPath
await mkdir(fragmentTempDirPath, { recursive: true });

// Move fragment files to temp fragments folder
for (const fragmentFilename of fragmentPaths) {
const fragmentPath = resolve(fragmentDirPath, fragmentFilename.name);
const fragmentTempPath = resolve(fragmentTempDirPath, fragmentFilename.name);
renameSync(fragmentPath, fragmentTempPath);
}
}

function generateChangelog(sections: Changelog) {
// Generate changelog sections
const changelogSections = Object.entries(sections).map(([sectionKey, entries]) => {
const sectionName = SECTION_MAPPING[sectionKey as SectionKey];
return entries.length === 0
? `### ${sectionName}`
: `### ${sectionName}\n\n${entries.map((entry) => ` - ${entry}`).join('\n')}`;
});

// Generate full changelog
const currentDate = getCurrentDateFormatted();
const changelog = `## [${pkgVersion}-${currentDate}](
${changelogSections.join('\n\n')}
`;
// Update changelog file
addContentAfterUnreleased(filePath, changelog);
return changelogSections;
}

function generateReleaseNote(changelogSections: string[]) {
// Generate release note
const releaseNoteFilename = `opensearch-dashboards.release-notes-${pkgVersion}.md`;
const releaseNoteHeader = `# VERSION ${pkgVersion} Release Note`;
const releaseNote = `${releaseNoteHeader}\n\n${changelogSections.join('\n\n')}`;
writeFileSync(resolve(releaseNotesDirPath, releaseNoteFilename), releaseNote);
}

(async () => {
const { sections, fragmentPaths } = await readFragments();

// move fragments to temp fragments folder
await moveFragments(fragmentPaths);

const changelogSections = generateChangelog(sections);

generateReleaseNote(changelogSections);

// remove temp fragments folder
await deleteFragments();
})();
68 changes: 68 additions & 0 deletions src/dev/generate_release_note_helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { resolve } from 'path';

export const filePath = resolve(__dirname, '..', '..', 'CHANGELOG.md');
export const fragmentDirPath = resolve(__dirname, '..', '..', 'changelogs', 'fragments');
export const fragmentTempDirPath = resolve(__dirname, '..', '..', 'changelogs', 'temp_fragments');
export const releaseNotesDirPath = resolve(__dirname, '..', '..', 'release-notes');

export function getCurrentDateFormatted(): string {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();

const formattedMonth = month.toString().padStart(2, '0');
const formattedDay = day.toString().padStart(2, '0');

return `${year}-${formattedMonth}-${formattedDay}`;
}

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

export type SectionKey = keyof typeof SECTION_MAPPING;
export type Changelog = Record<SectionKey, string[]>;

const MAX_ENTRY_LENGTH = 100;

// validate format of fragment files
export function validateFragment(content: string) {
const sections = content.split('\n\n');

// validate each section
for (const section of sections) {
const lines = section.split('\n');
const sectionName = lines[0];
const sectionKey = sectionName.slice(0, -1);

if (!SECTION_MAPPING[sectionKey as SectionKey] || !sectionName.endsWith(':')) {
throw new Error(`Unknown section ${sectionKey}.`);
}
// validate entries. each entry must start with '-' and a space. then followed by a string. string must be non-empty and less than 50 characters
const entryRegex = new RegExp(`^-.{1,${MAX_ENTRY_LENGTH}}\\(\\[#.+]\\(.+\\)\\)$`);
for (const entry of lines.slice(1)) {
if (entry === '') {
continue;
}
// if (!entryRegex.test(entry)) {
if (!entryRegex.test(entry.trim())) {
throw new Error(`Invalid entry ${entry} in section ${sectionKey}.`);
}
}
}
}