diff --git a/tools/tasks/changelog/createChangelog.ts b/tools/tasks/changelog/createChangelog.ts index 29ef9e4..22fc768 100644 --- a/tools/tasks/changelog/createChangelog.ts +++ b/tools/tasks/changelog/createChangelog.ts @@ -9,7 +9,7 @@ import { parsers } from "./definitions"; import parse from "./parser"; import { specialParserSetup } from "./specialParser"; import generateModChanges from "./generateModChanges"; -import pushAll, { pushChangelog, pushSeperator, pushTitle } from "./pusher"; +import pushAll, { pushChangelog, pushSeperator, pushSetup, pushTitle } from "./pusher"; import log from "fancy-log"; /** @@ -19,6 +19,7 @@ async function createChangelog(): Promise { const data: ChangelogData = new ChangelogData(); await data.init(); + await pushSetup(); // Handle Iterations if (data.shouldIterate()) { diff --git a/tools/tasks/changelog/pusher.ts b/tools/tasks/changelog/pusher.ts index 3013b52..0af5b42 100644 --- a/tools/tasks/changelog/pusher.ts +++ b/tools/tasks/changelog/pusher.ts @@ -3,17 +3,25 @@ import { categories, defaultIndentation } from "./definitions"; import { Category, ChangelogMessage, Commit } from "../../types/changelogTypes"; import { repoLink } from "./definitions"; import { Octokit } from "@octokit/rest"; -import { getIssueURL, getNewestIssueURLs, isEnvVariableSet } from "../../util/util"; -import log from "fancy-log"; +import { getIssueURL, getNewestIssueURLs } from "../../util/util"; let data: ChangelogData; -let octokit: Octokit | undefined; +let octokit: Octokit; export default async function pushAll(inputData: ChangelogData): Promise { pushTitle(inputData); await pushChangelog(inputData); } +export async function pushSetup(): Promise { + octokit = new Octokit({ + auth: process.env.GITHUB_TOKEN, + }); + + // Save Issue/PR Info to Cache + await getNewestIssueURLs(octokit); +} + export function pushTitle(inputData: ChangelogData): void { data = inputData; @@ -42,16 +50,6 @@ export function pushTitle(inputData: ChangelogData): void { export async function pushChangelog(inputData: ChangelogData): Promise { data = inputData; - if (isEnvVariableSet("GITHUB_TOKEN")) { - octokit = new Octokit({ - auth: process.env.GITHUB_TOKEN, - }); - } - - // Save Issue/PR Info to Cache - if (octokit) await getNewestIssueURLs(octokit); - else log("Skipping Transforming Issue/PR URLs! 'GITHUB_TOKEN' Not Set!"); - data.builder.push(`# Changes Since ${data.since}`, ""); // Push Sections of Changelog @@ -123,16 +121,7 @@ async function pushCategory(category: Category) { hasValues = true; } } - const promises: Promise[] = []; - for (let i = 0; i < categoryLog.length; i++) { - const categoryFormatted = categoryLog[i]; - // Transform PR and/or Issue tags into a link. - promises.push( - transformTags(categoryFormatted).then((categoryTransformed) => (categoryLog[i] = categoryTransformed)), - ); - } - // Apply all Link Changes - await Promise.all(promises); + await transformAllIssueURLs(categoryLog); if (hasValues) { // Push Title data.builder.push(`## ${category.categoryName}:`); @@ -251,12 +240,25 @@ function formatCommit(commit: Commit): string { return `* [\`${shortSHA}\`](${repoLink}commit/${commit.hash}): ${formattedCommit}`; } +/** + * Transforms PR/Issue Tags in all strings of the generated changelog. + * @param changelog The list to transform all PR/Issue Tags of. + */ +async function transformAllIssueURLs(changelog: string[]) { + const promises: Promise[] = []; + for (let i = 0; i < changelog.length; i++) { + const categoryFormatted = changelog[i]; + // Transform PR and/or Issue tags into a link. + promises.push(transformTags(categoryFormatted).then((categoryTransformed) => (changelog[i] = categoryTransformed))); + } + // Apply all Link Changes + await Promise.all(promises); +} + /** * Transforms PR/Issue Tags into Links. */ async function transformTags(message: string): Promise { - if (!octokit) return message; - const promises: Promise[] = []; if (message.search(/#\d+/) !== -1) { const matched = message.match(/#\d+/g);