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: repo option as string #128

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/commands/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
syncGithubRelease,
} from "../github";
import {
ChangelogConfig,
ResolvedChangelogConfig,
loadChangelogConfig,
parseChangelogMarkdown,
} from "..";
Expand Down Expand Up @@ -93,7 +93,7 @@ export default async function githubMain(args: Argv) {
}

export async function githubRelease(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
release: { version: string; body: string }
) {
if (!config.tokens.github) {
Expand Down
36 changes: 26 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { resolve } from "node:path";
import { loadConfig, setupDotenv } from "c12";
import { getLastGitTag, getCurrentGitRef } from "./git";
import { resolveRepoConfig, RepoProvider } from "./repo";
import { resolveRepoConfig, getRepoConfig } from "./repo";
import type { SemverBumpType } from "./semver";
import type { RepoConfig } from "./repo";
import type { RepoConfig, RepoProvider } from "./repo";

export interface ChangelogConfig {
cwd: string;
types: Record<string, { title: string; semver?: SemverBumpType }>;
scopeMap: Record<string, string>;
repo?: RepoConfig;
repo?: RepoConfig | string;
tokens: Partial<Record<RepoProvider, string>>;
from: string;
to: string;
Expand All @@ -27,6 +27,11 @@ export interface ChangelogConfig {
};
}

export type ResolvedChangelogConfig = Omit<ChangelogConfig, "repo"> & {
repo: RepoConfig;
};

const defaultOutput = "CHANGELOG.md";
const getDefaultConfig = () =>
<ChangelogConfig>{
types: {
Expand All @@ -46,7 +51,7 @@ const getDefaultConfig = () =>
cwd: null,
from: "",
to: "",
output: "CHANGELOG.md",
output: defaultOutput,
scopeMap: {},
tokens: {
github:
Expand All @@ -69,7 +74,7 @@ const getDefaultConfig = () =>
export async function loadChangelogConfig(
cwd: string,
overrides?: Partial<ChangelogConfig>
): Promise<ChangelogConfig> {
): Promise<ResolvedChangelogConfig> {
await setupDotenv({ cwd });
const defaults = getDefaultConfig();
const { config } = await loadConfig<ChangelogConfig>({
Expand All @@ -83,6 +88,13 @@ export async function loadChangelogConfig(
},
});

return await resolveChangelogConfig(config, cwd);
}

export async function resolveChangelogConfig(
config: ChangelogConfig,
cwd: string
) {
if (!config.from) {
config.from = await getLastGitTag();
}
Expand All @@ -91,16 +103,20 @@ export async function loadChangelogConfig(
config.to = await getCurrentGitRef();
}

if (!config.output) {
config.output = false;
} else if (config.output) {
if (config.output) {
config.output =
config.output === true ? defaults.output : resolve(cwd, config.output);
config.output === true ? defaultOutput : resolve(cwd, config.output);
} else {
config.output = false;
}

if (!config.repo) {
config.repo = await resolveRepoConfig(cwd);
}

return config;
if (typeof config.repo === "string") {
config.repo = getRepoConfig(config.repo);
}

return config as ResolvedChangelogConfig;
}
20 changes: 10 additions & 10 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, promises as fsp } from "node:fs";
import { homedir } from "node:os";
import { $fetch, FetchOptions } from "ofetch";
import { join } from "pathe";
import { ChangelogConfig } from "./config";
import { ResolvedChangelogConfig } from "./config";

export interface GithubOptions {
repo: string;
Expand All @@ -19,15 +19,15 @@ export interface GithubRelease {
}

export async function listGithubReleases(
config: ChangelogConfig
config: ResolvedChangelogConfig
): Promise<GithubRelease[]> {
return await githubFetch(config, `/repos/${config.repo.repo}/releases`, {
query: { per_page: 100 },
});
}

export async function getGithubReleaseByTag(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
tag: string
): Promise<GithubRelease> {
return await githubFetch(
Expand All @@ -37,15 +37,15 @@ export async function getGithubReleaseByTag(
);
}

export async function getGithubChangelog(config: ChangelogConfig) {
export async function getGithubChangelog(config: ResolvedChangelogConfig) {
return await githubFetch(
config,
`https://raw.githubusercontent.com/${config.repo.repo}/main/CHANGELOG.md`
);
}

export async function createGithubRelease(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
body: GithubRelease
) {
return await githubFetch(config, `/repos/${config.repo.repo}/releases`, {
Expand All @@ -55,7 +55,7 @@ export async function createGithubRelease(
}

export async function updateGithubRelease(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
id: string,
body: GithubRelease
) {
Expand All @@ -70,7 +70,7 @@ export async function updateGithubRelease(
}

export async function syncGithubRelease(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
release: { version: string; body: string }
) {
const currentGhRelease = await getGithubReleaseByTag(
Expand Down Expand Up @@ -109,15 +109,15 @@ export async function syncGithubRelease(
}

export function githubNewReleaseURL(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
release: { version: string; body: string }
) {
return `https://${config.repo.domain}/${config.repo.repo}/releases/new?tag=v${
release.version
}&title=v${release.version}&body=${encodeURIComponent(release.body)}`;
}

export async function resolveGithubToken(config: ChangelogConfig) {
export async function resolveGithubToken(config: ResolvedChangelogConfig) {
const env =
process.env.CHANGELOGEN_TOKENS_GITHUB ||
process.env.GITHUB_TOKEN ||
Expand All @@ -140,7 +140,7 @@ export async function resolveGithubToken(config: ChangelogConfig) {

// --- Internal utils ---
async function githubFetch(
config: ChangelogConfig,
config: ResolvedChangelogConfig,
url: string,
opts: FetchOptions = {}
) {
Expand Down
11 changes: 7 additions & 4 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { upperFirst } from "scule";
import { convert } from "convert-gitmoji";
import { fetch } from "node-fetch-native";
import type { ChangelogConfig } from "./config";
import type { ResolvedChangelogConfig } from "./config";
import type { GitCommit, Reference } from "./git";
import { formatReference, formatCompareChanges } from "./repo";

export async function generateMarkDown(
commits: GitCommit[],
config: ChangelogConfig
config: ResolvedChangelogConfig
) {
const typeGroups = groupBy(commits, "type");

Expand Down Expand Up @@ -126,7 +126,7 @@ export function parseChangelogMarkdown(contents: string) {

// --- Internal utils ---

function formatCommit(commit: GitCommit, config: ChangelogConfig) {
function formatCommit(commit: GitCommit, config: ResolvedChangelogConfig) {
return (
"- " +
(commit.scope ? `**${commit.scope.trim()}:** ` : "") +
Expand All @@ -136,7 +136,10 @@ function formatCommit(commit: GitCommit, config: ChangelogConfig) {
);
}

function formatReferences(references: Reference[], config: ChangelogConfig) {
function formatReferences(
references: Reference[],
config: ResolvedChangelogConfig
) {
const pr = references.filter((ref) => ref.type === "pull-request");
const issue = references.filter((ref) => ref.type === "issue");
if (pr.length > 0 || issue.length > 0) {
Expand Down
7 changes: 5 additions & 2 deletions src/repo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readPackageJSON } from "pkg-types";
import type { Reference } from "./git";
import type { ChangelogConfig } from "./config";
import type { ResolvedChangelogConfig } from "./config";
import { getGitRemoteURL } from "./git";

export type RepoProvider = "github" | "gitlab" | "bitbucket";
Expand Down Expand Up @@ -55,7 +55,10 @@ export function formatReference(ref: Reference, repo?: RepoConfig) {
}/${ref.value.replace(/^#/, "")})`;
}

export function formatCompareChanges(v: string, config: ChangelogConfig) {
export function formatCompareChanges(
v: string,
config: ResolvedChangelogConfig
) {
const part =
config.repo.provider === "bitbucket" ? "branches/compare" : "compare";
return `[compare changes](${baseUrl(config.repo)}/${part}/${config.from}...${
Expand Down