Skip to content

trustpub: Prefill repo owner and name for crates with GitHub repo URLs #11497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/routes/crate/settings/new-trusted-publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
63 changes: 63 additions & 0 deletions e2e/routes/crate/settings/new-trusted-publisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
});
62 changes: 62 additions & 0 deletions tests/routes/crate/settings/new-trusted-publisher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
});
Loading