Skip to content

Commit

Permalink
igwejk/improve logs (#140)
Browse files Browse the repository at this point in the history
* Improve consistency of logging

* Setup facilitation for development and debugging

* Improve logging in worker.ts
  • Loading branch information
igwejk authored Feb 28, 2024
1 parent 4452422 commit f280b38
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": ["<node_internals>/**"],
"program": "${workspaceFolder}/src/enable.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/lib/**/*.js"]
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/utils/clients/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAppAuth, StrategyOptions } from "@octokit/auth-app";
import { error, inform } from "../globals";

import { env } from "process";

Expand All @@ -24,10 +25,10 @@ export const auth = async (): Promise<string | Error> => {
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.",
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/getOrganisationsInEnterprise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const performOrganisationsQuery = async (
})) as GraphQlQueryResponseData;
return [hasNextPage, endCursor, nodes];
} catch (err) {
console.error(err);
error("\n", err);
throw err;
}
};
Expand Down Expand Up @@ -90,7 +90,7 @@ export const index = async (client: Octokit): Promise<void> => {
const data = await getOrganisationsInEnterprise(client, slug, query);
await createFile(data, orgsFileLocation);
} catch (err) {
console.error(err);
error("\n", err);
throw err;
}
};
22 changes: 15 additions & 7 deletions src/utils/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> => {
let res;
export const worker = async (): Promise<undefined> => {
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;

Expand All @@ -27,7 +28,7 @@ export const worker = async (): Promise<unknown> => {
}
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.",
);
Expand All @@ -47,15 +48,22 @@ export const worker = async (): Promise<unknown> => {
);

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.`,
);
}
};
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

0 comments on commit f280b38

Please sign in to comment.