Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added self-hosted gitlab to catalog command #3867

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

j2udev
Copy link

@j2udev j2udev commented Feb 10, 2025

Description

Fixes #3800.

This PR just adds a regex for "self-hosted gitlab" matching any domain that begins with gitlab.. The regex could probably be a bit better, but I just kept it consistent with the github enterprise regex because it also worked for my self-hosted instance.

I've attached screenshots of the catalog command working with my self-hosted gitlab, although there isn't anything in the TUI that shows that so you'll have to take my word for it.

Screenshot 2025-02-10 at 1 56 53 PM Screenshot 2025-02-10 at 1 57 31 PM

Something to note: for my specific, self-hosted gitlab repo, I'm using git submodules for all terraform/tofu modules under the modules directory. Subsequent executions of the catalog command are failing. Seems that go-getter can successfully clone the repo and pull the submodules on the first execution, but for whatever reason it's failing afterwards with this:

19:08:52.408 INFO   Cloning repository "git::https://gitlab.self-hosted.com/terraform/meta?ref=main" to temporary directory "/tmp/catalog4d474a6d6732674d7a5a4e565f7658416230364d36756f44367a45/meta"
19:08:57.524 ERROR  error downloading 'https://gitlab.self-hosted.com/terraform/meta?ref=HEAD': /usr/local/bin/git exited with 128: fatal: not a git repository: modules/cert-manager/../../.git/modules/cert-manager

19:08:57.526 ERROR  Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Upon inspecting the .git folder of the repo, it doesn't have the modules folder as it should. This is a separate issue that I can file and potentially look into.

TODOs

Read the Gruntwork contribution guidelines.

  • Update the docs. (There wasn't anything specific for the catalog command for github enterprise so I didn't add anything for self-hosted gitlab either).
  • Run the relevant tests successfully, including pre-commit checks.
  • Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Updated the catalog command to accept self-hosted GitLab URLs.

Summary by CodeRabbit

  • New Features

    • Expanded support for GitLab by enabling correct URL generation for self-hosted GitLab repositories alongside standard ones.
  • Tests

    • Added a test case to verify proper URL formatting for self-hosted GitLab instances, ensuring reliable functionality.

Copy link

vercel bot commented Feb 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 10, 2025 7:29pm

Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

This pull request extends the functionality of the module URL generation to support self-hosted GitLab repositories. It adds a new regex constant and its compiled variable to detect self-hosted GitLab URLs, and adjusts the URL construction logic in the ModuleURL method accordingly. Additionally, the PR updates the associated tests to verify that the new self-hosted GitLab URL handling produces the expected output.

Changes

File(s) Change Summary
cli/commands/catalog/module/repo.go Added constant gitlabSelfHostedRegex and variable gitlabSelfHostedPatternReg; modified ModuleURL method to construct URLs for self-hosted GitLab instances.
cli/commands/catalog/module/repo_test.go Introduced a new test case in TestModuleURL to validate correct URL generation for self-hosted GitLab repositories.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant M as ModuleURL Method
    participant R as Regex Checker

    U->>M: Call ModuleURL with repo URL (self-hosted GitLab)
    M->>R: Check against gitlabSelfHostedPatternReg
    alt URL matches self-hosted pattern
        M->>M: Construct self-hosted GitLab URL
    else Standard GitLab URL
        M->>M: Construct standard GitLab URL
    end
    M->>U: Return constructed URL
Loading

Assessment against linked issues

