Skip to content

Commit c7efd3b

Browse files
committed
refactor: inline getProjectId and getProjectPath
1 parent 743ab0a commit c7efd3b

5 files changed

+174
-83
lines changed

lib/get-project-context.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import escapeStringRegexp from "escape-string-regexp";
2+
import parseUrl from "parse-url";
13
import urlJoin from "url-join";
24

3-
import getProjectPath from "./get-project-path.js";
4-
import getProjectId from "./get-project-id.js";
5-
6-
export default (context, gitlabUrl, gitlabApiUrl, repositoryUrl) => {
7-
const projectId = getProjectId(context);
8-
const projectPath = getProjectPath(context, gitlabUrl, repositoryUrl);
5+
export default ({ envCi: { service } = {}, env: { CI_PROJECT_PATH } }, gitlabUrl, gitlabApiUrl, repositoryUrl) => {
6+
const projectId = service === "gitlab" && CI_PROJECT_ID ? CI_PROJECT_ID : null;
7+
const projectPath =
8+
service === "gitlab" && CI_PROJECT_PATH
9+
? CI_PROJECT_PATH
10+
: parseUrl(repositoryUrl)
11+
.pathname.replace(new RegExp(`^${escapeStringRegexp(parseUrl(gitlabUrl).pathname)}`), "")
12+
.replace(/^\//, "")
13+
.replace(/\/$/, "")
14+
.replace(/\.git$/, "");
915
const encodedProjectPath = encodeURIComponent(projectPath);
1016
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${projectId ?? encodedProjectPath}`);
1117
return {
18+
projectId,
1219
projectPath,
1320
encodedProjectPath,
1421
projectApiUrl,

lib/get-project-id.js

-2
This file was deleted.

lib/get-project-path.js

-11
This file was deleted.

test/get-project-context.test.js

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import test from "ava";
2+
import getProjectContext from "../lib/get-project-context.js";
3+
4+
test("Parse project path with https URL", (t) => {
5+
t.is(
6+
getProjectContext({ env: {} }, "https://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git")
7+
.projectPath,
8+
"owner/repo"
9+
);
10+
t.is(
11+
getProjectContext({ env: {} }, "https://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo"),
12+
"owner/repo"
13+
).projectPath;
14+
});
15+
16+
test("Parse project path with git URL", (t) => {
17+
t.is(
18+
getProjectContext(
19+
{ env: {} },
20+
"https://gitlab.com",
21+
"https://api.gitlab.com",
22+
"git+ssh://[email protected]/owner/repo.git"
23+
),
24+
"owner/repo"
25+
).projectPath;
26+
t.is(
27+
getProjectContext(
28+
{ env: {} },
29+
"https://gitlab.com",
30+
"https://api.gitlab.com",
31+
"git+ssh://[email protected]/owner/repo"
32+
),
33+
"owner/repo"
34+
).projectPath;
35+
});
36+
37+
test("Parse project path with context in repo URL", (t) => {
38+
t.is(
39+
getProjectContext(
40+
{ env: {} },
41+
"https://gitlab.com",
42+
"https://api.gitlab.com",
43+
"https://gitlab.com/context/owner/repo.git"
44+
).projectPath,
45+
"owner/repo"
46+
);
47+
t.is(
48+
getProjectContext(
49+
{ env: {} },
50+
"https://gitlab.com",
51+
"https://api.gitlab.com",
52+
"git+ssh://[email protected]/context/owner/repo.git"
53+
).projectPath,
54+
"owner/repo"
55+
);
56+
});
57+
58+
test("Parse project path with context not in repo URL", (t) => {
59+
t.is(
60+
getProjectContext({ env: {} }, "https://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git"),
61+
"owner/repo"
62+
).projectPath;
63+
t.is(
64+
getProjectContext(
65+
{ env: {} },
66+
"https://gitlab.com",
67+
"https://api.gitlab.com",
68+
"git+ssh://[email protected]/owner/repo.git"
69+
).projectPath,
70+
"owner/repo"
71+
);
72+
});
73+
74+
test("Parse project path with organization and subgroup", (t) => {
75+
t.is(
76+
getProjectContext(
77+
{ env: {} },
78+
"https://gitlab.com",
79+
"https://api.gitlab.com",
80+
"https://gitlab.com/orga/subgroup/owner/repo.git"
81+
).projectPath,
82+
"orga/subgroup/owner/repo"
83+
);
84+
t.is(
85+
getProjectContext(
86+
{ env: {} },
87+
"https://gitlab.com",
88+
"https://api.gitlab.com",
89+
"git+ssh://[email protected]/orga/subgroup/owner/repo.git"
90+
).projectPath,
91+
"orga/subgroup/owner/repo"
92+
);
93+
});
94+
95+
test("Get project path from GitLab CI", (t) => {
96+
t.is(
97+
getProjectContext(
98+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
99+
"https://gitlab.com",
100+
"https://api.gitlab.com",
101+
"https://gitlab.com/owner/repo.git"
102+
).projectPath,
103+
"other-owner/other-repo"
104+
);
105+
});
106+
107+
test("Ignore CI_PROJECT_PATH if not on GitLab CI", (t) => {
108+
t.is(
109+
getProjectContext(
110+
{ envCi: { service: "travis" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
111+
"https://gitlab.com",
112+
"https://api.gitlab.com",
113+
"https://gitlab.com/owner/repo.git"
114+
).projectPath,
115+
"owner/repo"
116+
);
117+
});
118+
119+
test("Get project ID from GitLab CI", (t) => {
120+
t.is(
121+
getProjectContext(
122+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
123+
"https://gitlbab.com",
124+
"https://gitlab.com/owner/repo.git"
125+
).projectId,
126+
"42"
127+
);
128+
});
129+
130+
test("Ignore CI_PROJECT_ID if not on GitLab CI", (t) => {
131+
t.is(
132+
getProjectContext(
133+
{ envCi: { service: "travis" }, env: { CI_PROJECT_ID: "42" } },
134+
"https://gitlbab.com",
135+
"https://gitlab.com/owner/repo.git"
136+
).projectPath,
137+
null
138+
);
139+
});
140+
141+
test("Uses project API URL with project path", (t) => {
142+
t.is(
143+
getProjectContext(
144+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
145+
"https://gitlbab.com",
146+
"https://gitlab.com/owner/repo.git"
147+
).projectApiUrl,
148+
"https://gitlbab.com/projects/other-owner/other-repo"
149+
);
150+
});
151+
152+
test("Uses project API URL with project ID", (t) => {
153+
t.is(
154+
getProjectContext(
155+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
156+
"https://gitlbab.com",
157+
"https://gitlab.com/owner/repo.git"
158+
).projectApiUrl,
159+
"https://gitlbab.com/projects/42"
160+
);
161+
});

test/get-project-path.test.js

-64
This file was deleted.

0 commit comments

Comments
 (0)