diff --git a/app/cypress/e2e/settings/special-directories.cy.js b/app/cypress/e2e/settings/special-directories.cy.js index 0a9e248d..021df88d 100644 --- a/app/cypress/e2e/settings/special-directories.cy.js +++ b/app/cypress/e2e/settings/special-directories.cy.js @@ -1,5 +1,5 @@ /* - * The five Special Directories fields on /admin/settings. + * The six Special Directories fields on /admin/settings. * * These are the fields that shipped printing their own prefix over their own value, and the * ones whose visible labels were never associated with their inputs. Both faults were @@ -19,6 +19,7 @@ describe('Special Directories settings', () => { ['Map Directory', 'map'], ['Zims Directory', 'zims'], ['Playlists Directory', 'playlists'], + ['Repos Directory', 'repos/%(repo_tag)s/%(repo_name)s'], ]; beforeEach(() => { @@ -38,8 +39,8 @@ describe('Special Directories settings', () => { it('names every directory field, so they can be told apart', () => { /* - * All five fields sit in one grid, hold similar-looking relative paths, and share the - * same prefix. Without a name attached to the input, a screen reader user hears five + * All six fields sit in one grid, hold similar-looking relative paths, and share the + * same prefix. Without a name attached to the input, a screen reader user hears six * fields described only as "/media/wrolpi/" and cannot tell which one saves videos. */ directories.forEach(([label, value]) => { diff --git a/app/src/App.css b/app/src/App.css index 4c961e69..6b423219 100644 --- a/app/src/App.css +++ b/app/src/App.css @@ -326,6 +326,13 @@ pre.wrap-text { padding: 0 0.25em; } +/* A repo's README is part of its page, not a modal; it grows with its content. */ +.repo-readme .markdown-preview { + height: auto; + min-height: 0; + overflow: visible; +} + .markdown-raw { margin: 0; white-space: pre-wrap; diff --git a/app/src/App.js b/app/src/App.js index 25881b13..c3299d98 100644 --- a/app/src/App.js +++ b/app/src/App.js @@ -30,6 +30,7 @@ import {FilePreviewProvider} from "./components/FilePreview"; import {TagsProvider} from "./Tags"; import {ZimRoute} from "./components/Zim"; import {PlaylistsRoute} from "./components/Playlists"; +import {ReposRoute} from "./components/Repos"; import {DocsRoute} from "./components/Docs"; import {FlasherRoute} from "./components/Flasher"; import ErrorBoundary from "./components/ErrorBoundary"; @@ -117,6 +118,7 @@ const router = createBrowserRouter(createRoutesFromElements(}/> }/> }/> + }/> }/> }/> )); diff --git a/app/src/api.js b/app/src/api.js index 907d6012..216382dd 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -7,6 +7,7 @@ import { COLLECTIONS_API, DEFAULT_LIMIT, Downloaders, + REPOS_API, VIDEOS_API, ZIM_API } from "./components/Vars"; @@ -406,6 +407,7 @@ export async function getSettings() { nav_color: content.nav_color, media_directory: content.media_directory, playlists_destination: content.playlists_destination, + repos_destination: content.repos_destination, require_cookies_unlocked: content.require_cookies_unlocked, require_media_mounted: content.require_media_mounted, save_ffprobe_json: content.save_ffprobe_json, @@ -927,6 +929,21 @@ export async function getCollectionTagInfo(collectionId, tagName) { } } +// Tag any Collection (and optionally move its directory). +export async function tagCollection(collectionId, tagName, directory) { + const body = {tag_name: tagName}; + if (directory) { + body['directory'] = directory; + } + const response = await apiPost(`${COLLECTIONS_API}/${collectionId}/tag`, body); + if (response.ok) { + return await response.json(); + } + const message = await getErrorMessage(response, 'Unable to tag. See server logs.'); + toast({type: 'error', title: 'Tag Error', description: message, time: 5000}); + throw new Error(message); +} + export async function tagDomain(domainId, tagName, directory) { const body = { tag_name: tagName, @@ -2051,6 +2068,7 @@ export async function searchSuggestions(search_str) { domains: content.domains, authors: content.authors || [], subjects: content.subjects || [], + repos: content.repos || [], } } else { console.error('Failed to get file search suggestions!'); @@ -2089,8 +2107,8 @@ export async function searchEstimateZims(search_str, tagNames) { } } -export async function searchEstimateOthers(tagNames) { - const body = {tag_names: tagNames}; +export async function searchEstimateOthers(tagNames, searchStr) { + const body = {tag_names: tagNames, search_str: searchStr || null}; const response = await apiPost(`${API_URI}/search_other_estimates`, body); if (response.ok) { const content = await response.json(); @@ -2488,3 +2506,79 @@ export async function moveBookmark(nodeId, parent_id, position = null) { export async function deleteBookmark(nodeId) { return await bookmarksModify(apiDelete(`${API_URI}/bookmarks/${nodeId}`), 'Could not delete bookmark'); } + +async function reposError(response, fallbackMessage) { + const message = await getErrorMessage(response, fallbackMessage); + toast({type: 'error', title: 'Repos Error', description: message, time: 5000}); + throw Error(message); +} + +export async function fetchRepos() { + const response = await apiGet(REPOS_API); + if (response.ok) { + return (await response.json())['repos']; + } + await reposError(response, 'Failed to fetch repos.'); +} + +export async function getRepo(repoId) { + const response = await apiGet(`${REPOS_API}/${repoId}`); + if (response.ok) { + return (await response.json())['repo']; + } + await reposError(response, 'Failed to fetch repo.'); +} + +// `repo`: {url, name?, tag_name?, frequency?, mode?, branch?, description?} +export async function createRepo(repo) { + const response = await apiPost(REPOS_API, repo); + if (response.ok) { + return (await response.json())['repo']; + } + await reposError(response, 'Failed to add repo.'); +} + +// `changes`: {description?, frequency?, mode?, branch?}. An empty branch follows the remote's default branch. +export async function updateRepo(repoId, changes) { + const response = await apiPut(`${REPOS_API}/${repoId}`, changes); + if (response.ok) { + return (await response.json())['repo']; + } + await reposError(response, 'Failed to update repo.'); +} + +export async function deleteRepo(repoId, deleteFiles = false) { + const response = await apiDelete(`${REPOS_API}/${repoId}?delete_files=${deleteFiles ? 'true' : 'false'}`); + if (!response.ok) { + await reposError(response, 'Failed to delete repo.'); + } +} + +// Download the repo's updates now. +export async function updateRepoNow(repoId) { + const response = await apiPost(`${REPOS_API}/${repoId}/update`); + if (!response.ok) { + await reposError(response, 'Failed to start the repo update.'); + } +} + +// One directory of a repo's files: {path, entries: [{name, path, is_dir, size}], readme_path}. +export async function getRepoTree(repoId, path) { + const query = path ? `?path=${encodeURIComponent(path)}` : ''; + const response = await apiGet(`${REPOS_API}/${repoId}/tree${query}`); + if (response.ok) { + return await response.json(); + } + await reposError(response, 'Failed to list the repo files.'); +} + +// Repos matching the search (name and README) and/or the tags. Each has an escaped `readme_headline`. +export async function searchRepos(searchStr, tagNames) { + const body = {search_str: searchStr || null, tag_names: tagNames || []}; + const response = await apiPost(`${REPOS_API}/search`, body); + if (response.ok) { + const content = await response.json(); + return {repos: content.repos, total: content.totals.repos}; + } + await reposError(response, 'Failed to search repos.'); +} diff --git a/app/src/components/Common.test.js b/app/src/components/Common.test.js index 408b7804..74ea1f8d 100644 --- a/app/src/components/Common.test.js +++ b/app/src/components/Common.test.js @@ -1121,7 +1121,7 @@ describe('a row of buttons', () => { * the two had been inline before. * * A row fixes it without touching the stack: the row is the stretched child, and it lays - * its buttons out at their own width. Nine sites, so this scan rather than nine + * its buttons out at their own width. Twelve sites, so this scan rather than twelve * assertions -- and a scan because a stretched button is still a working button. */ const offenders = []; @@ -1140,8 +1140,8 @@ describe('a row of buttons', () => { } } - // Nine renders. Common.js declares it, and a declaration is not a ` ({ ...jest.requireActual('../api'), searchChannels: jest.fn(), + searchRepos: jest.fn(), })); const NO_CHANNELS = /No Channels/; -beforeEach(() => searchChannels.mockReset()); +beforeEach(() => { + searchChannels.mockReset(); + searchRepos.mockReset(); + searchRepos.mockResolvedValue({repos: [], total: 0}); +}); test('pending: shows a loader, not "No Channels"', () => { searchChannels.mockReturnValue(new Promise(() => { @@ -54,3 +59,61 @@ test('loaded: lists the channels', async () => { expect(screen.getByText('Cooking')).toBeInTheDocument(); expect(screen.queryByText(NO_CHANNELS)).not.toBeInTheDocument(); }); + +describe('Repos', () => { + test('pending: the Repos section loads while Channels are shown', async () => { + searchChannels.mockResolvedValue({channels: []}); + searchRepos.mockReturnValue(new Promise(() => { + })); + const {container} = render(); + await act(async () => { + }); + expect(screen.getByText(NO_CHANNELS)).toBeInTheDocument(); + expect(container.querySelector('.wrolpi-loading')).toBeInTheDocument(); + expect(screen.queryByText('No Repos')).not.toBeInTheDocument(); + }); + + test('empty: shows "No Repos"', async () => { + searchChannels.mockResolvedValue({channels: []}); + render(); + await act(async () => { + }); + expect(screen.getByText('No Repos')).toBeInTheDocument(); + }); + + test('failed: shows an error only in the Repos section', async () => { + searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Cooking'}]}); + searchRepos.mockRejectedValue(new Error('boom')); + const spy = jest.spyOn(console, 'error').mockImplementation(() => { + }); + try { + render(); + await act(async () => { + }); + expect(screen.getByText('Cooking')).toBeInTheDocument(); + expect(screen.getByText(/Could not fetch the repos/)).toBeInTheDocument(); + } finally { + spy.mockRestore(); + } + }); + + test('loaded: lists repos by the search, with their README headline', async () => { + searchChannels.mockResolvedValue({channels: []}); + searchRepos.mockResolvedValue({ + total: 1, repos: [{ + id: 7, name: 'kiwix-tools', tag_name: null, + // The server escapes the README; only the highlight is markup. + readme_headline: 'Serve Zim files <script>', + }], + }); + const searchParams = new URLSearchParams('q=zim&tag=Software'); + const {container} = render(, {query: {searchParams}}); + await act(async () => { + }); + expect(searchRepos).toHaveBeenCalledWith('zim', ['Software']); + expect(screen.getByText('kiwix-tools').closest('a')).toHaveAttribute('href', '/repos/7'); + expect(container.querySelector('u').textContent).toBe('Zim'); + expect(container.querySelector('script')).toBeNull(); + expect(screen.getByText(/files