Objective Addressed Explanation
Support for self-hosted GitLab URLs (#3800)

Poem

Code evolves, and so do we,
With regex refining paths gracefully.
Self-hosted GitLab now has its say,
Tests ensure its proper display.
A little code magic brings brighter days! 🎉


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 299e5c5 and 8621377.

📒 Files selected for processing (2)
  • cli/commands/catalog/module/repo.go (3 hunks)
  • cli/commands/catalog/module/repo_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cli/commands/catalog/module/repo.go
  • cli/commands/catalog/module/repo_test.go

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a958298 and 299e5c5.

📒 Files selected for processing (2)
  • cli/commands/catalog/module/repo.go (3 hunks)
  • cli/commands/catalog/module/repo_test.go (1 hunks)
🔇 Additional comments (2)
cli/commands/catalog/module/repo.go (2)

29-29: LGTM! Good regex pattern for self-hosted GitLab.

The regex pattern ^(gitlab\.(.+))$ correctly matches any domain beginning with gitlab., maintaining consistency with the GitHub Enterprise regex pattern.


159-161: Fix URL construction for self-hosted GitLab.

Self-hosted GitLab instances use the same URL format as GitLab.com (/-/tree/ instead of /tree/). Let's reuse the GitLab URL format for consistency.

Apply this diff to fix the URL construction:

 	if gitlabSelfHostedPatternReg.MatchString(string(remote.Host)) {
-		return fmt.Sprintf("https://%s/%s/tree/%s/%s", remote.Host, remote.FullName, repo.BranchName, moduleDir), nil
+		return fmt.Sprintf("https://%s/%s/-/tree/%s/%s", remote.Host, remote.FullName, repo.BranchName, moduleDir), nil
 	}

Let's verify this with GitLab's documentation:

✅ Verification successful

🌐 Web query:

What is the URL format for GitLab repository tree view?

💡 Result:

The GitLab repository tree view URL follows a specific structure to navigate directories and files within a project. Here's how it works:

Standard Tree View URL Format

The basic pattern for accessing repository contents is:

https://gitlab.com/<namespace>/<project>/-/tree/<ref>/<path>

Components explained:

  • namespace: User/group name (e.g., gitlab-org)
  • project: Repository name (e.g., gitlab)
  • ref: Branch name/tag/commit SHA (e.g., main, v15.0.0, or 20ac59ad)
  • path: Directory path within repository (optional)

Example Use Cases

  1. Root directory of main branch:

    https://gitlab.com/gitlab-org/gitlab/-/tree/main
    
  2. Specific subdirectory:

    https://gitlab.com/gitlab-org/gitlab/-/tree/main/doc/api
    
  3. Commit-specific tree:

    https://gitlab.com/extra500/extra500/tree/20ac59ad
    

API Endpoint Structure

For programmatic access via GitLab API ([8]):

GET /projects/:id/repository/tree

Parameters:

Parameter Description Example
id Project ID/path gitlab-org%2Fgitlab
ref Branch/tag/SHA main
path Directory path doc/api
recursive Show nested files true/false

Example API request:

https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/tree?ref=main&path=doc/api

Key Observations

  1. Web UI vs API:

    • Web interface uses /tree/ path component
    • API uses /repository/tree endpoint
  2. Special Characters:

    • Spaces become %20
    • Slashes in paths remain unencoded
  3. Redirect Behavior:

    • Recent versions use permanent redirects (308) for optimized URLs[2]
    • Unique IDs may appear in URLs when "unique domain" setting is enabled[2]

For developers working with repository paths programmatically, consider using relative paths where possible to avoid hardcoding instance-specific domains[4].

Citations:


Self-hosted GitLab URL should use the /-/tree/ format.

The web documentation confirms that the correct URL format is:

  https://gitlab.com/<namespace>/<project>/-/tree/<ref>/<path>

This matches the diff provided in the review, so the corrected URL construction is on point.

  • File: cli/commands/catalog/module/repo.go, lines 159-161

Comment on lines 122 to 128
{
"gitlab self-hosted",
newRepo(t, "https://gitlab.acme.com/acme/terraform-aws-modules"),
".",
"https://gitlab.acme.com/acme/terraform-aws-modules/tree/main/.",
nil,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inconsistent URL format for self-hosted GitLab.

The test case expects a GitHub-style URL format (/tree/main/) for self-hosted GitLab, but GitLab consistently uses /-/tree/main/ across both hosted and self-hosted instances. Let's align this with GitLab's standard URL format.

Apply this diff to fix the URL format:

-			"https://gitlab.acme.com/acme/terraform-aws-modules/tree/main/.",
+			"https://gitlab.acme.com/acme/terraform-aws-modules/-/tree/main/.",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
"gitlab self-hosted",
newRepo(t, "https://gitlab.acme.com/acme/terraform-aws-modules"),
".",
"https://gitlab.acme.com/acme/terraform-aws-modules/tree/main/.",
nil,
},
{
"gitlab self-hosted",
newRepo(t, "https://gitlab.acme.com/acme/terraform-aws-modules"),
".",
"https://gitlab.acme.com/acme/terraform-aws-modules/-/tree/main/.",
nil,
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terragrunt catalog - hosting: "self-hosted.gitlab.com" is not supported yet
1 participant