Skip to content

Commit

Permalink
Refreshed Auth :) (#79)
Browse files Browse the repository at this point in the history
* first version of swapping out clients

* better reusability

* working model

* removing duplication

* Update to checkCodeQLEnablement (#80)

* Update to checkCodeQLEnablement

Handling a 404 return from API call when no result are found.

* Update checkCodeQLEnablement.ts

Co-authored-by: Nick Liffen <nickliffen@github.com>

* fixing linting

* Update src/utils/clients/core.ts

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>

* updated final small changes to get refresh working

Co-authored-by: Daniel Redman <43892709+djredman99@users.noreply.github.com>
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 16, 2022
1 parent dc3af5e commit a56edf7
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/getOrgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dotenv.config({ path: __dirname + "/../.env" });
import { error } from "./utils/globals";

import { index } from "./utils/getOrganisationsInEnterprise";
import { graphQLClient as octokit } from "./utils/clients";
import { client as octokit } from "./utils/clients";

async function start() {
try {
Expand Down
7 changes: 6 additions & 1 deletion src/utils/checkCodeQLEnablement.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { RequestError } from "@octokit/request-error";
import {
checkCodeScanningAnalysesParameters,
checkCodeScanningAnalysesResponse,
Octokit,
} from "./octokitTypes";

import { Octokit } from "@octokit/core";

export const checkIfCodeQLHasAlreadyRanOnRepo = async (
owner: string,
repo: string,
Expand All @@ -22,6 +24,9 @@ export const checkIfCodeQLHasAlreadyRanOnRepo = async (
if (data.length === 0) return false;
return true;
} catch (e) {
if (e instanceof RequestError) {
if (e.status == 404) return false; // 404 result means no codeQL scans found
}
return true;
}
};
16 changes: 4 additions & 12 deletions src/utils/clients/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ export const auth = async (): Promise<string | Error> => {
return env.GITHUB_API_TOKEN;
}
/* Checking if they have supplied all the required informaiton to generate a GitHub App */
if (
!env.APP_ID ||
!env.APP_PRIVATE_KEY ||
!env.APP_INSTALLATION_ID ||
!env.APP_CLIENT_ID ||
!env.APP_CLIENT_SECRET
) {
if (!env.APP_ID || !env.APP_PRIVATE_KEY || !env.APP_INSTALLATION_ID) {
throw new Error(
"You have not specified a Personal Access Token or all the requried variables for a GitHub App. Please re-check the documentation"
);
Expand All @@ -25,15 +19,13 @@ export const auth = async (): Promise<string | Error> => {
appId: env.APP_ID as string,
privateKey: env.APP_PRIVATE_KEY as string,
installationId: parseInt(env.APP_INSTALLATION_ID as string, 10) as number,
clientId: env.APP_CLIENT_ID as string,
clientSecret: env.APP_CLIENT_SECRET as string,
} as StrategyOptions;

const auth = createAppAuth(options);

try {
const { token } = await auth({ type: "installation" });
return token;
const data = await auth({ type: "installation" });
console.log(data);
return data.token;
} catch (err: any) {
console.error("Error within function (githubAuth)", err.message);
throw new Error(
Expand Down
52 changes: 52 additions & 0 deletions src/utils/clients/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Octokit } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app";
import { env } from "process";

import { baseRestApiURL as baseUrl } from "../globals";

import { RateLimitOptions } from "../../../types/common";

import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { OctokitOptions } from "@octokit/core/dist-types/types";

export const client = async (): Promise<Octokit> => {
const MyOctokit = Octokit.plugin(paginateRest, retry, throttling);
const baseOctokitOptions = {
baseUrl,
request: { retries: 3 },
throttle: {
onRateLimit: (options: RateLimitOptions) => {
return options.request.retryCount <= 3;
},
onAbuseLimit: () => {
return true;
},
},
} as OctokitOptions;

if (env.GITHUB_API_TOKEN) {
return new MyOctokit({
auth: env.GITHUB_API_TOKEN,
...baseOctokitOptions,
});
} else if (env.APP_ID && env.APP_PRIVATE_KEY && env.APP_INSTALLATION_ID) {
return new MyOctokit({
authStrategy: createAppAuth,
auth: {
appId: env.APP_ID as string,
privateKey: env.APP_PRIVATE_KEY as string,
installationId: parseInt(
env.APP_INSTALLATION_ID as string,
10
) as number,
},
...baseOctokitOptions,
});
} else {
throw new Error(
"You have not specified a Personal Access Token or all the requried variables for a GitHub App. Please re-check the documentation"
);
}
};
20 changes: 0 additions & 20 deletions src/utils/clients/graphql.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/utils/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { graphQLClient } from "./graphql";
import { restClient } from "./rest";
import { client } from "./core";
import { auth } from "./auth";

export { graphQLClient, restClient, auth };
export { auth, client };
42 changes: 0 additions & 42 deletions src/utils/clients/rest.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/createBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ref, error, inform } from "./globals";

import { createRefParameters, createRefResponse } from "./octokitTypes";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

export const createBranch = async (
sha: string,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/createPullRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { title, error, inform } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import {
createPullRequestParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/enableDependabotAlerts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inform, error } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import {
createVulnerabilityAlertsParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/enableDependabotUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inform, error } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import {
createVulnerabilityUpdatesParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/enableGHAS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inform, error } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import { updateReposResponse, updateReposParameters } from "./octokitTypes";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/enableIssueCreation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { issueText } from "./text/issueText";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

export const enableIssueCreation = async (
pr: string,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/enableSecretScanning.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inform, error } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import { updateReposResponse, updateReposParameters } from "./octokitTypes";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/findDefaultBranch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { error, inform } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import {
listDefaultBranchParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/findDefaultBranchSHA.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { error, inform } from "./globals";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import {
listDefaultBranchSHAParameters,
Expand Down
15 changes: 10 additions & 5 deletions src/utils/getOrganisationsInEnterprise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { graphql, GraphQlQueryResponseData } from "@octokit/graphql";
import { Octokit } from "@octokit/core";

import { GraphQlQueryResponseData } from "@octokit/graphql";

import { getOrganisationsQuery } from "./graphql";
import { createFile } from "./writeToFile";
Expand All @@ -11,7 +13,7 @@ import {
} from "../../types/common";

const performOrganisationsQuery = async (
client: typeof graphql,
client: Octokit,
query: string,
slug: string,
after: string | null
Expand All @@ -24,7 +26,10 @@ const performOrganisationsQuery = async (
nodes,
},
},
} = (await client(query, { slug, after })) as GraphQlQueryResponseData;
} = (await client.graphql(query, {
slug,
after,
})) as GraphQlQueryResponseData;
return [hasNextPage, endCursor, nodes];
} catch (err) {
console.error(err);
Expand All @@ -33,7 +38,7 @@ const performOrganisationsQuery = async (
};

const getOrganisationsInEnterprise = async (
client: typeof graphql,
client: Octokit,
slug: string,
query: string,
paginatedData = [] as orgsInEnterpriseArray,
Expand Down Expand Up @@ -72,7 +77,7 @@ const getOrganisationsInEnterprise = async (
}
};

export const index = async (client: typeof graphql): Promise<void> => {
export const index = async (client: Octokit): Promise<void> => {
try {
const slug = process.env.GITHUB_ENTERPRISE
? process.env.GITHUB_ENTERPRISE
Expand Down
15 changes: 10 additions & 5 deletions src/utils/paginateQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { graphql, GraphQlQueryResponseData } from "@octokit/graphql";
import { client as octokit } from "./clients";

import { graphQLClient as octokit } from "./clients";
import { Octokit } from "@octokit/core";

import { GraphQlQueryResponseData } from "@octokit/graphql";

import {
GraphQLQueryResponse,
Expand All @@ -11,7 +13,7 @@ import { filterAsync } from "./filterAsync";
import { error, inform } from "./globals";

const performRepositoryQuery = async (
client: typeof graphql,
client: Octokit,
query: string,
slug: string,
after: string | null
Expand All @@ -24,7 +26,10 @@ const performRepositoryQuery = async (
nodes,
},
},
} = (await client(query, { slug, after })) as GraphQlQueryResponseData;
} = (await client.graphql(query, {
slug,
after,
})) as GraphQlQueryResponseData;
return [hasNextPage, endCursor, nodes];
} catch (err) {
error(err);
Expand All @@ -33,7 +38,7 @@ const performRepositoryQuery = async (
};

const getRepositoryInOrganizationPaginate = async (
client: typeof graphql,
client: Octokit,
slug: string,
query: string,
paginatedData = [] as usersWriteAdminReposArray,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/putFileInBranch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from "fs";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";

import { putFileInPathParameters, putFileInPathResponse } from "./octokitTypes";

Expand Down
7 changes: 3 additions & 4 deletions src/utils/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createBranch } from "./createBranch.js";
import { enableSecretScanningAlerts } from "./enableSecretScanning";
import { createPullRequest } from "./createPullRequest.js";
import { writeToFile } from "./writeToFile.js";
import { restClient as octokit } from "./clients";
import { client as octokit } from "./clients";
import { commitFileMac } from "./commitFile.js";
import { enableGHAS } from "./enableGHAS.js";
import { enableDependabotAlerts } from "./enableDependabotAlerts";
Expand All @@ -17,18 +17,17 @@ import { enableIssueCreation } from "./enableIssueCreation";
import { auth as generateAuth } from "./clients";
import { checkIfCodeQLHasAlreadyRanOnRepo } from "./checkCodeQLEnablement";

import { Octokit } from "./octokitTypes";
import { Octokit } from "@octokit/core";
import { inform, reposFileLocation } from "./globals.js";
import { reposFile } from "../../types/common/index.js";

export const worker = async (): Promise<unknown> => {
const client = (await octokit()) as Octokit;
let res;
let orgIndex: number;
let repoIndex: number;
let repos: reposFile;
let file: string;

const client = (await octokit()) as Octokit;
// Read the repos.json file and get the list of repos using fs.readFileSync, handle errors, if empty file return error, if file exists and is not empty JSON.parse it and return the list of repos
try {
file = readFileSync(reposFileLocation, "utf8");
Expand Down

0 comments on commit a56edf7

Please sign in to comment.