diff --git a/app/routes/crate/settings/new-trusted-publisher.js b/app/routes/crate/settings/new-trusted-publisher.js index 259cad18732..de0005470f9 100644 --- a/app/routes/crate/settings/new-trusted-publisher.js +++ b/app/routes/crate/settings/new-trusted-publisher.js @@ -5,4 +5,27 @@ export default class NewTrustedPublisherRoute extends Route { let crate = this.modelFor('crate'); return { crate }; } + + setupController(controller, model) { + super.setupController(controller, model); + + controller.repositoryOwner = ''; + controller.repositoryName = ''; + controller.workflowFilename = ''; + controller.environment = ''; + + let repository = model.crate.repository; + if (repository && repository.startsWith('https://github.com/')) { + try { + let url = new URL(repository); + let pathParts = url.pathname.slice(1).split('/'); + if (pathParts.length >= 2) { + controller.repositoryOwner = pathParts[0]; + controller.repositoryName = pathParts[1].replace(/.git$/, ''); + } + } catch { + // ignore malformed URLs + } + } + } } diff --git a/e2e/routes/crate/settings/new-trusted-publisher.spec.ts b/e2e/routes/crate/settings/new-trusted-publisher.spec.ts index 5d5497d3df4..d69fcf81b0d 100644 --- a/e2e/routes/crate/settings/new-trusted-publisher.spec.ts +++ b/e2e/routes/crate/settings/new-trusted-publisher.spec.ts @@ -182,4 +182,67 @@ test.describe('Route | crate.settings.new-trusted-publisher', { tag: '@routes' } // Check that we're redirected back to the crate settings page await expect(page).toHaveURL(`/crates/${crate.name}/settings`); }); + + test.describe('prefill', () => { + const testCases = [ + { + name: 'simple https', + url: 'https://github.com/rust-lang/crates.io', + owner: 'rust-lang', + repo: 'crates.io', + }, + { + name: 'with .git suffix', + url: 'https://github.com/rust-lang/crates.io.git', + owner: 'rust-lang', + repo: 'crates.io', + }, + { + name: 'with extra path segments', + url: 'https://github.com/Byron/google-apis-rs/tree/main/gen/privateca1', + owner: 'Byron', + repo: 'google-apis-rs', + }, + { + name: 'non-github url', + url: 'https://gitlab.com/rust-lang/crates.io', + owner: '', + repo: '', + }, + { + name: 'not a url', + url: 'not a url', + owner: '', + repo: '', + }, + { + name: 'empty string', + url: '', + owner: '', + repo: '', + }, + { + name: 'null', + url: null, + owner: '', + repo: '', + }, + ]; + + for (const { name, url, owner, repo } of testCases) { + test(name, async ({ msw, page }) => { + let { crate } = await prepare(msw); + + msw.db.crate.update({ + where: { id: { equals: crate.id } }, + data: { repository: url }, + }); + + await page.goto(`/crates/${crate.name}/settings/new-trusted-publisher`); + + await expect(page.locator('[data-test-repository-owner]')).toHaveValue(owner); + await expect(page.locator('[data-test-repository-name]')).toHaveValue(repo); + }); + } + }); }); diff --git a/tests/routes/crate/settings/new-trusted-publisher-test.js b/tests/routes/crate/settings/new-trusted-publisher-test.js index 415c79f122c..157231163ae 100644 --- a/tests/routes/crate/settings/new-trusted-publisher-test.js +++ b/tests/routes/crate/settings/new-trusted-publisher-test.js @@ -190,4 +190,66 @@ module('Route | crate.settings.new-trusted-publisher', hooks => { // Check that we're redirected back to the crate settings page assert.strictEqual(currentURL(), `/crates/${crate.name}/settings`); }); + + module('prefill', function () { + let testCases = [ + { + name: 'simple https', + url: 'https://github.com/rust-lang/crates.io', + owner: 'rust-lang', + repo: 'crates.io', + }, + { + name: 'with .git suffix', + url: 'https://github.com/rust-lang/crates.io.git', + owner: 'rust-lang', + repo: 'crates.io', + }, + { + name: 'with extra path segments', + url: 'https://github.com/Byron/google-apis-rs/tree/main/gen/privateca1', + owner: 'Byron', + repo: 'google-apis-rs', + }, + { + name: 'non-github url', + url: 'https://gitlab.com/rust-lang/crates.io', + owner: '', + repo: '', + }, + { + name: 'not a url', + url: 'not a url', + owner: '', + repo: '', + }, + { + name: 'empty string', + url: '', + owner: '', + repo: '', + }, + { + name: 'null', + url: null, + owner: '', + repo: '', + }, + ]; + + for (let { name, url, owner, repo } of testCases) { + test(name, async function (assert) { + let { crate } = prepare(this); + this.db.crate.update({ + where: { id: { equals: crate.id } }, + data: { repository: url }, + }); + + await visit(`/crates/${crate.name}/settings/new-trusted-publisher`); + + assert.dom('[data-test-repository-owner]').hasValue(owner); + assert.dom('[data-test-repository-name]').hasValue(repo); + }); + } + }); });