let user choose SSH key owner when migrating to an org

This commit is contained in:
pomidorry committed 2026-06-07 14:38:26 +03:00
1 parent 781666b5eb
commit 6ce30aa144
13 files changed
+109 -19

No files matched your search

+67 -6
View File
@@ -60,6 +60,12 @@ async function doMigrationRetry(e: Event) {
window.location.reload();
}
function isSSHURL(url: string): boolean {
return url.startsWith('ssh://') ||
url.startsWith('git@') ||
(url.includes('@') && url.includes(':') && !url.includes('://'));
}
export function initRepoMigrationForm() {
const cloneAddrInput = document.querySelector<HTMLInputElement>('#clone_addr');
const authUsernameInput = document.querySelector<HTMLInputElement>('#auth_username');
@@ -68,12 +74,6 @@ export function initRepoMigrationForm() {
if (!cloneAddrInput || !authUsernameInput || !authPasswordInput || !sshHelpText) return;
function isSSHURL(url: string): boolean {
return url.startsWith('ssh://') ||
url.startsWith('git@') ||
(url.includes('@') && url.includes(':') && !url.includes('://'));
}
function updateAuthFields() {
const url = cloneAddrInput!.value.trim();
const isSSH = isSSHURL(url);
@@ -99,4 +99,65 @@ export function initRepoMigrationForm() {
updateAuthFields();
cloneAddrInput.addEventListener('input', updateAuthFields);
cloneAddrInput.addEventListener('blur', updateAuthFields);
initSSHKeyOwnerSelector(cloneAddrInput);
}
// initSSHKeyOwnerSelector wires the "managed SSH key owner" selector. It is
// hidden by default and only shown when an SSH URL is entered AND the chosen
// target owner is an organisation (i.e. not the signed-in user) — in that case
// the user can pick between the org's managed key (default) and their personal
// managed key. The hidden #ssh_key_owner_id field is submitted with the form.
function initSSHKeyOwnerSelector(cloneAddrInput: HTMLInputElement) {
const container = document.querySelector<HTMLElement>('.ssh-key-owner-selector');
const select = document.querySelector<HTMLSelectElement>('#ssh_key_owner_select');
const hiddenId = document.querySelector<HTMLInputElement>('#ssh_key_owner_id');
const uidInput = document.querySelector<HTMLInputElement>('#uid');
if (!container || !select || !hiddenId || !uidInput) return;
const signedUserID = container.dataset.signedUserId ?? '';
const signedUserName = container.dataset.signedUserName ?? '';
// Build {ownerID -> name} from the owner dropdown menu items
const ownerNameById = new Map<string, string>();
for (const item of document.querySelectorAll<HTMLElement>('.owner.dropdown .menu .item')) {
const id = item.dataset.value;
const name = item.getAttribute('title') ?? item.textContent?.trim() ?? '';
if (id) ownerNameById.set(id, name);
}
function update() {
const isSSH = isSSHURL(cloneAddrInput.value.trim());
const targetUid = uidInput!.value;
// No choice: non-SSH URL, or migrating into the user's own account
if (!isSSH || targetUid === signedUserID) {
hideElem(container!);
hiddenId!.value = '0';
return;
}
// Target is an organisation — offer both keys
const orgName = ownerNameById.get(targetUid) ?? `#${targetUid}`;
select!.innerHTML = '';
select!.add(new Option(`Use ${orgName}'s managed SSH key (default)`, '0'));
select!.add(new Option(`Use your personal managed SSH key (${signedUserName})`, signedUserID));
select!.value = hiddenId!.value || '0';
hiddenId!.value = select!.value;
showElem(container!);
}
select.addEventListener('change', () => {
hiddenId!.value = select!.value;
});
// Semantic UI updates the #uid hidden input via menu item clicks
for (const item of document.querySelectorAll<HTMLElement>('.owner.dropdown .menu .item')) {
item.addEventListener('click', () => setTimeout(update, 0));
}
cloneAddrInput.addEventListener('input', update);
cloneAddrInput.addEventListener('blur', update);
update();
}