Skip to content

Commit

Permalink
feat: add support for --github-repo and --github-token
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 25, 2024
1 parent d2539e7 commit 7ec4f9d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Options:
Defaults to "true" when "org" is set, false otherwise
-o, --org <string> The NPM org scope that should be used WITHOUT "@" sign or trailing "/"
--preid [string] The "prerelease identifier" to use as a prefix for the "prerelease" part of a semver
--identifier-base <string> The base number (0 or 1) to be used for the prerelease identifier.
--identifier-base <number> The base number (0 or 1) to be used for the prerelease identifier.
--no-identifier-base Do not use a base number for the prerelease identifier.
-c, --commit-message-template [string] A custom commit message template to use.
Defaults to "chore({{name}}): release {{full-name}}@{{new-version}}"
Expand All @@ -120,6 +120,11 @@ Options:
default "true" when CI=true, "false" otherwise (default: false)
--no-skip-tag Whether to skip creating a git tag
default "true" when CI=true, "false" otherwise
--github-repo The GitHub repository to use for linking to issues and PRs in the changelog.
You can pass the unique string "auto" to automatically set this value as {{org}}/{{name}} as provided from --org and --name
This should be in the format "owner/repo"
You can use the "GITHUB_REPO" environment variable to automatically set this value
--github-token A token to authenticate requests to the GitHub API. This is required when using the "--github-repo" option. You can also set the "GITHUB_TOKEN" environment variable.
-v, --verbose Whether to print verbose information (default: false)
-h, --help display help for command
```
Expand All @@ -144,6 +149,8 @@ package). It should be named `.cliff-jumperrc`, optionally suffixed with
- `--install` map to `install`
- `--skip-changelog` and `--no-skip-changelog` map to `skipChangelog`
- `--skip-tag` and `--no-skip-tag` map to `skipTag`
- `--github-repo` maps to `githubRepo`
- `--github-token` maps to `githubToken`
- `--verbose` maps to `verbose`
When using `.cliff-jumperrc` or `.cliff-jumperrc.json` as your config file you
Expand Down Expand Up @@ -220,6 +227,8 @@ This library has opinionated defaults for its options. These are as follows:
the same value set in your config file
- `{{full-name}}` will be replaced with `{{name}}` (when `org` is not
provided), or `@{{org}}/{{name}}` (when `org` is provided).
- `--github-repo` will default to `undefined`.
- `--github-token` will default to `undefined`.
- `--verbose` will default to `false`.
### Merging of config file, defaults and CLI provided flags
Expand Down
8 changes: 8 additions & 0 deletions assets/cliff-jumper.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
"description": "A custom tag template to use.\n\nWhen \"org\" is provided this will default to \"@{{org}}/{{name}}@{{new-version}}\", for example \"@favware/cliff-jumper@1.0.0\"\nWhen \"org\" is not provided this will default to \"v{{new-version}}\", for example \"v1.0.0\"\n\nYou can use \"{{new-version}}\" in your template which will be dynamically replaced with whatever the new version is that will be published.\n\nYou can use \"{{org}}\" in your template, this will be replaced with the org provided through \"-o\", \"--org\" or the same value set in your config \nfile.\n\nYou can use \"{{name}}\" in your template, this will be replaced with the name provided through \"-n\", \"--name\" or the same value set in your config \nfile.\n\nYou can use \"{{full-name}}\" in your template, this will be replaced \"{{name}}\" (when \"org\" is not provided), or \"@{{org}}/{{name}}\" (when \"org\" is provided).",
"type": "string"
},
"githubRepo": {
"description": "The GitHub repository to use for linking to issues and PRs in the changelog.\n\nYou can pass the unique string \"auto\" to automatically set this value as \"{{org}}/{{name}}\" as provided from \"--org\" and \"--name\". This should be in the format \"owner/repo\". You can use the \"GITHUB_REPO\" environment variable to automatically set this value",
"type": "string"
},
"githubToken": {
"description": "A token to authenticate requests to the GitHub API. This is required when using the \"--github-repo\" option. You can also set the \"GITHUB_TOKEN\" environment variable.",
"type": "string"
},
"install": {
"description": "Whether to run npm install after bumping the version but before committing and creating a git tag. This is useful when you have a mono repo where bumping one package would then cause the lockfile to be out of date.",
"type": "boolean",
Expand Down
15 changes: 15 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { preflightChecks } from '#lib/preflight-checks';
import {
doActionAndLog,
getFullPackageName,
getGitHubRepo,
getGitHubToken,
getReleaseType,
resolveInstallCommand,
resolvePublishCommand,
Expand Down Expand Up @@ -44,6 +46,12 @@ const skipTagDescription = [
'Whether to skip creating a git tag', //
'default "true" when CI=true, "false" otherwise'
].join('\n');
const githubRepoDescription = [
'The GitHub repository to use for linking to issues and PRs in the changelog.', //
'You can pass the unique string "auto" to automatically set this value as {{org}}/{{name}} as provided from --org and --name',
'This should be in the format "owner/repo"',
'You can use the "GITHUB_REPO" environment variable to automatically set this value'
].join('\n');

const command = new Command()
.version(packageJson.version)
Expand Down Expand Up @@ -93,6 +101,11 @@ const command = new Command()
.option('--no-skip-changelog', skipChangelogDescription)
.option('-t, --skip-tag', skipTagDescription, isCi)
.option('--no-skip-tag', skipTagDescription)
.option('--github-repo', githubRepoDescription)
.option(
'--github-token',
'A token to authenticate requests to the GitHub API. This is required when using the "--github-repo" option. You can also set the "GITHUB_TOKEN" environment variable.'
)
.option('-v, --verbose', 'Whether to print verbose information', false);

const program = command.parse(process.argv);
Expand All @@ -115,6 +128,8 @@ logVerboseInfo(
`${indent}skip changelog: ${JSON.stringify(options.skipChangelog)}`,
`${indent}skip tag: ${JSON.stringify(options.skipTag)}`,
`${indent}verbose: ${JSON.stringify(options.verbose)}`,
`${indent}github repo: ${JSON.stringify(getGitHubRepo(options))}`,
`${indent}github token: ${getGitHubToken(options) ? 'Unset' : 'SECRET([REDACTED])'}`,
''
],
options.verbose
Expand Down
20 changes: 14 additions & 6 deletions src/commands/update-changelog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { doActionAndLog, getGitRootDirection, resolveTagTemplate } from '#lib/utils';
import { doActionAndLog, getGitHubRepo, getGitHubToken, getGitRootDirection, resolveTagTemplate } from '#lib/utils';
import { isNullishOrEmpty } from '@sapphire/utilities';
import type { Options } from 'commander';
import { execSync } from 'node:child_process';
Expand All @@ -10,11 +10,19 @@ export function updateChangelog(options: Options, newVersion: string) {

return doActionAndLog('Updating Changelog', () => {
if (!options.dryRun) {
execSync(
`npx git-cliff --tag ${options.tagTemplate} --prepend ./CHANGELOG.md -u -c ./cliff.toml ${
isNullishOrEmpty(repositoryRootDirectory) ? '' : `-r ${repositoryRootDirectory}/ --include-path "${options.packagePath}/*"`
}`
);
const monoRepoConfig = isNullishOrEmpty(repositoryRootDirectory)
? ''
: `-r ${repositoryRootDirectory}/ --include-path "${options.packagePath}/*"`;
let githubConfig = '';

const githubToken = getGitHubToken(options);
const githubRepo = getGitHubRepo(options);
if (!isNullishOrEmpty(githubRepo) && !isNullishOrEmpty(githubToken)) {
const resolvedGitHubRepo = githubRepo === 'auto' ? `${options.org}/${options.name}` : `${githubRepo}`;
githubConfig = `--github-repo ${resolvedGitHubRepo} --github-token ${githubToken}`;
}

execSync(`npx git-cliff --tag ${options.tagTemplate} --prepend ./CHANGELOG.md -u -c ./cliff.toml ${githubConfig} ${monoRepoConfig}`);
}
});
}
2 changes: 2 additions & 0 deletions src/lib/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ declare module 'commander' {
monoRepo: boolean;
commitMessageTemplate: string;
tagTemplate: string;
githubRepo: string;
githubToken: string;
}
}
16 changes: 15 additions & 1 deletion src/lib/preflight-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { packageCwd } from '#lib/constants';
import { createFile } from '#lib/createFile';
import { fileExists } from '#lib/fileExists';
import { logVerboseError } from '#lib/logger';
import { doActionAndLog, readJson } from '#lib/utils';
import { doActionAndLog, getGitHubRepo, getGitHubToken, readJson } from '#lib/utils';
import { isNullishOrEmpty } from '@sapphire/utilities';
import type { Options } from 'commander';
import { join } from 'node:path';
Expand All @@ -24,6 +24,20 @@ export async function preflightChecks(options: Options) {
});
}

const githubRepo = getGitHubRepo(options);
if (!isNullishOrEmpty(githubRepo)) {
const githubToken = getGitHubToken(options);

if (isNullishOrEmpty(githubToken)) {
logVerboseError({
text: [`GitHub Repository was configured as ${githubRepo} but no token was provided`],
verboseText: ['You can provide the token either through the "--github-token" option or the "GITHUB_TOKEN" environment variable'],
exitAfterLog: true,
verbose: options.verbose
});
}
}

const packageJsonPath = join(packageCwd, 'package.json');

const packageJsonExistsInCwd = await doActionAndLog(
Expand Down
33 changes: 31 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,34 @@ export function resolveTagTemplate(options: Options, newVersion: string) {
}

/** Resolves the release-as prefix */
export const getReleaseType = (options: Options, changelogResolvedReleaseType: Recommendation.ReleaseType): ReleaseType =>
((Boolean(options.preid) ? 'pre' : '') + changelogResolvedReleaseType) as ReleaseType;
export function getReleaseType(options: Options, changelogResolvedReleaseType: Recommendation.ReleaseType): ReleaseType {
return ((Boolean(options.preid) ? 'pre' : '') + changelogResolvedReleaseType) as ReleaseType;
}

/**
* Retrieves the GitHub repo, either from the environment variables or from the config
*
* The order of precedence is:
* 1. Environment variable `GITHUB_REPO`
* 2. The `githubRepo` property in the options object
*
* @param options The options object
* @returns The GitHub repo or `undefined` if it was not found
*/
export function getGitHubRepo(options: Options): string | undefined {
return process.env.GITHUB_REPO ?? (options.githubRepo === 'auto' ? `${options.org}/${options.name}` : options.githubRepo) ?? undefined;
}

/**
* Retrieves the GitHub token, either from the environment variables or from the config
*
* The order of precedence is:
* 1. Environment variable `GITHUB_TOKEN`
* 2. The `githubToken` property in the options object
*
* @param options The options object
* @returns The GitHub token or `undefined` if it was not found
*/
export function getGitHubToken(options: Options): string | undefined {
return process.env.GITHUB_TOKEN ?? options.githubToken ?? undefined;
}

0 comments on commit 7ec4f9d

Please sign in to comment.