|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
2 | | -import { getRepositoryId, clearRepositoryIdCache } from "./repository-id"; |
| 2 | +import { getOrgId, clearOrgIdCache } from "./org-id"; |
3 | 3 | import { execSync } from "child_process"; |
4 | 4 |
|
5 | 5 | vi.mock("child_process"); |
6 | 6 |
|
7 | | -describe("getRepositoryId", () => { |
| 7 | +describe("getOrgId", () => { |
8 | 8 | const originalEnv = process.env; |
9 | 9 |
|
10 | 10 | beforeEach(() => { |
11 | 11 | process.env = { ...originalEnv }; |
12 | 12 | vi.clearAllMocks(); |
13 | | - clearRepositoryIdCache(); // Clear cache between tests |
| 13 | + clearOrgIdCache(); // Clear cache between tests |
14 | 14 | }); |
15 | 15 |
|
16 | 16 | afterEach(() => { |
17 | 17 | process.env = originalEnv; |
18 | 18 | }); |
19 | 19 |
|
20 | 20 | describe("CI environment variables", () => { |
21 | | - it("should detect GitHub repository from GITHUB_REPOSITORY", () => { |
| 21 | + it("should detect GitHub org from GITHUB_REPOSITORY", () => { |
22 | 22 | process.env.GITHUB_REPOSITORY = "owner/repo"; |
23 | | - expect(getRepositoryId()).toBe("github:owner/071ca222"); |
| 23 | + expect(getOrgId()).toBe("github:owner"); |
24 | 24 | }); |
25 | 25 |
|
26 | | - it("should detect GitLab repository from CI_PROJECT_PATH", () => { |
| 26 | + it("should detect GitLab org from CI_PROJECT_PATH", () => { |
27 | 27 | process.env.CI_PROJECT_PATH = "namespace/project"; |
28 | | - expect(getRepositoryId()).toBe("gitlab:namespace/244210e4"); |
| 28 | + expect(getOrgId()).toBe("gitlab:namespace"); |
29 | 29 | }); |
30 | 30 |
|
31 | | - it("should detect Bitbucket repository from BITBUCKET_REPO_FULL_NAME", () => { |
| 31 | + it("should detect Bitbucket org from BITBUCKET_REPO_FULL_NAME", () => { |
32 | 32 | process.env.BITBUCKET_REPO_FULL_NAME = "workspace/repo"; |
33 | | - expect(getRepositoryId()).toBe("bitbucket:workspace/071ca222"); |
| 33 | + expect(getOrgId()).toBe("bitbucket:workspace"); |
34 | 34 | }); |
35 | 35 |
|
36 | 36 | it("should prioritize CI environment variables over git remote", () => { |
37 | 37 | process.env.GITHUB_REPOSITORY = "owner/repo"; |
38 | 38 | vi.mocked(execSync).mockReturnValue( |
39 | 39 | "git@gitlab.com:other/project.git" as any, |
40 | 40 | ); |
41 | | - expect(getRepositoryId()).toBe("github:owner/071ca222"); |
| 41 | + expect(getOrgId()).toBe("github:owner"); |
42 | 42 | }); |
43 | 43 | }); |
44 | 44 |
|
45 | 45 | describe("git remote URL parsing", () => { |
46 | 46 | it("should parse GitHub SSH URL", () => { |
47 | 47 | vi.mocked(execSync).mockReturnValue("git@github.com:owner/repo.git" as any); |
48 | | - expect(getRepositoryId()).toBe("github:owner/071ca222"); |
| 48 | + expect(getOrgId()).toBe("github:owner"); |
49 | 49 | }); |
50 | 50 |
|
51 | 51 | it("should parse GitHub HTTPS URL", () => { |
52 | 52 | vi.mocked(execSync).mockReturnValue( |
53 | 53 | "https://github.com/owner/repo.git" as any, |
54 | 54 | ); |
55 | | - expect(getRepositoryId()).toBe("github:owner/071ca222"); |
| 55 | + expect(getOrgId()).toBe("github:owner"); |
56 | 56 | }); |
57 | 57 |
|
58 | 58 | it("should parse GitLab SSH URL", () => { |
59 | 59 | vi.mocked(execSync).mockReturnValue( |
60 | 60 | "git@gitlab.com:namespace/project.git" as any, |
61 | 61 | ); |
62 | | - expect(getRepositoryId()).toBe("gitlab:namespace/244210e4"); |
| 62 | + expect(getOrgId()).toBe("gitlab:namespace"); |
63 | 63 | }); |
64 | 64 |
|
65 | 65 | it("should parse GitLab HTTPS URL", () => { |
66 | 66 | vi.mocked(execSync).mockReturnValue( |
67 | 67 | "https://gitlab.com/namespace/project.git" as any, |
68 | 68 | ); |
69 | | - expect(getRepositoryId()).toBe("gitlab:namespace/244210e4"); |
| 69 | + expect(getOrgId()).toBe("gitlab:namespace"); |
70 | 70 | }); |
71 | 71 |
|
72 | 72 | it("should parse Bitbucket SSH URL", () => { |
73 | 73 | vi.mocked(execSync).mockReturnValue( |
74 | 74 | "git@bitbucket.org:workspace/repo.git" as any, |
75 | 75 | ); |
76 | | - expect(getRepositoryId()).toBe("bitbucket:workspace/071ca222"); |
| 76 | + expect(getOrgId()).toBe("bitbucket:workspace"); |
77 | 77 | }); |
78 | 78 |
|
79 | 79 | it("should parse Bitbucket HTTPS URL", () => { |
80 | 80 | vi.mocked(execSync).mockReturnValue( |
81 | 81 | "https://bitbucket.org/workspace/repo.git" as any, |
82 | 82 | ); |
83 | | - expect(getRepositoryId()).toBe("bitbucket:workspace/071ca222"); |
| 83 | + expect(getOrgId()).toBe("bitbucket:workspace"); |
84 | 84 | }); |
85 | 85 |
|
86 | 86 | it("should parse self-hosted git URL with generic prefix", () => { |
87 | 87 | vi.mocked(execSync).mockReturnValue( |
88 | 88 | "git@custom-git.company.com:team/project.git" as any, |
89 | 89 | ); |
90 | | - expect(getRepositoryId()).toBe("git:team/244210e4"); |
| 90 | + expect(getOrgId()).toBe("git:team"); |
91 | 91 | }); |
92 | 92 |
|
93 | 93 | it("should handle URLs without .git extension", () => { |
94 | 94 | vi.mocked(execSync).mockReturnValue("git@github.com:owner/repo" as any); |
95 | | - expect(getRepositoryId()).toBe("github:owner/071ca222"); |
| 95 | + expect(getOrgId()).toBe("github:owner"); |
96 | 96 | }); |
97 | 97 |
|
98 | 98 | it("should return null when git command fails", () => { |
99 | 99 | vi.mocked(execSync).mockImplementation(() => { |
100 | 100 | throw new Error("not a git repository"); |
101 | 101 | }); |
102 | | - expect(getRepositoryId()).toBe(null); |
| 102 | + expect(getOrgId()).toBe(null); |
103 | 103 | }); |
104 | 104 |
|
105 | 105 | it("should return null when git remote is empty", () => { |
106 | 106 | vi.mocked(execSync).mockReturnValue("" as any); |
107 | | - expect(getRepositoryId()).toBe(null); |
| 107 | + expect(getOrgId()).toBe(null); |
108 | 108 | }); |
109 | 109 |
|
110 | 110 | it("should return null when git remote URL is invalid", () => { |
111 | 111 | vi.mocked(execSync).mockReturnValue("invalid-url" as any); |
112 | | - expect(getRepositoryId()).toBe(null); |
| 112 | + expect(getOrgId()).toBe(null); |
113 | 113 | }); |
114 | 114 | }); |
115 | 115 | }); |
0 commit comments