Skip to content

Commit

Permalink
refactor: inline getProjectId / getProjectPath functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher committed Jan 12, 2025
1 parent 743ab0a commit 2102ec3
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 83 deletions.
24 changes: 18 additions & 6 deletions lib/get-project-context.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import escapeStringRegexp from "escape-string-regexp";
import parseUrl from "parse-url";
import urlJoin from "url-join";

import getProjectPath from "./get-project-path.js";
import getProjectId from "./get-project-id.js";

export default (context, gitlabUrl, gitlabApiUrl, repositoryUrl) => {
const projectId = getProjectId(context);
const projectPath = getProjectPath(context, gitlabUrl, repositoryUrl);
export default (
{ envCi: { service } = {}, env: { CI_PROJECT_ID, CI_PROJECT_PATH } },
gitlabUrl,
gitlabApiUrl,
repositoryUrl
) => {
const projectId = service === "gitlab" && CI_PROJECT_ID ? CI_PROJECT_ID : null;
const projectPath =
service === "gitlab" && CI_PROJECT_PATH
? CI_PROJECT_PATH
: parseUrl(repositoryUrl)
.pathname.replace(new RegExp(`^${escapeStringRegexp(parseUrl(gitlabUrl).pathname)}`), "")
.replace(/^\//, "")
.replace(/\/$/, "")
.replace(/\.git$/, "");
const encodedProjectPath = encodeURIComponent(projectPath);
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${projectId ?? encodedProjectPath}`);
return {
projectId,
projectPath,
encodedProjectPath,
projectApiUrl,
Expand Down
2 changes: 0 additions & 2 deletions lib/get-project-id.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/get-project-path.js

This file was deleted.

170 changes: 170 additions & 0 deletions test/get-project-context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import test from "ava";
import getProjectContext from "../lib/get-project-context.js";

test("Parse project path with https URL", (t) => {
t.is(
getProjectContext({ env: {} }, "https://gitlbab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git")
.projectPath,
"owner/repo"
);
t.is(
getProjectContext({ env: {} }, "https://gitlbab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo").projectPath,
"owner/repo"
);
});

test("Parse project path with git URL", (t) => {
t.is(
getProjectContext(
{ env: {} },
"https://gitlab.com",
"https://api.gitlab.com",
"git+ssh://[email protected]/owner/repo.git"
).projectPath,
"owner/repo"
);
t.is(
getProjectContext(
{ env: {} },
"https://gitlab.com",
"https://api.gitlab.com",
"git+ssh://[email protected]/owner/repo"
).projectPath,
"owner/repo"
);
});

test("Parse project path with context in repo URL", (t) => {
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"https://gitlab.com/context/owner/repo.git"
).projectPath,
"owner/repo"
);
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"git+ssh://[email protected]/context/owner/repo.git"
).projectPath,
"owner/repo"
);
});

test("Parse project path with context not in repo URL", (t) => {
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectPath,
"owner/repo"
);
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"git+ssh://[email protected]/owner/repo.git"
).projectPath,
"owner/repo"
);
});

test("Parse project path with organization and subgroup", (t) => {
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"https://gitlab.com/orga/subgroup/owner/repo.git"
).projectPath,
"orga/subgroup/owner/repo"
);
t.is(
getProjectContext(
{ env: {} },
"https://gitlbab.com/context",
"https://api.gitlab.com",
"git+ssh://[email protected]/orga/subgroup/owner/repo.git"
).projectPath,
"orga/subgroup/owner/repo"
);
});

test("Get project path from GitLab CI", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
"https://gitlbab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectPath,
"other-owner/other-repo"
);
});

test("Ignore CI_PROJECT_PATH if not on GitLab CI", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "travis" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
"https://gitlbab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectPath,
"owner/repo"
);
});

test("Get project ID from GitLab CI", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
"https://gitlbab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectId,
"42"
);
});

test("Ignore CI_PROJECT_ID if not on GitLab CI", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "travis" }, env: { CI_PROJECT_ID: "42" } },
"https://gitlbab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectId,
null
);
});

test("Uses project API URL with project path", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } },
"https://gitlab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectApiUrl,
"https://api.gitlab.com/projects/other-owner%2Fother-repo"
);
});

test("Uses project API URL with project ID", (t) => {
t.is(
getProjectContext(
{ envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } },
"https://gitlab.com",
"https://api.gitlab.com",
"https://gitlab.com/owner/repo.git"
).projectApiUrl,
"https://api.gitlab.com/projects/42"
);
});
64 changes: 0 additions & 64 deletions test/get-project-path.test.js

This file was deleted.

0 comments on commit 2102ec3

Please sign in to comment.