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

parseGitURI is not working for repositories in sub directory #148

Open
alirezavalizade opened this issue Feb 5, 2024 · 3 comments
Open

Comments

@alirezavalizade
Copy link

alirezavalizade commented Feb 5, 2024

Hi :) Let's say we have a repository name within a directory in GitLab, such as xxx/dir/website-1#master. The regular expression you are using here is not working for this type of repository name.

/^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w./-]+)?/;

resulting:
"url": "https://git.xxx.com/xxx/dir/tree/master/website-1",

and should be
"url": "https://git.xxx.com/xxx/dir/website-1/tree/master/website-1-master.tar.bz"

@JvanderHeide
Copy link

JvanderHeide commented May 16, 2024

We're using groups in gitlab and are experiencing the same issue. I've been able to (albeit nasty, by copying some code) to provide for our own specific use case, it may be crude but it works for now. The original idea I had was to call the provided gitlab TemplateProvider and override properties on the object where necessary, but as they are not exported I had to resort to copying it. I've also tried naming it as an other provider entirely but that didn't seem to get picked up.

/**
 * Handles the parsing of gitlab repo's with groups,
 * does not work with nested branches in return as it assumes any slash after # is going to be the sub dir
 * 
 * Copied from built-in giget gitlab TemplateProvider
 * @see node_modules/giget/dist/index.cjs
 * @see node_modules/giget/dist/index.mjs
 * */
const groupedGitlab: TemplateProvider = async (input, options) => {
    const gitlab = process.env.GIGET_GITLAB_URL || "https://gitlab.com";

    const [repo, refWithPath = 'main'] = input.split('#');
    const [ref, subDir = '/'] = refWithPath.split('/');

    return {
        name: repo.replace("/", "-"),
        version: ref,
        subdir: subDir,
        headers: {
            authorization: options.auth ? `Bearer ${options.auth}` : void 0,
            // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
            "sec-fetch-mode": "same-origin"
        },
        url: `${gitlab}/${repo}/tree/${ref}${subDir}`,
        tar: `${gitlab}/${repo}/-/archive/${ref}.tar.gz`
    };
};
import {groupedGitlab} from "./template-providers/grouped-gitlab";

export default defineNuxtConfig({
    extends: [
        ['gitlab:technology/nuxt-layers/example-layer', {
            giget: {
                providers: {
                    gitlab: groupedGitlab
                },
            }
        }]
    ],
});

Did not properly test with subDir and refs, just wanted to share with you and others, what I came up with whilst trying to work around the same issue.

@moleCuleFFF
Copy link

Could be related to issue with RegEx
#183

@L422Y
Copy link

L422Y commented Dec 11, 2024

Modified @JvanderHeide's solution to work with sub-groups in my case org/subgroup/repo

import type { TemplateProvider } from 'giget'

/**
 * Handles the parsing of GitLab repos with nested groups.
 * Supports repositories within multiple group levels while maintaining
 * proper branch and subdirectory handling.
 *
 * Example inputs:
 * - org/repo
 * - org/subgroup/repo
 * - org/subgroup/repo#branch
 * - org/subgroup/repo#branch/subdir
 * 
 */
export const groupedGitlab: TemplateProvider = async (input, options) => {
  const gitlab = process.env.GIGET_GITLAB_URL || 'https://gitlab.com'

  // Split into repo path and reference/subdir part
  const [repoPath, refWithPath = 'main'] = input.split('#')
  const [ref, subDir = '/'] = refWithPath.split('/')

  // Handle the full repository path including groups
  const encodedRepoPath = repoPath
    .split('/')
    .map(segment => encodeURIComponent(segment))
    .join('/')

  return {
    // Use the last part of the repo path as the name
    name: repoPath.split('/').pop() || '',
    version: ref,
    subdir: subDir,
    headers: {
      authorization: options.auth ? `Bearer ${options.auth}` : undefined,
      'sec-fetch-mode': 'same-origin'
    },
    url: `${gitlab}/${encodedRepoPath}/tree/${ref}${subDir === '/' ? '' : '/' + subDir}`,
    tar: `${gitlab}/${encodedRepoPath}/-/archive/${ref}/${repoPath.split('/').pop()}-${ref}.tar.gz`
  }
}

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

No branches or pull requests

4 participants