From f280b38fddb5f98bbebd86d4ee97f79bd449a53d Mon Sep 17 00:00:00 2001 From: Igwe Kalu Date: Wed, 28 Feb 2024 11:40:51 +0100 Subject: [PATCH] igwejk/improve logs (#140) * Improve consistency of logging * Setup facilitation for development and debugging * Improve logging in worker.ts --- .vscode/launch.json | 18 ++++++++++++++++++ package.json | 3 ++- src/utils/clients/auth.ts | 5 +++-- src/utils/getOrganisationsInEnterprise.ts | 4 ++-- src/utils/worker.ts | 22 +++++++++++++++------- tsconfig.json | 5 +++++ 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e6851ac --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "runtimeExecutable": "/Users/igwejk/.nvm/versions/node/v20.11.0/bin/node", + "type": "node", + "request": "launch", + "name": "Debug", + "skipFiles": ["/**"], + "program": "${workspaceFolder}/src/enable.ts", + "preLaunchTask": "tsc: build - tsconfig.json", + "outFiles": ["${workspaceFolder}/lib/**/*.js"] + } + ] +} diff --git a/package.json b/package.json index a97e23f..061c811 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "getRepos": "node ./lib/getRepos.js", "getOrgs": "node ./lib/getOrgs.js", "enableOrg": "node ./lib/enableOrg.js", - "start": "npm run build && node ./lib/enable.js" + "start": "npm run build && node ./lib/enable.js", + "start:debug": "npx ts-node ./src/enable.ts" }, "keywords": [ "GitHub", diff --git a/src/utils/clients/auth.ts b/src/utils/clients/auth.ts index 9061bb7..8f50033 100644 --- a/src/utils/clients/auth.ts +++ b/src/utils/clients/auth.ts @@ -1,4 +1,5 @@ import { createAppAuth, StrategyOptions } from "@octokit/auth-app"; +import { error, inform } from "../globals"; import { env } from "process"; @@ -24,10 +25,10 @@ export const auth = async (): Promise => { const auth = createAppAuth(options); try { const data = await auth({ type: "installation" }); - console.log(data); + inform(data); return data.token; } catch (err: any) { - console.error("Error within function (githubAuth)", err.message); + error("\n", "Error within function (githubAuth)", err.message); throw new Error( "We failed to generate a token from the credentials provided on the GitHub App. Please re-check the credentails provided.", ); diff --git a/src/utils/getOrganisationsInEnterprise.ts b/src/utils/getOrganisationsInEnterprise.ts index 1ad274f..105b22f 100644 --- a/src/utils/getOrganisationsInEnterprise.ts +++ b/src/utils/getOrganisationsInEnterprise.ts @@ -32,7 +32,7 @@ const performOrganisationsQuery = async ( })) as GraphQlQueryResponseData; return [hasNextPage, endCursor, nodes]; } catch (err) { - console.error(err); + error("\n", err); throw err; } }; @@ -90,7 +90,7 @@ export const index = async (client: Octokit): Promise => { const data = await getOrganisationsInEnterprise(client, slug, query); await createFile(data, orgsFileLocation); } catch (err) { - console.error(err); + error("\n", err); throw err; } }; diff --git a/src/utils/worker.ts b/src/utils/worker.ts index 2ea7a85..4746fd9 100644 --- a/src/utils/worker.ts +++ b/src/utils/worker.ts @@ -5,15 +5,16 @@ import { Octokit } from "@octokit/core"; import { enableFeaturesForRepository } from "./enableFeaturesForRepository"; import { client as octokit, auth as generateAuth } from "./clients"; -import { inform, reposFileLocation } from "./globals.js"; -import { reposFile } from "../../types/common/index.js"; +import { error, inform, reposFileLocation } from "./globals"; +import { reposFile } from "../../types/common"; -export const worker = async (): Promise => { - let res; +export const worker = async (): Promise => { let orgIndex: number; let repoIndex: number; let repos: reposFile; let file: string; + let repoCount: number = 0; + let errorCount: number = 0; const client = (await octokit()) as Octokit; @@ -27,7 +28,7 @@ export const worker = async (): Promise => { } repos = JSON.parse(file); } catch (err) { - console.error(err); + error("\n", err); throw new Error( "We did not find your repos.json file, please run `yarn run getRepos` to collect the repos to run this script on.", ); @@ -47,15 +48,22 @@ export const worker = async (): Promise => { ); try { + ++repoCount; await enableFeaturesForRepository({ repository: repos[orgIndex].repos[repoIndex], client, generateAuth, }); } catch (err) { - // boo + ++errorCount; + error("\n", err); } } } - return res; + + if (errorCount > 0) { + throw new Error( + `\nProcessed a total of ${repoCount} repositories, encountered errors for ${errorCount} out of the total ${repoCount} repositories.`, + ); + } }; diff --git a/tsconfig.json b/tsconfig.json index 69dee4b..d33b62e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,11 +4,16 @@ "exclude": ["lib", "node_modules"], "compilerOptions": { "typeRoots": ["./types", "node_modules/@types"], + "strict": true, "outDir": "./lib", + "rootDir": "./src", + "target": "ES2017", + "sourceMap": true, "noUnusedLocals": true, "noUnusedParameters": true, "removeComments": true, "preserveConstEnums": true, "resolveJsonModule": true, + "esModuleInterop": true, }, }