Skip to content

Commit

Permalink
fixed repo count bug due to organization not being ignored, and added…
Browse files Browse the repository at this point in the history
… ignore organization logic.
  • Loading branch information
aasmal97 committed Dec 14, 2023
1 parent 2770c1a commit c7bfe0a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export const ignoreRepoMap = {
".github-private": true,
};
export const ignoreOrganization = {
'chingu-voyages': true,
}
export const includeRepoMapOverwrite = {
"v46-tier3-team-39": true,
}
export const includeRepoMapIfPrivate = {
"War-Chronicle-AWS-Backend": true,
"Production-MongoDB-Realm-App": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";
import { getRepositories } from "../../../../../../../utils/github/getUserRepos";
import {
getAllGithubRepos
} from "../../../../../../../utils/github/getUserRepos";
import { convertToStr } from "../../../../../../../utils/general/convertToStr";
import axios, { AxiosError } from "axios";
import {
ignoreOrganization,
ignoreRepoMap,
includeRepoMapIfPrivate,
includeRepoMapOverwrite,
} from "../../../github/githubPost/ignoreRepoList";
import { getDocuments } from "../../../../../../../utils/crudRestApiMethods/getMethod";
import { putDocument } from "../../../../../../../utils/crudRestApiMethods/putMethod";
Expand Down Expand Up @@ -147,19 +151,19 @@ const createChannel = async ({
return `Error setting up webhook for ${reqUrl}`;
}
};

export const createWatchChannels = async () => {
const githubToken = convertToStr(process.env.GIT_HUB_PERSONAL_ACCESS_TOKEN);
const {
data: {
user: {
repositories: { nodes: repositories },
},
},
} = await getRepositories(githubToken);
const repositories = await getAllGithubRepos({ token: githubToken });
const repoNames: (Omit<GithubChannelProps, "githubToken"> | null)[] =
repositories.map((repo: any) => {
if (repo.name in ignoreRepoMap) return null;
if (repo.isPrivate && !(repo.name in includeRepoMapIfPrivate)) return null;
if (!(repo.name in includeRepoMapOverwrite)) {
if ((repo.owner?.login || "") in ignoreOrganization) return null;
if (repo.name in ignoreRepoMap) return null;
if (repo.isPrivate && !(repo.name in includeRepoMapIfPrivate))
return null;
}
//we continue if repo name is in overwrite, or passes prev conditions
const nodes = repo.repositoryTopics.nodes;
const topicNames = nodes.map((n: any) => n.topic.name);
return {
Expand All @@ -177,7 +181,8 @@ export const createWatchChannels = async () => {
},
};
});
const promiseArr = repoNames.map((repo) => {
const filteredRepoNames = repoNames.filter((r) => r !== null);
const promiseArr = filteredRepoNames.map((repo) => {
if (!repo) return null;
return createChannel({
...repo,
Expand Down
163 changes: 140 additions & 23 deletions utils/github/getUserRepos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";
// import * as dotenv from "dotenv";
// dotenv.config();
import { ignoreOrganization } from "../../app/lib/webhooks/resources/github/githubPost/ignoreRepoList";
export const callGithubGraphQL = async ({
query,
variables,
Expand All @@ -21,35 +20,111 @@ export const callGithubGraphQL = async ({
});
return data;
};
export const getRepositories = async (token?: string) => {
export const getOrgRepos = async ({
token,
org,
cursor = null,
}: {
token?: string;
org: string;
cursor?: string | null;
}) => {
const query = `query {
viewer{
organization(login: "${org}"){
repositories(first:100, after:${
cursor ? `"${cursor}"` : cursor
}){
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes {
id
name
}
}
}
}
}
`;
const data = await callGithubGraphQL({
token,
query,
});
return data;
};
export const getAllGithubOrgRepos = async({
token,
org
}:{
token: string
org: string
}) => {
const repositories: any[] = [];
let hasEnded = false;
let nextCursor: string | null = null;
while (!hasEnded) {
try {
const {
data: {
search: {
edges: newRepositories,
pageInfo: { endCursor, hasNextPage },
},
},
} = await getOrgRepos({
token, org, cursor: nextCursor,
});
hasEnded = !hasNextPage as boolean;
nextCursor = endCursor as string;
repositories.push(...newRepositories.map((repo: any) => repo.node));
} catch (err) {
console.log(err);
break;
}
}
return repositories
}
export const getRepositories = async ({
token,
cursor = null,
}: {
token?: string;
cursor?: string | null;
}) => {
const username = "aasmal97";
const query = `query {
user(login: "${username}"){
repositories(first: 100, affiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR], ownerAffiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR]) {
repositories(first: 100, after:${
cursor ? `"${cursor}"` : cursor
}, affiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR],ownerAffiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR]) {
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes{
name
isInOrganization
isPrivate
url
homepageUrl
description
createdAt
repositoryTopics(first: 100) {
nodes {
topic {
name
id
name
isInOrganization
isPrivate
url
homepageUrl
description
createdAt
repositoryTopics(first: 100) {
nodes {
topic {
name
}
}
}
}
owner {
login
url
}
owner {
login
url
}
}
}
}
Expand All @@ -61,8 +136,50 @@ export const getRepositories = async (token?: string) => {
});
return data;
};
export const getRepoCount = async (token?:string) => {
const data = await getRepositories(token);
const repoCount = data.data.user.repositories.totalCount;
export const getAllGithubRepos = async({
token
}:{
token: string
}) => {
const repositories: any[] = [];
let hasEnded = false;
let nextCursor: string | null = null;
while (!hasEnded) {
try {
const {
data: {
user: {
repositories: {
nodes: newRepositories,
pageInfo: { endCursor, hasNextPage },
},
},
},
} = await getRepositories({
token,
cursor: nextCursor,
});
hasEnded = !hasNextPage as boolean;
nextCursor = endCursor as string;
repositories.push(...newRepositories);
} catch (err) {
console.log(err);
break;
}
}
return repositories
}
export const getRepoCount = async (token?: string) => {
const orgRepoCountsPromise = Object.entries(ignoreOrganization).map(
([key, value]) => getOrgRepos({ token, org: key})
);
const dataPromise = getRepositories({ token });
const results = await Promise.all([...orgRepoCountsPromise, dataPromise]);
const data = results.pop();
const totalOrgCount = results.reduce(
(acc, cur) => acc + cur.data.viewer.organization.repositories.totalCount,
0
);
const repoCount = data.data.user.repositories.totalCount - totalOrgCount;
return repoCount;
};

0 comments on commit c7bfe0a

Please sign in to comment.