Skip to content

Commit

Permalink
🚧Add a new prep release setting (#51)
Browse files Browse the repository at this point in the history
* Start work for issue #50

* feat: add setting to trim the start of version used for prep releases

* chore: add code docs

* refactor: remove unused async await keywords

* config: create deno task for linting

* refactor: change code to meet coding standards

* ci: improve format and lint status check workflows
  • Loading branch information
CalvinWilkinson authored Oct 2, 2024
1 parent b2f5faa commit 37d6140
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format-status-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
deno-version: ${{ vars.DENO_VERSION }}

- name: Run Lint
run: deno fmt --check
run: deno fmt --check **/*/*.ts;
2 changes: 1 addition & 1 deletion .github/workflows/lint-status-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
deno-version: ${{ vars.DENO_VERSION }}

- name: Run Lint
run: deno lint
run: deno lint **/*/*.ts;
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "v1.0.0-preview.5",
"tasks": {
"check": "deno check **/*/*.ts",
"lint": "deno lint **/*/*.ts",
"tests": "deno test ./Tests/*Tests.ts",
"reload-cache": "deno cache --reload --lock=deno.lock \"*/**/*.ts\""
},
Expand Down
10 changes: 5 additions & 5 deletions installation/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (existsSync(`./${createPrShellFileName}`, { isFile: true })) {
});

if (confirmCreatePRShellScript) {
Deno.writeTextFileSync(`./${createPrShellFileName}`, "& \"dev-tools/bin/kd-admin\" create-pr;\n");
Deno.writeTextFileSync(`./${createPrShellFileName}`, '& "dev-tools/bin/kd-admin" create-pr;\n');
console.log(`%c\tCreated '${createPrShellFileName}' file.`, "color: gray");
}
}
Expand All @@ -51,7 +51,7 @@ if (existsSync(`./${prepareReleaseShellFileName}`, { isFile: true })) {
});

if (confirmCreatePrepareReleaseShellScript) {
Deno.writeTextFileSync(`./${prepareReleaseShellFileName}`, "& \"dev-tools/bin/kd-admin\" prepare-for-release;\n");
Deno.writeTextFileSync(`./${prepareReleaseShellFileName}`, '& "dev-tools/bin/kd-admin" prepare-for-release;\n');
console.log(`%c\tCreated '${prepareReleaseShellFileName}' file.`, "color: gray");
}
}
Expand All @@ -69,12 +69,12 @@ if (existsSync(`./dev-tools/${createPrSettingsFileName}`, { isFile: true })) {
ownerName: "",
repoName: "",
githubTokenEnvVarName: "",
baseBranches: []
baseBranches: [],
};

const settingsObj = {
$schema: "https://raw.githubusercontent.com/KinsonDigital/kd-admin/preview/schemas/create-pr-schema.json",
...prSettings
...prSettings,
};

Deno.writeTextFileSync(`./dev-tools/${createPrSettingsFileName}`, `${JSON.stringify(settingsObj, null, 4)}\n`);
Expand All @@ -89,7 +89,7 @@ if (existsSync(`./dev-tools/${prepareReleaseSettingsFileName}`, { isFile: true }
message: "Do you want to add a the prepare for release setting files?",
default: true,
});

if (confirmCreatePrepareReleaseSettingFiles) {
const prepareReleaseSettings: PrepareReleaseSettings = {
ownerName: "",
Expand Down
15 changes: 7 additions & 8 deletions installation/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { walkSync } from "https://deno.land/std@0.224.0/fs/walk.ts";
import { TagClient } from "https://deno.land/x/kd_clients@v1.0.0-preview.13/GitHubClients/TagClient.ts";

const baseDirPath = "./dev-tools/bin";
const filesToUpdate = [...walkSync(baseDirPath, { includeFiles: true, match: [/kd-admin/]})].map((f) => f.path);
const filesToUpdate = [...walkSync(baseDirPath, { includeFiles: true, match: [/kd-admin/] })].map((f) => f.path);

const tagClient = new TagClient("KinsonDigital", "kd-admin");
const tags: string[] = (await tagClient.getAllTags()).map((t) => t.name);
const latestVersion = tags[0];

filesToUpdate.forEach(async file => {
await updateFile(file);
filesToUpdate.forEach((file) => {
updateFile(file);
});

console.log(`%ckd-admin has been updated to version '${latestVersion}'.`, "color: green");

async function updateFile (filePath: string) {
function updateFile(filePath: string) {
const isInstalled = existsSync(filePath, { isFile: true });

if (!isInstalled) {
Expand All @@ -28,7 +28,7 @@ async function updateFile (filePath: string) {

const urlLine = fileLines.find((line) => line.includes("deno") && line.includes("https://"));

const corruptErrorMsg = `%cThe file '${filePath}' is corrupted. Please reinstall the script.`;
const corruptErrorMsg = `%cThe file '${filePath}' is corrupted. Please reinstall the script.`;

if (urlLine === undefined) {
console.log(corruptErrorMsg, "color: red");
Expand Down Expand Up @@ -70,10 +70,9 @@ async function updateFile (filePath: string) {
return section;
});


const newUrlLine = newSections.join(" ");

const newFileLines = fileLines.map((line) => {
const newFileLines = fileLines.map((line) => {
if (line.includes("deno") && line.includes("https://")) {
return newUrlLine;
}
Expand All @@ -84,4 +83,4 @@ async function updateFile (filePath: string) {
const newFileContent = newFileLines.join("\n");

Deno.writeTextFileSync(filePath, newFileContent);
};
}
4 changes: 4 additions & 0 deletions schemas/prepare-release-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"releaseNotesFilePrefix": {
"type": "string",
"description": "The path to the version number in the JSON file. Only used if the file is a JSON file. (Optional)"
},
"trimFromStartOfVersion": {
"type": "array",
"description": "The list of strings to trim from the start of the version number. (Optional)"
}
},
"required": ["ownerName", "repoName", "releaseTypes", "githubTokenEnvVarName"]
Expand Down
5 changes: 5 additions & 0 deletions src/prepare-release-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export interface PrepareReleaseSettings {
* Gets the value to prefix the release notes file name with.
*/
releaseNotesFilePrefix?: string;

/**
* Gets the list of strings to trim from the start of the version.
*/
trimFromStartOfVersion?: string[];
}
36 changes: 30 additions & 6 deletions src/release-prepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class ReleasePrepper {
Deno.exit(1);
}

