Skip to content

Commit

Permalink
Add local_changelog_file_path option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjitse-E committed Feb 19, 2024
1 parent 628eeb8 commit 4d3bb05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ name: 'github-add-changelog-action'
description: 'Extract changelog entries and add to CHANGELOG.md'
author: 'Tjitse-E'
runs:
using: 'node16'
using: 'node20'
main: 'dist/index.js'
inputs:
local_changelog_file_path:
description: >
The path to the local changelog file, we'll use this path instead of the remote file if it's set.
required: false
token:
description: Your Github token
required: true
Expand Down
15 changes: 12 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15260,21 +15260,22 @@ const crypto_1 = __nccwpck_require__(6113);
const git_1 = __nccwpck_require__(7732);
const changelog_entries_1 = __nccwpck_require__(2677);
async function run() {
var _a, _b;
var _a, _b, _c;
try {
core.debug('Starting updating CHANGELOG.md');
const token = process.env.GITHUB_TOKEN || core.getInput('token');
const commitMessage = (_a = core.getInput('commit_message')) !== null && _a !== void 0 ? _a : 'Update CHANGELOG.md';
const committerUsername = core.getInput('committer_username');
const committerEmail = core.getInput('committer_email');
const repoUrl = github.context.payload.repository.html_url;
const localChangelogPath = (_b = core.getInput('local_changelog_path')) !== null && _b !== void 0 ? _b : null;
core.info(`Starting updating CHANGELOG.md for ${repoUrl}`);
if (token === '' || typeof token === 'undefined') {
throw new Error('Input token is missing or empty.');
}
let body = core.getInput('body');
if (body.length === 0) {
const pull_request = (_b = github.context.payload.pull_request) !== null && _b !== void 0 ? _b : github.context.payload.event.pull_request;
const pull_request = (_c = github.context.payload.pull_request) !== null && _c !== void 0 ? _c : github.context.payload.event.pull_request;
body = pull_request.body;
}
// Remove for double quotes at the start or end of body
Expand Down Expand Up @@ -15303,8 +15304,16 @@ async function run() {
const git = (0, simple_git_1.simpleGit)(options);
// Clone repo and check changelog file
await (0, git_1.clone)(token, repoUrl, dir, git);
await checkIfChangelogExists(dir);
const changelogPath = `${dir}/CHANGELOG.md`;
// Replace cloned changelog with the local one
if (localChangelogPath) {
if ((0, fs_1.existsSync)(localChangelogPath) === false) {
throw new Error(`Local changelog file does not exist: ${localChangelogPath}`);
}
core.info(`Replacing cloned changelog with local one: ${localChangelogPath}`);
(0, fs_1.copyFileSync)(localChangelogPath, changelogPath);
}
await checkIfChangelogExists(dir);
const changelogContent = (0, fs_1.readFileSync)(changelogPath, { encoding: 'utf8', flag: 'r' });
const changelog = (0, keep_a_changelog_1.parser)(changelogContent);
const unreleased = getUnReleasedSection(changelog);
Expand Down
15 changes: 13 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import {simpleGit, SimpleGit, SimpleGitOptions} from "simple-git";
import path from "path";
import {existsSync, mkdirSync, readFileSync, rmSync, writeFileSync} from "fs";
import {existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, copyFileSync} from "fs";
import {Changelog, parser, Release} from "keep-a-changelog";
import {randomBytes} from "crypto";
import {clone, push, isChangelogChanged} from './git'
Expand All @@ -16,6 +16,7 @@ export default async function run(): Promise<void> {
const committerUsername = core.getInput('committer_username');
const committerEmail = core.getInput('committer_email');
const repoUrl = github.context.payload.repository.html_url
const localChangelogPath = core.getInput('local_changelog_path') ?? null

core.info(`Starting updating CHANGELOG.md for ${repoUrl}`)

Expand Down Expand Up @@ -61,9 +62,19 @@ export default async function run(): Promise<void> {

// Clone repo and check changelog file
await clone(token, repoUrl, dir, git);
const changelogPath = `${dir}/CHANGELOG.md`

// Replace cloned changelog with the local one
if (localChangelogPath) {
if (existsSync(localChangelogPath) === false) {
throw new Error(`Local changelog file does not exist: ${localChangelogPath}`)
}
core.info(`Replacing cloned changelog with local one: ${localChangelogPath}`)
copyFileSync(localChangelogPath, changelogPath)
}

await checkIfChangelogExists(dir)

const changelogPath = `${dir}/CHANGELOG.md`
const changelogContent = readFileSync(changelogPath, {encoding: 'utf8', flag: 'r'});
const changelog: Changelog = parser(changelogContent);
const unreleased = getUnReleasedSection(changelog);
Expand Down

0 comments on commit 4d3bb05

Please sign in to comment.