0
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-26 07:06:13 +01:00
gitea/web_src/js/features/repo-migrate.js
Lunny Xiao 2e3a191097
Fix javascript error when an anonymous user visiting migration page (#32144) (#32179)
backport #32144

This PR fixes javascript errors when an anonymous user visits the
migration page.
It also makes task view checking more restrictive.

The router moved from `/user/task/{id}/status` to
`/username/reponame/-/migrate/status` because it's a migrate status.

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2024-10-04 17:58:04 +00:00

62 lines
1.7 KiB
JavaScript

import {hideElem, showElem} from '../utils/dom.js';
import {GET, POST} from '../modules/fetch.js';
export function initRepoMigrationStatusChecker() {
const repoMigrating = document.getElementById('repo_migrating');
if (!repoMigrating) return;
document.getElementById('repo_migrating_retry')?.addEventListener('click', doMigrationRetry);
const repoLink = repoMigrating.getAttribute('data-migrating-repo-link');
// returns true if the refresh still needs to be called after a while
const refresh = async () => {
const res = await GET(`${repoLink}/-/migrate/status`);
if (res.status !== 200) return true; // continue to refresh if network error occurs
const data = await res.json();
// for all status
if (data.message) {
document.getElementById('repo_migrating_progress_message').textContent = data.message;
}
// TaskStatusFinished
if (data.status === 4) {
window.location.reload();
return false;
}
// TaskStatusFailed
if (data.status === 3) {
hideElem('#repo_migrating_progress');
hideElem('#repo_migrating');
showElem('#repo_migrating_retry');
showElem('#repo_migrating_failed');
showElem('#repo_migrating_failed_image');
document.getElementById('repo_migrating_failed_error').textContent = data.message;
return false;
}
return true; // continue to refresh
};
const syncTaskStatus = async () => {
let doNextRefresh = true;
try {
doNextRefresh = await refresh();
} finally {
if (doNextRefresh) {
setTimeout(syncTaskStatus, 2000);
}
}
};
syncTaskStatus(); // no await
}
async function doMigrationRetry(e) {
await POST(e.target.getAttribute('data-migrating-task-retry-url'));
window.location.reload();
}