Skip to content

Commit e3498f5

Browse files
committed
fetchGithubData avoids unnecessary request for main branch if master exists
1 parent 73e72c3 commit e3498f5

3 files changed

Lines changed: 49 additions & 36 deletions

File tree

src/types/github-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type RepositoryInfo = {
33
description: string;
44
topics?: string[];
55
createdDatetime: string;
6-
lastCommitDatetime: string; // must always be present
6+
lastCommitDatetime: string;
77
license?: string;
88
readme_content: string;
99
};

src/utils/fetchGithubData.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -121,50 +121,60 @@ async function getPackageJsonLicenseFromMaster(
121121
owner: string,
122122
repo: string,
123123
): Promise<string | undefined> {
124-
// Spec: license from package.json on master HEAD commit, if present.
125-
// Fallback: if 'master' isn't available, try 'main'.
126-
const candidateBranches = ['master', 'main'] as const;
124+
// Spec: Read license from package.json on master HEAD.
125+
// Only try 'main' if fetching from 'master' fails (e.g., 404).
127126

128-
for (const branch of candidateBranches) {
127+
const extract = (raw: string): string | undefined => {
129128
try {
130-
const raw = await fetchText(
131-
`https://raw.githubusercontent.com/${owner}/${repo}/${branch}/package.json`,
132-
);
133-
try {
134-
const pkg = JSON.parse(raw) as unknown;
129+
const pkg = JSON.parse(raw) as unknown;
130+
if (
131+
pkg &&
132+
typeof pkg === 'object' &&
133+
'license' in (pkg as Record<string, unknown>)
134+
) {
135+
const lic = (pkg as Record<string, unknown>).license;
136+
if (typeof lic === 'string') return lic;
135137
if (
136-
pkg &&
137-
typeof pkg === 'object' &&
138-
'license' in (pkg as Record<string, unknown>)
138+
lic &&
139+
typeof lic === 'object' &&
140+
'type' in (lic as Record<string, unknown>)
139141
) {
140-
const lic = (pkg as Record<string, unknown>).license;
141-
if (typeof lic === 'string') return lic;
142-
if (
143-
lic &&
144-
typeof lic === 'object' &&
145-
'type' in (lic as Record<string, unknown>)
146-
) {
147-
const typeVal = (lic as Record<string, unknown>).type;
148-
if (typeof typeVal === 'string') return typeVal;
149-
}
142+
const typeVal = (lic as Record<string, unknown>).type;
143+
if (typeof typeVal === 'string') return typeVal;
150144
}
151-
// Support legacy `licenses` array
152-
if (pkg && typeof pkg === 'object') {
153-
const anyPkg = pkg as Record<string, unknown>;
154-
const licensesRaw = anyPkg.licenses;
155-
if (Array.isArray(licensesRaw) && licensesRaw.length > 0) {
156-
const first = licensesRaw[0] as { type?: unknown };
157-
if (typeof first.type === 'string') return first.type;
158-
}
145+
}
146+
// Support legacy `licenses` array
147+
if (pkg && typeof pkg === 'object') {
148+
const anyPkg = pkg as Record<string, unknown>;
149+
const licensesRaw = anyPkg.licenses;
150+
if (Array.isArray(licensesRaw) && licensesRaw.length > 0) {
151+
const first = licensesRaw[0] as { type?: unknown };
152+
if (typeof first.type === 'string') return first.type;
159153
}
160-
} catch {
161-
// ignore JSON parsing errors and try next branch
162154
}
163155
} catch {
164-
// package.json not found or inaccessible on this branch; try next
156+
// ignore JSON parsing errors; treat as no license
157+
}
158+
return undefined;
159+
};
160+
161+
// Try master
162+
try {
163+
const raw = await fetchText(
164+
`https://raw.githubusercontent.com/${owner}/${repo}/master/package.json`,
165+
);
166+
return extract(raw);
167+
} catch {
168+
// If master not available, try main
169+
try {
170+
const raw = await fetchText(
171+
`https://raw.githubusercontent.com/${owner}/${repo}/main/package.json`,
172+
);
173+
return extract(raw);
174+
} catch {
175+
return undefined;
165176
}
166177
}
167-
return undefined;
168178
}
169179

170180
async function getLastCommitDatetime(

src/utils/testsSetup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import '@testing-library/jest-dom';
22
import { vi } from 'vitest';
33

4-
import { REPOSITORIES as STUB_REPOSITORIES, METADATA as STUB_METADATA } from 'mocks/mockGithubData';
4+
import {
5+
REPOSITORIES as STUB_REPOSITORIES,
6+
METADATA as STUB_METADATA,
7+
} from 'mocks/mockGithubData';
58

69
vi.mock('utils/githubData', () => ({
710
REPOSITORIES: STUB_REPOSITORIES,

0 commit comments

Comments
 (0)