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 prettier #143

Merged
merged 5 commits into from
Jul 16, 2024
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 120,
"tabWidth": 2,
"endOfLine": "lf"
}
2 changes: 1 addition & 1 deletion .woodpecker/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ steps:
repo: woodpeckerci/plugin-ready-release-go
username: woodpeckerbot
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
tag: [latest, "${CI_COMMIT_TAG}"]
tag: [latest, '${CI_COMMIT_TAG}']
secrets: [docker_password]
when:
event: tag
Expand Down
6 changes: 6 additions & 0 deletions .woodpecker/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ steps:
- corepack enable
- pnpm typecheck

format:
image: node:20-slim
commands:
- corepack enable
- pnpm format:check

when:
- event: pull_request
- event: push
Expand Down
2 changes: 1 addition & 1 deletion docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Add a `release-config.ts` file to the root of your repository. Have a look at th
export default {
commentOnReleasedPullRequests: false,
};
````
```

The plugin also supports executing custom hooks which can e.g. help to perform additional actions during a release (e.g. updating a helm chart's `appVersion` field):

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"scripts": {
"start": "tsx src/run.ts",
"build": "tsx src/run.ts --out-dir dist",
"test": "vitest",
"test": "vitest run",
"test:watch": "vitest --ui --open=false",
"format:check": "prettier --check .",
"typecheck": "tsc --noEmit"
},
"dependencies": {
Expand All @@ -26,7 +27,8 @@
"@types/semver": "^7.5.0",
"@types/shelljs": "^0.8.11",
"@vitest/ui": "^1.0.0",
"vitest": "^1.0.0",
"typescript": "^5.3.2"
"prettier": "^3.3.3",
"typescript": "^5.3.2",
"vitest": "^1.0.0"
}
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UserConfig } from "./src/utils/types";
import type { UserConfig } from './src/utils/types';

export default {
skipCommitsWithoutPullRequest: false,
Expand Down
91 changes: 29 additions & 62 deletions src/cmd/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import c from "picocolors";
import c from 'picocolors';

import { CommandContext, HookContext } from "../utils/types";
import { updateChangelogSection, getChangeLogSection } from "../utils/change";
import { promises as fs } from "fs";
import { CommandContext, HookContext } from '../utils/types';
import { updateChangelogSection, getChangeLogSection } from '../utils/change';
import { promises as fs } from 'fs';

export async function prepare(cmdCtx: CommandContext) {
const {
Expand All @@ -18,11 +18,7 @@ export async function prepare(cmdCtx: CommandContext) {
useVersionPrefixV,
} = cmdCtx;

console.log(
"# Preparing release pull-request for version:",
c.green(nextVersion),
"..."
);
console.log('# Preparing release pull-request for version:', c.green(nextVersion), '...');

const hookCtx: HookContext = {
exec,
Expand All @@ -35,82 +31,58 @@ export async function prepare(cmdCtx: CommandContext) {

const branches = await git.branch();
if (branches.all.includes(`remotes/origin/${pullRequestBranch}`)) {
console.log(
c.yellow(`Branch "${pullRequestBranch}" already exists, checking it out.`)
);
console.log(c.yellow(`Branch "${pullRequestBranch}" already exists, checking it out.`));

await git.checkout([pullRequestBranch]);

try {
await git.pull(pullRequestBranch);
} catch (e) {
console.log(
c.yellow(
`Error pulling "${pullRequestBranch}" branch. Maybe it does not exist yet?`
),
e
);
console.log(c.yellow(`Error pulling "${pullRequestBranch}" branch. Maybe it does not exist yet?`), e);
}

await git.merge([
`origin/${releaseBranch}`,
"-m",
'-m',
`Merge branch 'origin/${releaseBranch}' into '${pullRequestBranch}'`,
"--no-edit",
'--no-edit',
]);
} else {
console.log(
c.yellow(`Branch "${pullRequestBranch}" does not exist yet, creating it.`)
);
console.log(c.yellow(`Branch "${pullRequestBranch}" does not exist yet, creating it.`));

await git.checkout(["-B", pullRequestBranch, "--track"]);
await git.checkout(['-B', pullRequestBranch, '--track']);
}

if (config.user.beforePrepare) {
console.log("# Running beforePrepare hook");
console.log('# Running beforePrepare hook');
const hookResult = await config.user.beforePrepare(hookCtx);
if (hookResult === false) {
console.log("# beforePrepare hook returned false, skipping prepare.");
console.log('# beforePrepare hook returned false, skipping prepare.');
return;
}
}

const tag =
useVersionPrefixV && !nextVersion.startsWith("v")
? `v${nextVersion}`
: nextVersion;
const tag = useVersionPrefixV && !nextVersion.startsWith('v') ? `v${nextVersion}` : nextVersion;

let oldChangelog = "";
if (await fs.stat("CHANGELOG.md").catch(() => false)) {
oldChangelog = await fs.readFile("CHANGELOG.md", "utf-8");
let oldChangelog = '';
if (await fs.stat('CHANGELOG.md').catch(() => false)) {
oldChangelog = await fs.readFile('CHANGELOG.md', 'utf-8');
}
const newChangelogSection = getChangeLogSection(
nextVersion,
tag,
config,
changes,
forge,
true
);
const changelog = updateChangelogSection(
latestVersion,
nextVersion,
oldChangelog,
newChangelogSection
);
const newChangelogSection = getChangeLogSection(nextVersion, tag, config, changes, forge, true);
const changelog = updateChangelogSection(latestVersion, nextVersion, oldChangelog, newChangelogSection);

console.log("# Updating CHANGELOG.md");
await fs.writeFile("CHANGELOG.md", changelog);
console.log('# Updating CHANGELOG.md');
await fs.writeFile('CHANGELOG.md', changelog);

const { isClean } = await git.status();
if (!isClean()) {
await git.add(".");
await git.add('.');
await git.commit(`${config.ci.releasePrefix} ${nextVersion}`);
await git.push(["-u", "origin", pullRequestBranch]);
await git.push(['-u', 'origin', pullRequestBranch]);
}

if (!config.ci.repoOwner || !config.ci.repoName) {
throw new Error("Missing repoOwner or repoName");
throw new Error('Missing repoOwner or repoName');
}

const releaseDescription = config.user.getReleaseDescription
Expand All @@ -122,12 +94,10 @@ export async function prepare(cmdCtx: CommandContext) {
`If you're not ready to do a release yet, that's fine, whenever you add ` +
`more changes to \`${releaseBranch}\` this pull-request will be updated.\n\n` +
`## Options\n\n` +
`- [${
shouldBeRC ? "x" : " "
}] Mark this version as a release candidate\n\n` +
`- [${shouldBeRC ? 'x' : ' '}] Mark this version as a release candidate\n\n` +
getChangeLogSection(nextVersion, tag, config, changes, forge, false);

