httpimport icon indicating copy to clipboard operation
httpimport copied to clipboard

Support for Gitlab private repos

Open priamai opened this issue 2 years ago • 9 comments

Hi there, this is very neat, would be fantastic to find a way to also support private gitlab/github maybe via an ssh key? Cheers!

priamai avatar Mar 01 '23 12:03 priamai

For instance: https://gitlab.com/api/v4/projects/xxxxxxxx/repository/files/avapi.tar.xz/raw?ref=main

requires a private-token in the header, how do I set that via the INIT files?

priamai avatar Mar 01 '23 13:03 priamai

I have tried this:

import httpimport

profile = """
[gitlab]
headers:
    PRIVATE-TOKEN:glpat-asfasfdsafdsfdsfddsf
"""
httpimport.set_profile(profile)


with httpimport.remote_repo("https://gitlab.com/api/v4/projects/xxxxxxxxx/repository/files/avapi.tar.xz/raw?ref=main",profile= profile):
    import ciao

    print(ciao)

but it doesn't seem to like it.

priamai avatar Mar 01 '23 14:03 priamai

Hello @priamai ! SSH authentication for Git will not be implemented as this project is only using HTTP/S.

There is an example very close to your use-case in the README.md: https://github.com/operatorequals/httpimport#named-profiles

You'll need to use gitlab_repo instead of remote_repo and also use the Gitlab Token as an HTTP Basic Auth password (never tried that myself, read that here: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#clone-repository-using-personal-access-token)

Also the profile parameter does accept the name of the profile, not the whole string itself, in your case profile='gitlab' should work!

operatorequals avatar Mar 01 '23 15:03 operatorequals

Like this:

import httpimport


profile = """
[gitlab]
headers:
    PRIVATE-TOKEN:glpat-xxxxxxxxxxxxxxxxxxxxx
"""
httpimport.set_profile(profile)

with httpimport.gitlab_repo("ava-clustering","ava-modules",profile= 'gitlab'):
    import avapi
    print(avapi.__dict__)

but I am getting this error ModuleNotFoundError: No module named 'avapi'

priamai avatar Mar 01 '23 15:03 priamai

This is how my folder looks like

image

priamai avatar Mar 01 '23 15:03 priamai


profile = """
[gitlab]
headers:
    PRIVATE-TOKEN:glpat-xxxxxxxxxxxxxxxxxxxxx
"""
httpimport.set_profile(profile)

with httpimport.gitlab_repo("ava-clustering","ava-modules",profile= 'gitlab'):
    import avapi
    print(avapi.__dict__)

That's.much better but you need to see how Gitlab is expecting the token in the HTTP header. PRIVATE-TOKEN will not work.

Check Gitlab's documentation!

operatorequals avatar Mar 01 '23 16:03 operatorequals

The documentation shows this: curl --header "PRIVATE-TOKEN: <your_access_token>"

but I need to check how your code makes the request :-).

priamai avatar Mar 01 '23 18:03 priamai

example from documentation: curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master"

here: https://docs.gitlab.com/ee/api/repository_files.html

priamai avatar Mar 01 '23 18:03 priamai

I see. The code uses this template for the URL: https://github.com/operatorequals/httpimport/blob/master/httpimport.py#L34

It does not use the API so, the PRIVATE-TOKEN header doesn't work. I haven't tried authenticated GitLab yet. Let's leave this issue open, so I can furtherly test it with Gitlab.

I also changed the Issue title to something more specific, so we can track the problem and eventually solve.

You are also welcome to submit a PR in case you make it work by altering httpimport!

operatorequals avatar Mar 01 '23 20:03 operatorequals