Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,47 @@ type RepositoriesResponse = {
} | null;
};

const REPOSITORIES_GRAPHQL_QUERY = `query($login: String!) {
user(login: $login) {
repositories(first: 100, ownerAffiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], orderBy: {field: STARGAZERS, direction: DESC}, isFork: false, privacy: PUBLIC) {
totalCount
nodes {
name
description
url
stargazerCount
forkCount
isFork
primaryLanguage { name color }
languages(first: 10) {
edges {
size
node { name color }
}
}
repositoryTopics(first: 10) {
nodes {
topic { name }
}
}
}
}
}
}`;

async function fetchRepositoriesGraphQL(
username: string,
token: string
): Promise<RepositoryData> {
const data = await graphql<RepositoriesResponse>(REPOSITORIES_GRAPHQL_QUERY, token, { login: username });
if (!data.user) {
throw new UserNotFoundError(username);
}

const repos = data.user.repositories.nodes.filter((r) => !r.isFork);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the GraphQL query REPOSITORIES_GRAPHQL_QUERY already specifies isFork: false in its arguments, the GitHub API will not return any fork repositories. Therefore, filtering the nodes with !r.isFork is redundant. You can simplify this by just filtering out any potential null/undefined nodes using filter(Boolean).

Suggested change
const repos = data.user.repositories.nodes.filter((r) => !r.isFork);
const repos = data.user.repositories.nodes.filter(Boolean);

return processRepoData(repos);
}

/**
* Task⑤: リポジトリ一覧・言語統計・トップリポジトリを取得
* 認証時: GraphQL (言語バイト数ベース), 未認証時: REST フォールバック
Expand All @@ -310,41 +351,7 @@ export const fetchRepositories = cache(async function fetchRepositories(
return fetchRepositoriesREST(username);
}

const query = `query($login: String!) {
user(login: $login) {
repositories(first: 100, ownerAffiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], orderBy: {field: STARGAZERS, direction: DESC}, isFork: false, privacy: PUBLIC) {
totalCount
nodes {
name
description
url
stargazerCount
forkCount
isFork
primaryLanguage { name color }
languages(first: 10) {
edges {
size
node { name color }
}
}
repositoryTopics(first: 10) {
nodes {
topic { name }
}
}
}
}
}
}`;

const data = await graphql<RepositoriesResponse>(query, token, { login: username });
if (!data.user) {
throw new UserNotFoundError(username);
}

const repos = data.user.repositories.nodes.filter((r) => !r.isFork);
return processRepoData(repos);
return fetchRepositoriesGraphQL(username, token);
});

async function fetchRepositoriesREST(username: string): Promise<RepositoryData> {
Expand Down
Loading