Skip to content

Commit a8d21b7

Browse files
committed
refactor: Use OfficialRepositoryName from metadata instead of fetching buckets.json
1 parent cc5a0a3 commit a8d21b7

File tree

8 files changed

+20
-45
lines changed

8 files changed

+20
-45
lines changed

src/__tests__/mocks/fixtures.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const mockSearchResponse = {
2020
Metadata: {
2121
Repository: 'ScoopInstaller/Main',
2222
OfficialRepository: true,
23+
OfficialRepositoryName: 'main',
2324
RepositoryStars: 5000,
2425
BranchName: 'master',
2526
FilePath: 'bucket/git.json',
@@ -44,6 +45,7 @@ export const mockSearchResponse = {
4445
Metadata: {
4546
Repository: 'ScoopInstaller/Extras',
4647
OfficialRepository: true,
48+
OfficialRepositoryName: 'extras',
4749
RepositoryStars: 3500,
4850
BranchName: 'master',
4951
FilePath: 'bucket/github.json',
@@ -78,6 +80,7 @@ export const mockManifest = {
7880
Metadata: {
7981
Repository: 'ScoopInstaller/Main',
8082
OfficialRepository: true,
83+
OfficialRepositoryName: 'main',
8184
RepositoryStars: 5000,
8285
BranchName: 'master',
8386
FilePath: 'bucket/nodejs.json',
@@ -117,20 +120,6 @@ export const mockBucketsResponse = {
117120
],
118121
};
119122

120-
// Mock GitHub buckets.json response
121-
export const mockGitHubBucketsJson = [
122-
{
123-
name: 'main',
124-
repository: 'https://github.com/ScoopInstaller/Main',
125-
official: true,
126-
},
127-
{
128-
name: 'extras',
129-
repository: 'https://github.com/ScoopInstaller/Extras',
130-
official: true,
131-
},
132-
];
133-
134123
// Mock error response
135124
export const mockErrorResponse = {
136125
error: {

src/components/Search.test.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ beforeEach(() => {
122122
mockSearchResult.mockClear();
123123
mockSearchPagination.mockClear();
124124

125-
(globalThis.fetch as unknown as Mock) = vi.fn().mockResolvedValue({
126-
json: vi.fn().mockResolvedValue({ Official: 'main' }),
127-
});
125+
(globalThis.fetch as unknown as Mock) = vi.fn();
128126

129127
(window as unknown as { scrollTo: () => void }).scrollTo = vi.fn();
130128

src/components/Search.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ const Search = (): JSX.Element => {
123123
);
124124
const [installBucketName, setInstallBucketName] = useState<boolean>(getInstallBucketNameFromSearchParams());
125125
const [searchResults, setSearchResults] = useState<SearchResultsJson>();
126-
const [officialRepositories, setOfficialRepositories] = useState<{ [key: string]: string }>({});
127126
const [selectedResult, setSelectedResult] = useState<ManifestJson | null>();
128127
const [selectedResultId, setSelectedResultId] = useState<string>(getSelectedResultFromSearchParams);
129128
const selectedResultRef = useRef<HTMLDivElement>(null);
@@ -184,20 +183,6 @@ const Search = (): JSX.Element => {
184183
updateSearchParams(SEARCH_PARAM_SELECTED_RESULT, selectedResultId, false);
185184
}, [selectedResultId, searchResults, updateSearchParams]);
186185

187-
useEffect(() => {
188-
fetch('https://cdn.jsdelivr.net/gh/ScoopInstaller/Scoop/buckets.json')
189-
.then((response) => response.json())
190-
.then((response) => {
191-
const json = response as { [key: string]: string };
192-
const mapping: { [key: string]: string } = {};
193-
Object.keys(json).forEach((key) => {
194-
mapping[json[key]] = key;
195-
});
196-
setOfficialRepositories(mapping);
197-
})
198-
.catch((error) => console.log(error));
199-
}, []);
200-
201186
const handleQueryChange = useCallback(
202187
(newQuery: string): void => {
203188
updateSearchParams(SEARCH_PARAM_QUERY, newQuery, false);
@@ -318,7 +303,6 @@ const Search = (): JSX.Element => {
318303
cardRef={searchResult.id === selectedResultId ? selectedResultRef : undefined}
319304
key={searchResult.id}
320305
result={searchResult}
321-
officialRepositories={officialRepositories}
322306
installBucketName={installBucketName}
323307
onCopyToClipboard={handleCopyToClipboard}
324308
onResultSelected={handleResultSelected}
@@ -351,7 +335,6 @@ const Search = (): JSX.Element => {
351335
{selectedResult && (
352336
<SearchResult
353337
result={selectedResult}
354-
officialRepositories={officialRepositories}
355338
installBucketName={installBucketName}
356339
onCopyToClipboard={handleCopyToClipboard}
357340
/>

src/components/SearchProcessor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ const SearchProcessor = (props: SearchProcessorProps): JSX.Element => {
191191
'Metadata/Repository',
192192
'Metadata/FilePath',
193193
'Metadata/OfficialRepository',
194+
'Metadata/OfficialRepositoryName',
194195
'Metadata/RepositoryStars',
195196
'Metadata/Committed',
196197
'Metadata/Sha',

src/components/SearchResult.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ describe('SearchResult', () => {
1111
const mockOnCopyToClipboard = vi.fn();
1212
const mockOnResultSelected = vi.fn();
1313

14-
const officialRepositories = {
15-
'ScoopInstaller/Main': 'main',
16-
'ScoopInstaller/Extras': 'extras',
17-
};
18-
1914
const defaultProps = {
2015
result: jsonConvert.deserializeObject(mockManifest, ManifestJsonClass),
21-
officialRepositories,
2216
installBucketName: false,
2317
onCopyToClipboard: mockOnCopyToClipboard,
2418
onResultSelected: mockOnResultSelected,

src/components/SearchResult.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ dayjs.extend(relativeTime);
1919

2020
type SearchResultProps = {
2121
result: ManifestJson;
22-
officialRepositories: { [key: string]: string };
2322
installBucketName: boolean;
2423
onCopyToClipboard: (content: string) => void;
2524
onResultSelected?: (result: ManifestJson) => void;
2625
cardRef?: React.RefObject<HTMLDivElement | null>;
2726
};
2827

2928
const SearchResult = (props: SearchResultProps): JSX.Element => {
30-
const { result, officialRepositories, installBucketName, onCopyToClipboard, onResultSelected, cardRef } = props;
29+
const { result, installBucketName, onCopyToClipboard, onResultSelected, cardRef } = props;
3130
const homepageRef = useRef<HTMLSpanElement>(null);
3231
const [homepageTooltipHidden, setHomepageTooltipHidden] = useState<boolean>(false);
3332

@@ -113,14 +112,13 @@ const SearchResult = (props: SearchResultProps): JSX.Element => {
113112

114113
// Use known repository name for official repositories and keep only organization+repo for community repositories
115114
const formattedHighlightedRepository = metadata.repositoryOfficial
116-
? highlightedRepository?.toString().replace(metadata.repository, officialRepositories[metadata.repository])
115+
? highlightedRepository?.toString().replace(metadata.repository, metadata.officialRepositoryName || metadata.repository)
117116
: highlightedRepository?.toString().replace(/^(<mark>|)(?:.*?\/){3}(.+)$/, '$1$2');
118117

119118
const versionPrefix = version.length > 0 && /^\d/.test(version) && 'v';
120119

121120
const bucketName = metadata.repositoryOfficial
122-
? officialRepositories[metadata.repository] ||
123-
metadata.repository.substring(metadata.repository.lastIndexOf('/') + 1).toLowerCase()
121+
? metadata.officialRepositoryName || metadata.repository.substring(metadata.repository.lastIndexOf('/') + 1).toLowerCase()
124122
: `${extractPathFromUrl(metadata.repository, '_')}`;
125123
const bucketUrl = metadata.repositoryOfficial ? '' : `${metadata.repository}`;
126124
const bucketCommandLine = `${bucketName} ${bucketUrl}`.trim();

src/serialization/MetadataJson.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('MetadataJson', () => {
1414
const metadata = {
1515
Repository: 'ScoopInstaller/Main',
1616
OfficialRepository: true,
17+
OfficialRepositoryName: 'main',
1718
RepositoryStars: 5000,
1819
BranchName: 'master',
1920
FilePath: 'bucket/git.json',
@@ -25,6 +26,7 @@ describe('MetadataJson', () => {
2526

2627
expect(result.repository).toBe('ScoopInstaller/Main');
2728
expect(result.repositoryOfficial).toBe(true);
29+
expect(result.officialRepositoryName).toBe('main');
2830
expect(result.stars).toBe(5000);
2931
expect(result.branchName).toBe('master');
3032
expect(result.filePath).toBe('bucket/git.json');
@@ -46,6 +48,7 @@ describe('MetadataJson', () => {
4648

4749
expect(result.repository).toBe('user/repo');
4850
expect(result.repositoryOfficial).toBe(false);
51+
expect(result.officialRepositoryName).toBe('');
4952
expect(result.stars).toBe(0);
5053
expect(result.branchName).toBe('');
5154
});
@@ -66,6 +69,7 @@ describe('MetadataJson', () => {
6669
expect(result.committed.getFullYear()).toBe(2024);
6770
expect(result.committed.getMonth()).toBe(0); // January = 0
6871
expect(result.committed.getDate()).toBe(15);
72+
expect(result.officialRepositoryName).toBe('');
6973
});
7074
});
7175

@@ -74,6 +78,7 @@ describe('MetadataJson', () => {
7478
const metadata = {
7579
Repository: 'ScoopInstaller/Main',
7680
OfficialRepository: true,
81+
OfficialRepositoryName: 'main',
7782
RepositoryStars: 5000,
7883
FilePath: 'bucket/app.json',
7984
Committed: '2024-01-01T00:00:00Z',
@@ -83,6 +88,7 @@ describe('MetadataJson', () => {
8388
const result = jsonConvert.deserializeObject(metadata, MetadataJson);
8489

8590
expect(result.repositoryOfficial).toBe(true);
91+
expect(result.officialRepositoryName).toBe('main');
8692
});
8793

8894
it('should correctly identify non-official repository', () => {
@@ -98,6 +104,7 @@ describe('MetadataJson', () => {
98104
const result = jsonConvert.deserializeObject(metadata, MetadataJson);
99105

100106
expect(result.repositoryOfficial).toBe(false);
107+
expect(result.officialRepositoryName).toBe('');
101108
});
102109
});
103110

@@ -121,6 +128,7 @@ describe('MetadataJson', () => {
121128
const metadata = {
122129
Repository: 'popular/repo',
123130
OfficialRepository: true,
131+
OfficialRepositoryName: 'extras',
124132
RepositoryStars: 999999,
125133
FilePath: 'bucket/app.json',
126134
Committed: '2024-01-01T00:00:00Z',
@@ -130,6 +138,7 @@ describe('MetadataJson', () => {
130138
const result = jsonConvert.deserializeObject(metadata, MetadataJson);
131139

132140
expect(result.stars).toBe(999999);
141+
expect(result.officialRepositoryName).toBe('extras');
133142
});
134143
});
135144
});

src/serialization/MetadataJson.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class MetadataJson {
1010
@JsonProperty('OfficialRepository', Boolean)
1111
repositoryOfficial = false;
1212

13+
@JsonProperty('OfficialRepositoryName', String, true)
14+
officialRepositoryName?: string = '';
15+
1316
@JsonProperty('RepositoryStars', Number)
1417
stars = 0;
1518

0 commit comments

Comments
 (0)