unjs / giget

✨ Download templates and git repositories with pleasure!
MIT License
419 stars 33 forks source link

parseGitURI is not working for repositories in sub directory #148

Open alirezavalizade opened 5 months ago

alirezavalizade commented 5 months ago

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.

https://github.com/unjs/giget/blob/83eb734bb6df993958023e19a363d93d60568248/src/_utils.ts#L49

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 commented 1 month ago

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.