console.log("# Creating release pull-request");
console.log('# Creating release pull-request');
const pullRequestLink = await forge.createOrUpdatePullRequest({
owner: config.ci.repoOwner,
repo: config.ci.repoName,
Expand All @@ -139,14 +109,11 @@ export async function prepare(cmdCtx: CommandContext) {
});

if (config.user.afterPrepare) {
console.log("# Running afterPrepare hook");
console.log('# Running afterPrepare hook');
await config.user.afterPrepare(hookCtx);
}

console.log(
"# Successfully prepared release pull-request: ",
pullRequestLink
);
console.log('# Successfully prepared release pull-request: ', pullRequestLink);

console.log("# Pull-request created");
console.log('# Pull-request created');
}
32 changes: 11 additions & 21 deletions src/cmd/release.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getChangeLogSection } from "../utils/change";
import { CommandContext, HookContext } from "../utils/types";
import c from "picocolors";
import { getChangeLogSection } from '../utils/change';
import { CommandContext, HookContext } from '../utils/types';
import c from 'picocolors';

export async function release({
config,
Expand All @@ -20,35 +20,25 @@ export async function release({
};

if (config.user.beforeRelease) {
console.log("# Running beforeRelease hook");
console.log('# Running beforeRelease hook');
if ((await config.user.beforeRelease(hookCtx)) === false) {
return;
}
}

if (!config.ci.repoOwner || !config.ci.repoName) {
throw new Error("Missing repoOwner or repoName");
throw new Error('Missing repoOwner or repoName');
}

const tag =
useVersionPrefixV && !nextVersion.startsWith("v")
? `v${nextVersion}`
: nextVersion;
const tag = useVersionPrefixV && !nextVersion.startsWith('v') ? `v${nextVersion}` : nextVersion;

const newChangelogSection = getChangeLogSection(
nextVersion,
tag,
config,
changes,
forge,
true
);
const newChangelogSection = getChangeLogSection(nextVersion, tag, config, changes, forge, true);

const releaseDescription = config.user.getReleaseDescription
? await config.user.getReleaseDescription(hookCtx)
: newChangelogSection;

console.log("# Creating release");
console.log('# Creating release');
const { releaseLink } = await forge.createRelease({
owner: config.ci.repoOwner,
repo: config.ci.repoName,
Expand All @@ -58,10 +48,10 @@ export async function release({
prerelease: shouldBeRC,
});

console.log(c.green("# Successfully created release:"), releaseLink);
console.log(c.green('# Successfully created release:'), releaseLink);

if (config.user.commentOnReleasedPullRequests) {
console.log("# Adding release comments to pull-requests");
console.log('# Adding release comments to pull-requests');
for await (const { pullRequestNumber } of changes) {
if (!pullRequestNumber) {
continue;
Expand All @@ -83,7 +73,7 @@ Thank you for your contribution. ❤️📦🚀`;
}

if (config.user.afterRelease) {
console.log("# Running afterRelease hook");
console.log('# Running afterRelease hook');
await config.user.afterRelease(hookCtx);
}
}
18 changes: 3 additions & 15 deletions src/forges/forge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,8 @@ export abstract class Forge {
}): Promise<PullRequest | undefined>;

abstract getRepoUrl(owner: string, repo: string): string;
abstract getCommitUrl(
owner: string,
repo: string,
commitHash: string
): string;
abstract getIssueUrl(
owner: string,
repo: string,
issueNumber: string | number
): string;
abstract getPullRequestUrl(
owner: string,
repo: string,
pullRequestNumber: string | number
): string;
abstract getCommitUrl(owner: string, repo: string, commitHash: string): string;
abstract getIssueUrl(owner: string, repo: string, issueNumber: string | number): string;
abstract getPullRequestUrl(owner: string, repo: string, pullRequestNumber: string | number): string;
abstract getReleaseUrl(owner: string, repo: string, release: string): string;
}
Loading