Skip to content

Commit 2102ec3

Browse files
committed
refactor: inline getProjectId / getProjectPath functions
1 parent 743ab0a commit 2102ec3

5 files changed

+188
-83
lines changed

lib/get-project-context.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
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 (
6+
{ envCi: { service } = {}, env: { CI_PROJECT_ID, CI_PROJECT_PATH } },
7+
gitlabUrl,
8+
gitlabApiUrl,
9+
repositoryUrl
10+
) => {
11+
const projectId = service === "gitlab" && CI_PROJECT_ID ? CI_PROJECT_ID : null;
12+
const projectPath =
13+
service === "gitlab" && CI_PROJECT_PATH
14+
? CI_PROJECT_PATH
15+
: parseUrl(repositoryUrl)
16+
.pathname.replace(new RegExp(`^${escapeStringRegexp(parseUrl(gitlabUrl).pathname)}`), "")
17+
.replace(/^\//, "")
18+
.replace(/\/$/, "")
19+
.replace(/\.git$/, "");
920
const encodedProjectPath = encodeURIComponent(projectPath);
1021
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${projectId ?? encodedProjectPath}`);
1122
return {
23+
projectId,
1224
projectPath,
1325
encodedProjectPath,
1426
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

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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://gitlbab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git")
7+
.projectPath,
8+
"owner/repo"
9+
);
10+
t.is(
11+
getProjectContext({ env: {} }, "https://gitlbab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo").projectPath,
12+
"owner/repo"
13+
);
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+
).projectPath,
24+
"owner/repo"
25+
);
26+
t.is(
27+
getProjectContext(
28+
{ env: {} },
29+
"https://gitlab.com",
30+
"https://api.gitlab.com",
31+
"git+ssh://[email protected]/owner/repo"
32+
).projectPath,
33+
"owner/repo"
34+
);
35+
});
36+
37+
test("Parse project path with context in repo URL", (t) => {
38+
t.is(
39+
getProjectContext(
40+
{ env: {} },
41+
"https://gitlbab.com/context",
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://gitlbab.com/context",
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(
61+
{ env: {} },
62+
"https://gitlbab.com/context",
63+
"https://api.gitlab.com",
64+
"https://gitlab.com/owner/repo.git"
65+
).projectPath,
66+
"owner/repo"
67+
);
68+
t.is(
69+
getProjectContext(
70+
{ env: {} },
71+
"https://gitlbab.com/context",
72+
"https://api.gitlab.com",
73+
"git+ssh://[email protected]/owner/repo.git"
74+
).projectPath,
75+
"owner/repo"
76+
);
77+
});
78+
79+
test("Parse project path with organization and subgroup", (t) => {
80+
t.is(
81+
getProjectContext(
82+
{ env: {} },
83+
"https://gitlbab.com/context",
84+
"https://api.gitlab.com",
85+
"https://gitlab.com/orga/subgroup/owner/repo.git"
86+
).projectPath,
87+
"orga/subgroup/owner/repo"
88+
);
89+
t.is(
90+
getProjectContext(
91+
{ env: {} },
92+
"https://gitlbab.com/context",
93+
"https://api.gitlab.com",
94+
"git+ssh://[email protected]/orga/subgroup/owner/repo.git"
95+
).projectPath,
96+
"orga/subgroup/owner/repo"
97+
);
98+
});
99+
100+
test("Get project path from GitLab CI", (t) => {
101+
t.is(
102+
getProjectContext(
103+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
104+
"https://gitlbab.com",
105+
"https://api.gitlab.com",
106+
"https://gitlab.com/owner/repo.git"
107+
).projectPath,
108+
"other-owner/other-repo"
109+
);
110+
});
111+
112+
test("Ignore CI_PROJECT_PATH if not on GitLab CI", (t) => {
113+
t.is(
114+
getProjectContext(
115+
{ envCi: { service: "travis" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
116+
"https://gitlbab.com",
117+
"https://api.gitlab.com",
118+
"https://gitlab.com/owner/repo.git"
119+
).projectPath,
120+
"owner/repo"
121+
);
122+
});
123+
124+
test("Get project ID from GitLab CI", (t) => {
125+
t.is(
126+
getProjectContext(
127+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
128+
"https://gitlbab.com",
129+
"https://api.gitlab.com",
130+
"https://gitlab.com/owner/repo.git"
131+
).projectId,
132+
"42"
133+
);
134+
});
135+
136+
test("Ignore CI_PROJECT_ID if not on GitLab CI", (t) => {
137+
t.is(
138+
getProjectContext(
139+
{ envCi: { service: "travis" }, env: { CI_PROJECT_ID: "42" } },
140+
"https://gitlbab.com",
141+
"https://api.gitlab.com",
142+
"https://gitlab.com/owner/repo.git"
143+
).projectId,
144+
null
145+
);
146+
});
147+
148+
test("Uses project API URL with project path", (t) => {
149+
t.is(
150+
getProjectContext(
151+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
152+
"https://gitlab.com",
153+
"https://api.gitlab.com",
154+
"https://gitlab.com/owner/repo.git"
155+
).projectApiUrl,
156+
"https://api.gitlab.com/projects/other-owner%2Fother-repo"
157+
);
158+
});
159+
160+
test("Uses project API URL with project ID", (t) => {
161+
t.is(
162+
getProjectContext(
163+
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
164+
"https://gitlab.com",
165+
"https://api.gitlab.com",
166+
"https://gitlab.com/owner/repo.git"
167+
).projectApiUrl,
168+
"https://api.gitlab.com/projects/42"
169+
);
170+
});

test/get-project-path.test.js

-64
This file was deleted.

0 commit comments

Comments
 (0)