MCPcopy
hub / github.com/firecow/gitlab-ci-local / _generateEnvironmentSlug

Method _generateEnvironmentSlug

src/job.ts:373–409  ·  view source on GitHub ↗

* Generates a compliant slug for an environment name. * See: https://gitlab.com/gitlab-org/gitlab/-/blob/fc31e7ac344e53ebae182ea1dca183bdc0e2ea71/lib/gitlab/slug/environment.rb * * The slug: * - Contains only lowercase letters (a-z), numbers (0-9), and '-'. * - Begins with a

(name: string)

Source from the content-addressed store, hash-verified

371 * @returns A compliant environment slug.
372 */
373 private _generateEnvironmentSlug (name: string): string {
374 // 1. Lowercase, replace non-alphanumeric with '-', and squeeze repeating '-'
375 let slug = name
376 .toLowerCase()
377 .replaceAll(/[^a-z0-9]/g, "-")
378 .replaceAll(/-+/g, "-");
379
380 // 2. Must start with a letter
381 if (!/^[a-z]/.test(slug)) {
382 slug = `env-${slug}`;
383 }
384
385 // 3. If it's too long or was modified, shorten and add a hash suffix
386 if (slug.length > 24 || slug !== name) {
387 // Truncate to 17 chars (leaving room for '-' + 6-char hash)
388 slug = slug.slice(0, 17);
389
390 // Ensure it ends with a dash before adding the suffix
391 if (!slug.endsWith("-")) {
392 slug += "-";
393 }
394
395 // Create the 6-char suffix from a hash of the *original* name
396 const hexHash = crypto
397 .createHash("sha256")
398 .update(name)
399 .digest("hex");
400
401 // Use BigInt for safe conversion from hex -> base36
402 const suffix = BigInt(`0x${hexHash}`).toString(36).slice(-6);
403
404 return slug + suffix;
405 }
406
407 // 4. If it was short and unmodified, just ensure it doesn't end with '-'
408 return slug.replace(/-$/, "");
409 }
410
411 get jobStatus () {
412 if (this.preScriptsExitCode == null) return "pending";

Callers 2

constructorMethod · 0.95
_predefinedVariablesMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected