Skip to content

Commit 890c9b4

Browse files
committed
trustpub: Implement config field prefilling for GitLab URLs
1 parent 55139b0 commit 890c9b4

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

app/routes/crate/settings/new-trusted-publisher.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ export default class NewTrustedPublisherRoute extends Route {
2727
} catch {
2828
// ignore malformed URLs
2929
}
30+
} else if (repository && repository.startsWith('https://gitlab.com/')) {
31+
controller.publisher = 'GitLab';
32+
try {
33+
let url = new URL(repository);
34+
let pathParts = url.pathname.slice(1).split('/');
35+
36+
// Find the repository path end (indicated by /-/ for trees/blobs/etc)
37+
let repoEndIndex = pathParts.indexOf('-');
38+
if (repoEndIndex !== -1) {
39+
pathParts = pathParts.slice(0, repoEndIndex);
40+
}
41+
42+
if (pathParts.length >= 2) {
43+
// For GitLab, support nested groups: https://gitlab.com/a/b/c
44+
// namespace = "a/b", project = "c"
45+
controller.namespace = pathParts.slice(0, -1).join('/');
46+
controller.project = pathParts.at(-1).replace(/.git$/, '');
47+
}
48+
} catch {
49+
// ignore malformed URLs
50+
}
3051
}
3152
}
3253
}

e2e/routes/crate/settings/new-trusted-publisher.spec.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,57 @@ test.describe('Route | crate.settings.new-trusted-publisher', { tag: '@routes' }
5353
test.describe('prefill', () => {
5454
const testCases = [
5555
{
56-
name: 'simple https',
56+
name: 'GitHub: simple https',
5757
url: 'https://github.com/rust-lang/crates.io',
5858
publisher: 'GitHub',
5959
owner: 'rust-lang',
6060
repo: 'crates.io',
6161
},
6262
{
63-
name: 'with .git suffix',
63+
name: 'GitHub: with .git suffix',
6464
url: 'https://github.com/rust-lang/crates.io.git',
6565
publisher: 'GitHub',
6666
owner: 'rust-lang',
6767
repo: 'crates.io',
6868
},
6969
{
70-
name: 'with extra path segments',
70+
name: 'GitHub: with extra path segments',
7171
url: 'https://github.com/Byron/google-apis-rs/tree/main/gen/privateca1',
7272
publisher: 'GitHub',
7373
owner: 'Byron',
7474
repo: 'google-apis-rs',
7575
},
7676
{
77-
name: 'non-github url',
77+
name: 'GitLab: simple https',
7878
url: 'https://gitlab.com/rust-lang/crates.io',
79+
publisher: 'GitLab',
80+
owner: 'rust-lang',
81+
repo: 'crates.io',
82+
},
83+
{
84+
name: 'GitLab: with .git suffix',
85+
url: 'https://gitlab.com/rust-lang/crates.io.git',
86+
publisher: 'GitLab',
87+
owner: 'rust-lang',
88+
repo: 'crates.io',
89+
},
90+
{
91+
name: 'GitLab: with extra path segments',
92+
url: 'https://gitlab.com/Byron/google-apis-rs/-/tree/main/gen/privateca1',
93+
publisher: 'GitLab',
94+
owner: 'Byron',
95+
repo: 'google-apis-rs',
96+
},
97+
{
98+
name: 'GitLab: nested groups',
99+
url: 'https://gitlab.com/a/b/c',
100+
publisher: 'GitLab',
101+
owner: 'a/b',
102+
repo: 'c',
103+
},
104+
{
105+
name: 'non-github url',
106+
url: 'https://example.com/rust-lang/crates.io',
79107
publisher: 'GitHub',
80108
owner: '',
81109
repo: '',

tests/routes/crate/settings/new-trusted-publisher-test.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,57 @@ module('Route | crate.settings.new-trusted-publisher', hooks => {
6363
module('prefill', function () {
6464
let testCases = [
6565
{
66-
name: 'simple https',
66+
name: 'GitHub: simple https',
6767
url: 'https://github.com/rust-lang/crates.io',
6868
publisher: 'GitHub',
6969
owner: 'rust-lang',
7070
repo: 'crates.io',
7171
},
7272
{
73-
name: 'with .git suffix',
73+
name: 'GitHub: with .git suffix',
7474
url: 'https://github.com/rust-lang/crates.io.git',
7575
publisher: 'GitHub',
7676
owner: 'rust-lang',
7777
repo: 'crates.io',
7878
},
7979
{
80-
name: 'with extra path segments',
80+
name: 'GitHub: with extra path segments',
8181
url: 'https://github.com/Byron/google-apis-rs/tree/main/gen/privateca1',
8282
publisher: 'GitHub',
8383
owner: 'Byron',
8484
repo: 'google-apis-rs',
8585
},
8686
{
87-
name: 'non-github url',
87+
name: 'GitLab: simple https',
8888
url: 'https://gitlab.com/rust-lang/crates.io',
89+
publisher: 'GitLab',
90+
owner: 'rust-lang',
91+
repo: 'crates.io',
92+
},
93+
{
94+
name: 'GitLab: with .git suffix',
95+
url: 'https://gitlab.com/rust-lang/crates.io.git',
96+
publisher: 'GitLab',
97+
owner: 'rust-lang',
98+
repo: 'crates.io',
99+
},
100+
{
101+
name: 'GitLab: with extra path segments',
102+
url: 'https://gitlab.com/Byron/google-apis-rs/-/tree/main/gen/privateca1',
103+
publisher: 'GitLab',
104+
owner: 'Byron',
105+
repo: 'google-apis-rs',
106+
},
107+
{
108+
name: 'GitLab: nested groups',
109+
url: 'https://gitlab.com/a/b/c',
110+
publisher: 'GitLab',
111+
owner: 'a/b',
112+
repo: 'c',
113+
},
114+
{
115+
name: 'non-github url',
116+
url: 'https://example.com/rust-lang/crates.io',
89117
publisher: 'GitHub',
90118
owner: '',
91119
repo: '',

0 commit comments

Comments
 (0)