const chosenVersion = await this.getAndValidateVersion();
const chosenVersion = await this.getAndValidateVersion(settings);

const repoDoesNotExist = !(await this.repoClient.exists());

Expand Down Expand Up @@ -199,12 +199,13 @@ export class ReleasePrepper {
}

// Assignee milestone to the pr
const milestone = await this.milestoneClient.getMilestoneByName(chosenVersion);
const milestoneTitle = chosenVersion.startsWith("v") ? chosenVersion : `v${chosenVersion}`;
const milestone = await this.milestoneClient.getMilestoneByName(milestoneTitle);
const prData: IssueOrPRRequestData = {
milestone: milestone.number,
};

ConsoleLogColor.gray(` ⏳Assigning pr to milestone '${chosenVersion}'.`);
ConsoleLogColor.gray(` ⏳Assigning pr to milestone '${milestoneTitle}'.`);
await this.prClient.updatePullRequest(prNumber, prData);

const prUrl = `https://github.com/${ownerName}/${repoName}/pull/${prNumber}`;
Expand Down Expand Up @@ -409,7 +410,12 @@ export class ReleasePrepper {
return true;
}

private async getAndValidateVersion(): Promise<string> {
/**
* Gets the version from the user and trims and validates the version.
* @param settings The prepare release settings.
* @returns The validated and trimmed version.
*/
private async getAndValidateVersion(settings: PrepareReleaseSettings): Promise<string> {
let chosenVersion = "";

let versionExists = true;
Expand Down Expand Up @@ -440,9 +446,24 @@ export class ReleasePrepper {
}
}

// Trim the starting section of the version if trim values exist
if (!Guards.isNothing(settings.trimFromStartOfVersion)) {
for (let i = 0; i < settings.trimFromStartOfVersion.length; i++) {
const trimValue = settings.trimFromStartOfVersion[i];

if (chosenVersion.startsWith(trimValue)) {
chosenVersion = chosenVersion.slice(trimValue.length);
}
}
}

return chosenVersion;
}

/**
* Creates a release branch based on the given {@link releaseType}.
* @param releaseType The type of release.
*/
private async createReleaseBranch(releaseType: ReleaseType): Promise<void> {
const createBranchResult = await runAsync("git", ["checkout", "-B", releaseType.headBranch]);

Expand Down Expand Up @@ -472,6 +493,7 @@ export class ReleasePrepper {
let notesDirPath = releaseType.releaseNotesDirPath.trim().replace(/\\/g, "/");

notesDirPath = notesDirPath.endsWith("/") ? notesDirPath.slice(0, -1) : notesDirPath;
chosenVersion = chosenVersion.startsWith("v") ? chosenVersion : `v${chosenVersion}`;

const settings = this.loadGenNotesSettings(releaseType, chosenVersion, tokenEnvVarName);

Expand Down Expand Up @@ -742,9 +764,11 @@ export class ReleasePrepper {
}

private async milestoneExists(chosenVersion: string): Promise<boolean> {
ConsoleLogColor.gray(` ⏳Validating milestone '${chosenVersion}'.`);
const title = chosenVersion.startsWith("v") ? chosenVersion : `v${chosenVersion}`;

ConsoleLogColor.gray(` ⏳Validating milestone '${title}'.`);

return await this.milestoneClient.milestoneExists(chosenVersion);
return await this.milestoneClient.milestoneExists(title);
}

/**
Expand Down

0 comments on commit 37d6140

Please sign in to comment.