diff --git a/lib/get-project-context.js b/lib/get-project-context.js index 5844df0b..4139afaf 100644 --- a/lib/get-project-context.js +++ b/lib/get-project-context.js @@ -1,14 +1,21 @@ +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_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, diff --git a/lib/get-project-id.js b/lib/get-project-id.js deleted file mode 100644 index 8062acff..00000000 --- a/lib/get-project-id.js +++ /dev/null @@ -1,2 +0,0 @@ -export default ({ envCi: { service } = {}, env: { CI_PROJECT_ID } }) => - service === "gitlab" && CI_PROJECT_ID ? CI_PROJECT_ID : null; diff --git a/lib/get-project-path.js b/lib/get-project-path.js deleted file mode 100644 index f56cf82b..00000000 --- a/lib/get-project-path.js +++ /dev/null @@ -1,11 +0,0 @@ -import parseUrl from "parse-url"; -import escapeStringRegexp from "escape-string-regexp"; - -export default ({ envCi: { service } = {}, env: { CI_PROJECT_PATH } }, gitlabUrl, repositoryUrl) => - service === "gitlab" && CI_PROJECT_PATH - ? CI_PROJECT_PATH - : parseUrl(repositoryUrl) - .pathname.replace(new RegExp(`^${escapeStringRegexp(parseUrl(gitlabUrl).pathname)}`), "") - .replace(/^\//, "") - .replace(/\/$/, "") - .replace(/\.git$/, ""); diff --git a/test/get-project-context.test.js b/test/get-project-context.test.js new file mode 100644 index 00000000..968ea74b --- /dev/null +++ b/test/get-project-context.test.js @@ -0,0 +1,161 @@ +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://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git") + .projectPath, + "owner/repo" + ); + t.is( + getProjectContext({ env: {} }, "https://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo"), + "owner/repo" + ).projectPath; +}); + +test("Parse project path with git URL", (t) => { + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "git+ssh://git@gitlab.com/owner/repo.git" + ), + "owner/repo" + ).projectPath; + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "git+ssh://git@gitlab.com/owner/repo" + ), + "owner/repo" + ).projectPath; +}); + +test("Parse project path with context in repo URL", (t) => { + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "https://gitlab.com/context/owner/repo.git" + ).projectPath, + "owner/repo" + ); + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "git+ssh://git@gitlab.com/context/owner/repo.git" + ).projectPath, + "owner/repo" + ); +}); + +test("Parse project path with context not in repo URL", (t) => { + t.is( + getProjectContext({ env: {} }, "https://gitlab.com", "https://api.gitlab.com", "https://gitlab.com/owner/repo.git"), + "owner/repo" + ).projectPath; + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "git+ssh://git@gitlab.com/owner/repo.git" + ).projectPath, + "owner/repo" + ); +}); + +test("Parse project path with organization and subgroup", (t) => { + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "https://gitlab.com/orga/subgroup/owner/repo.git" + ).projectPath, + "orga/subgroup/owner/repo" + ); + t.is( + getProjectContext( + { env: {} }, + "https://gitlab.com", + "https://api.gitlab.com", + "git+ssh://git@gitlab.com/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://gitlab.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://gitlab.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://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://gitlab.com/owner/repo.git" + ).projectPath, + 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://gitlbab.com", + "https://gitlab.com/owner/repo.git" + ).projectApiUrl, + "https://gitlbab.com/projects/other-owner/other-repo" + ); +}); + +test("Uses project API URL with project ID", (t) => { + t.is( + getProjectContext( + { envCi: { service: "gitlab" }, env: { CI_PROJECT_ID: "42" } }, + "https://gitlbab.com", + "https://gitlab.com/owner/repo.git" + ).projectApiUrl, + "https://gitlbab.com/projects/42" + ); +}); diff --git a/test/get-project-path.test.js b/test/get-project-path.test.js deleted file mode 100644 index 23825973..00000000 --- a/test/get-project-path.test.js +++ /dev/null @@ -1,64 +0,0 @@ -import test from "ava"; -import getProjectPath from "../lib/get-project-path.js"; - -test("Parse repo id with https URL", (t) => { - t.is(getProjectPath({ env: {} }, "https://gitlbab.com", "https://gitlab.com/owner/repo.git"), "owner/repo"); - t.is(getProjectPath({ env: {} }, "https://gitlbab.com", "https://gitlab.com/owner/repo"), "owner/repo"); -}); - -test("Parse repo id with git URL", (t) => { - t.is(getProjectPath({ env: {} }, "https://gitlab.com", "git+ssh://git@gitlab.com/owner/repo.git"), "owner/repo"); - t.is(getProjectPath({ env: {} }, "https://gitlab.com", "git+ssh://git@gitlab.com/owner/repo"), "owner/repo"); -}); - -test("Parse repo id with context in repo URL", (t) => { - t.is( - getProjectPath({ env: {} }, "https://gitlbab.com/context", "https://gitlab.com/context/owner/repo.git"), - "owner/repo" - ); - t.is( - getProjectPath({ env: {} }, "https://gitlab.com/context", "git+ssh://git@gitlab.com/context/owner/repo.git"), - "owner/repo" - ); -}); - -test("Parse repo id with context not in repo URL", (t) => { - t.is(getProjectPath({ env: {} }, "https://gitlbab.com/context", "https://gitlab.com/owner/repo.git"), "owner/repo"); - t.is( - getProjectPath({ env: {} }, "https://gitlab.com/context", "git+ssh://git@gitlab.com/owner/repo.git"), - "owner/repo" - ); -}); - -test("Parse repo id with organization and subgroup", (t) => { - t.is( - getProjectPath({ env: {} }, "https://gitlbab.com/context", "https://gitlab.com/orga/subgroup/owner/repo.git"), - "orga/subgroup/owner/repo" - ); - t.is( - getProjectPath({ env: {} }, "https://gitlab.com/context", "git+ssh://git@gitlab.com/orga/subgroup/owner/repo.git"), - "orga/subgroup/owner/repo" - ); -}); - -test("Get repo id from GitLab CI", (t) => { - t.is( - getProjectPath( - { envCi: { service: "gitlab" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } }, - "https://gitlbab.com", - "https://gitlab.com/owner/repo.git" - ), - "other-owner/other-repo" - ); -}); - -test("Ignore CI_PROJECT_PATH if not on GitLab CI", (t) => { - t.is( - getProjectPath( - { envCi: { service: "travis" }, env: { CI_PROJECT_PATH: "other-owner/other-repo" } }, - "https://gitlbab.com", - "https://gitlab.com/owner/repo.git" - ), - "owner/repo" - ); -});