@checkstack/gitops-common 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/package.json +4 -4
- package/src/index.ts +4 -0
- package/src/provenance-types.ts +6 -0
- package/src/source-url.test.ts +152 -0
- package/src/source-url.ts +89 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @checkstack/gitops-common
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f6f9a5c: Surface the source repository for GitOps-managed entities and gate the
|
|
8
|
+
system→group remove button on the system's lock state.
|
|
9
|
+
|
|
10
|
+
- `provenanceSchema` now carries a `sourceUrl` field, derived on the
|
|
11
|
+
backend from the provider type, baseUrl, repository and filePath. URLs
|
|
12
|
+
are constructed for github.com / gitlab.com and self-hosted
|
|
13
|
+
GitHub/GitLab where the API base ends in `/api/v3` or `/api/v4`. Other
|
|
14
|
+
baseUrls fall back to `null` so the UI keeps showing the raw path.
|
|
15
|
+
- New `useProvenanceLocks` hook (bulk variant of `useProvenanceLock`)
|
|
16
|
+
for views that render many entities and need to look up locks
|
|
17
|
+
client-side.
|
|
18
|
+
- New `<GitOpsSourceBadge>` popover component that replaces the bare
|
|
19
|
+
GitBranch icon on system and group catalog cards. The popover
|
|
20
|
+
surfaces the repository, file path, and a "View in source provider"
|
|
21
|
+
deep link.
|
|
22
|
+
- `<GitOpsLockBanner>` repo line is now a real link when a sourceUrl is
|
|
23
|
+
available.
|
|
24
|
+
- The system→group remove button in the catalog now disables itself
|
|
25
|
+
when the system is GitOps-managed, matching the backend lock that was
|
|
26
|
+
already in place.
|
|
27
|
+
|
|
28
|
+
### Patch Changes
|
|
29
|
+
|
|
30
|
+
- Updated dependencies [42abfff]
|
|
31
|
+
- @checkstack/common@0.9.0
|
|
32
|
+
|
|
3
33
|
## 0.2.2
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@checkstack/gitops-common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@checkstack/common": "0.
|
|
12
|
+
"@checkstack/common": "0.8.0",
|
|
13
13
|
"@orpc/contract": "^1.13.14",
|
|
14
14
|
"zod": "^4.2.1"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"typescript": "^5.7.2",
|
|
18
|
-
"@checkstack/tsconfig": "0.0.
|
|
19
|
-
"@checkstack/scripts": "0.
|
|
18
|
+
"@checkstack/tsconfig": "0.0.7",
|
|
19
|
+
"@checkstack/scripts": "0.3.0"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"typecheck": "tsgo -b",
|
package/src/index.ts
CHANGED
package/src/provenance-types.ts
CHANGED
|
@@ -16,6 +16,12 @@ export const provenanceSchema = z.object({
|
|
|
16
16
|
providerId: z.string(),
|
|
17
17
|
repository: z.string(),
|
|
18
18
|
filePath: z.string(),
|
|
19
|
+
/**
|
|
20
|
+
* Browsable web URL pointing to the entity's defining file in its source
|
|
21
|
+
* provider, derived from the provider type, baseUrl, repository and filePath.
|
|
22
|
+
* Null when the URL cannot be safely derived (unknown baseUrl shape, etc.).
|
|
23
|
+
*/
|
|
24
|
+
sourceUrl: z.string().nullable(),
|
|
19
25
|
lastSyncHash: z.string(),
|
|
20
26
|
status: provenanceStatusSchema,
|
|
21
27
|
errorMessage: z.string().nullable(),
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { deriveSourceUrl } from "./source-url";
|
|
3
|
+
|
|
4
|
+
describe("deriveSourceUrl", () => {
|
|
5
|
+
test("public github.com", () => {
|
|
6
|
+
expect(
|
|
7
|
+
deriveSourceUrl({
|
|
8
|
+
providerType: "github",
|
|
9
|
+
baseUrl: null,
|
|
10
|
+
repository: "acme/checkstack",
|
|
11
|
+
filePath: "catalog/system.yaml",
|
|
12
|
+
}),
|
|
13
|
+
).toBe("https://github.com/acme/checkstack/blob/HEAD/catalog/system.yaml");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("public gitlab.com", () => {
|
|
17
|
+
expect(
|
|
18
|
+
deriveSourceUrl({
|
|
19
|
+
providerType: "gitlab",
|
|
20
|
+
baseUrl: null,
|
|
21
|
+
repository: "acme/checkstack",
|
|
22
|
+
filePath: "catalog/system.yaml",
|
|
23
|
+
}),
|
|
24
|
+
).toBe(
|
|
25
|
+
"https://gitlab.com/acme/checkstack/-/blob/HEAD/catalog/system.yaml",
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("api.github.com baseUrl maps to public host", () => {
|
|
30
|
+
expect(
|
|
31
|
+
deriveSourceUrl({
|
|
32
|
+
providerType: "github",
|
|
33
|
+
baseUrl: "https://api.github.com",
|
|
34
|
+
repository: "acme/checkstack",
|
|
35
|
+
filePath: "catalog/system.yaml",
|
|
36
|
+
}),
|
|
37
|
+
).toBe("https://github.com/acme/checkstack/blob/HEAD/catalog/system.yaml");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("github enterprise baseUrl strips /api/v3", () => {
|
|
41
|
+
expect(
|
|
42
|
+
deriveSourceUrl({
|
|
43
|
+
providerType: "github",
|
|
44
|
+
baseUrl: "https://github.example.com/api/v3",
|
|
45
|
+
repository: "acme/checkstack",
|
|
46
|
+
filePath: "catalog/system.yaml",
|
|
47
|
+
}),
|
|
48
|
+
).toBe(
|
|
49
|
+
"https://github.example.com/acme/checkstack/blob/HEAD/catalog/system.yaml",
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("self-hosted gitlab baseUrl strips /api/v4", () => {
|
|
54
|
+
expect(
|
|
55
|
+
deriveSourceUrl({
|
|
56
|
+
providerType: "gitlab",
|
|
57
|
+
baseUrl: "https://gitlab.example.com/api/v4",
|
|
58
|
+
repository: "team/sub/checkstack",
|
|
59
|
+
filePath: "catalog/system.yaml",
|
|
60
|
+
}),
|
|
61
|
+
).toBe(
|
|
62
|
+
"https://gitlab.example.com/team/sub/checkstack/-/blob/HEAD/catalog/system.yaml",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("nested gitlab namespaces preserved", () => {
|
|
67
|
+
expect(
|
|
68
|
+
deriveSourceUrl({
|
|
69
|
+
providerType: "gitlab",
|
|
70
|
+
baseUrl: null,
|
|
71
|
+
repository: "group/sub/project",
|
|
72
|
+
filePath: "a/b.yaml",
|
|
73
|
+
}),
|
|
74
|
+
).toBe("https://gitlab.com/group/sub/project/-/blob/HEAD/a/b.yaml");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("encodes spaces and special characters in path segments", () => {
|
|
78
|
+
expect(
|
|
79
|
+
deriveSourceUrl({
|
|
80
|
+
providerType: "github",
|
|
81
|
+
baseUrl: null,
|
|
82
|
+
repository: "acme/checkstack",
|
|
83
|
+
filePath: "catalog/my system.yaml",
|
|
84
|
+
}),
|
|
85
|
+
).toBe(
|
|
86
|
+
"https://github.com/acme/checkstack/blob/HEAD/catalog/my%20system.yaml",
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("strips leading slashes from filePath", () => {
|
|
91
|
+
expect(
|
|
92
|
+
deriveSourceUrl({
|
|
93
|
+
providerType: "github",
|
|
94
|
+
baseUrl: null,
|
|
95
|
+
repository: "acme/checkstack",
|
|
96
|
+
filePath: "/catalog/system.yaml",
|
|
97
|
+
}),
|
|
98
|
+
).toBe("https://github.com/acme/checkstack/blob/HEAD/catalog/system.yaml");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("returns null for unknown self-hosted baseUrl", () => {
|
|
102
|
+
expect(
|
|
103
|
+
deriveSourceUrl({
|
|
104
|
+
providerType: "github",
|
|
105
|
+
baseUrl: "https://github.example.com/some/weird/path",
|
|
106
|
+
repository: "acme/checkstack",
|
|
107
|
+
filePath: "catalog/system.yaml",
|
|
108
|
+
}),
|
|
109
|
+
).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("returns null for traversal attempts", () => {
|
|
113
|
+
expect(
|
|
114
|
+
deriveSourceUrl({
|
|
115
|
+
providerType: "github",
|
|
116
|
+
baseUrl: null,
|
|
117
|
+
repository: "acme/checkstack",
|
|
118
|
+
filePath: "../etc/passwd",
|
|
119
|
+
}),
|
|
120
|
+
).toBeNull();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("returns null for empty inputs", () => {
|
|
124
|
+
expect(
|
|
125
|
+
deriveSourceUrl({
|
|
126
|
+
providerType: "github",
|
|
127
|
+
baseUrl: null,
|
|
128
|
+
repository: "",
|
|
129
|
+
filePath: "x.yaml",
|
|
130
|
+
}),
|
|
131
|
+
).toBeNull();
|
|
132
|
+
expect(
|
|
133
|
+
deriveSourceUrl({
|
|
134
|
+
providerType: "github",
|
|
135
|
+
baseUrl: null,
|
|
136
|
+
repository: "a/b",
|
|
137
|
+
filePath: "",
|
|
138
|
+
}),
|
|
139
|
+
).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("returns null for malformed baseUrl", () => {
|
|
143
|
+
expect(
|
|
144
|
+
deriveSourceUrl({
|
|
145
|
+
providerType: "github",
|
|
146
|
+
baseUrl: "not a url",
|
|
147
|
+
repository: "acme/checkstack",
|
|
148
|
+
filePath: "x.yaml",
|
|
149
|
+
}),
|
|
150
|
+
).toBeNull();
|
|
151
|
+
});
|
|
152
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derive a human-browsable source URL for a GitOps-managed entity.
|
|
3
|
+
*
|
|
4
|
+
* Returns null when we don't have enough information to construct a stable URL
|
|
5
|
+
* (e.g., unknown provider type or a malformed `repository` value). Callers
|
|
6
|
+
* should fall back to displaying the raw `repository`/`filePath` text.
|
|
7
|
+
*
|
|
8
|
+
* The URL points at the file on the default branch via the `HEAD` ref so we
|
|
9
|
+
* don't need to know the branch name. Both GitHub and GitLab resolve `HEAD`
|
|
10
|
+
* server-side for blob URLs.
|
|
11
|
+
*/
|
|
12
|
+
export interface DeriveSourceUrlInput {
|
|
13
|
+
providerType: "github" | "gitlab";
|
|
14
|
+
/** The provider's `baseUrl` (API base) — null for public github.com/gitlab.com. */
|
|
15
|
+
baseUrl: string | null;
|
|
16
|
+
/** GitHub: `owner/repo`. GitLab: `group/project` (or nested `group/sub/project`). */
|
|
17
|
+
repository: string;
|
|
18
|
+
filePath: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const GITHUB_PUBLIC_HOST = "https://github.com";
|
|
22
|
+
const GITLAB_PUBLIC_HOST = "https://gitlab.com";
|
|
23
|
+
|
|
24
|
+
const stripApiSuffix = ({
|
|
25
|
+
providerType,
|
|
26
|
+
baseUrl,
|
|
27
|
+
}: {
|
|
28
|
+
providerType: "github" | "gitlab";
|
|
29
|
+
baseUrl: string;
|
|
30
|
+
}): string | null => {
|
|
31
|
+
// We only know how to strip the well-known API path suffixes. Anything else
|
|
32
|
+
// we treat as untrusted and bail out — better to fall back to text than to
|
|
33
|
+
// construct a broken link.
|
|
34
|
+
try {
|
|
35
|
+
const url = new URL(baseUrl);
|
|
36
|
+
if (providerType === "github") {
|
|
37
|
+
// api.github.com → github.com (public host has no API path component)
|
|
38
|
+
if (url.host === "api.github.com") return GITHUB_PUBLIC_HOST;
|
|
39
|
+
// GitHub Enterprise: https://github.example.com/api/v3
|
|
40
|
+
if (url.pathname.replace(/\/$/, "") === "/api/v3") {
|
|
41
|
+
return `${url.protocol}//${url.host}`;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
// GitLab: https://gitlab.example.com/api/v4
|
|
46
|
+
if (url.pathname.replace(/\/$/, "") === "/api/v4") {
|
|
47
|
+
return `${url.protocol}//${url.host}`;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const encodeFilePath = (filePath: string) =>
|
|
56
|
+
filePath
|
|
57
|
+
.replace(/^\/+/, "")
|
|
58
|
+
.split("/")
|
|
59
|
+
.map((segment) => encodeURIComponent(segment))
|
|
60
|
+
.join("/");
|
|
61
|
+
|
|
62
|
+
export const deriveSourceUrl = ({
|
|
63
|
+
providerType,
|
|
64
|
+
baseUrl,
|
|
65
|
+
repository,
|
|
66
|
+
filePath,
|
|
67
|
+
}: DeriveSourceUrlInput): string | null => {
|
|
68
|
+
if (!repository || !filePath) return null;
|
|
69
|
+
// Reject backslashes and absolute-looking paths to keep things tidy.
|
|
70
|
+
if (repository.includes("..") || filePath.includes("..")) return null;
|
|
71
|
+
|
|
72
|
+
const host = baseUrl
|
|
73
|
+
? stripApiSuffix({ providerType, baseUrl })
|
|
74
|
+
: providerType === "github"
|
|
75
|
+
? GITHUB_PUBLIC_HOST
|
|
76
|
+
: GITLAB_PUBLIC_HOST;
|
|
77
|
+
if (!host) return null;
|
|
78
|
+
|
|
79
|
+
const repoPath = repository
|
|
80
|
+
.split("/")
|
|
81
|
+
.map((segment) => encodeURIComponent(segment))
|
|
82
|
+
.join("/");
|
|
83
|
+
const encodedFile = encodeFilePath(filePath);
|
|
84
|
+
|
|
85
|
+
if (providerType === "github") {
|
|
86
|
+
return `${host}/${repoPath}/blob/HEAD/${encodedFile}`;
|
|
87
|
+
}
|
|
88
|
+
return `${host}/${repoPath}/-/blob/HEAD/${encodedFile}`;
|
|
89
|
+
};
|