git 直接从 GitLab 私有存储库打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24207644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:07:14  来源:igfitidea点击:

Open a file directly from a GitLab private repository

linuxgitdrupalgitlabdrush

提问by tvl

I have a private repository on a GitLab server and using the SSH I can pull a project using git clone.

我在 GitLab 服务器上有一个私有存储库,并且使用 SSH 我可以使用 git clone 拉出一个项目。

But I want to run a script on linux command line directly from the server (more specific, a Drupal / Drush .make file)

但我想直接从服务器在 linux 命令行上运行脚本(更具体地说,一个 Drupal / Drush .make 文件)

I tried to run it using the raw file:

我尝试使用原始文件运行它:

drush make http://server.com/user/project/raw/master/file.make

(for the convenience of non Drupal users let's say)

(为了方便非 Drupal 用户,我们说)

curl http://server.com/user/project/raw/master/file.make

Without success. Of course, it returns me the login page.

没有成功。当然,它会返回登录页面。

Is it possible?

是否可以?

回答by tvl

With Chris's valuable help, here is how you can run a script (drupal .make file in my case) from a GitLab server. (Probably it works for GitHub but I didn't test it. Maybe the syntax will be a bit different). (Of course this works for any type of script)

Chris的宝贵帮助下,这里是如何从 GitLab 服务器运行脚本(在我的例子中是 drupal .make 文件)。(可能它适用于 GitHub,但我没有测试它。也许语法会有点不同)。(当然这适用于任何类型的脚本)

It can be done using the authentication tokens. Here is the documentation of the GitLab's APIand here is the GitHub's API

可以使用身份验证令牌来完成。这里是GitLab的API的文档这里是GitHub的API

For convenient I will use the https://gitlab.comas the example server.

为方便起见,我将使用https://gitlab.com作为示例服务器。

  • Go to https://gitlab.com/profile/accountand find your "Private token"

  • Then print the list of the projects and find the id of your project you are looking for

    curl https://gitlab.com/api/v3/projects?private_token=<your_private_token>

    or go there with your browser (a json viewerwill help a lot)

  • Then print the list of the files that are on this project and find the id of your file you are looking for

    curl https://gitlab.com/api/v3/projects/<project_id>/repository/tree?private_token=<your_private_token>

  • Finally get / run the file!

    curl https://gitlab.com/api/v3/projects/<project_id>/repository/raw_blobs/<file_id>?private_token=<your_private_token>

  • 转到https://gitlab.com/profile/account并找到您的“私人令牌”

  • 然后打印项目列表,找到你要找的项目的id

    curl https://gitlab.com/api/v3/projects?private_token=<your_private_token>

    或者用你的浏览器去那里(一个json 查看器很有帮助

  • 然后打印此项目中的文件列表并找到您要查找的文件的 ID

    curl https://gitlab.com/api/v3/projects/<project_id>/repository/tree?private_token=<your_private_token>

  • 最后获取/运行文件!

    curl https://gitlab.com/api/v3/projects/<project_id>/repository/raw_blobs/<file_id>?private_token=<your_private_token>

In case you want to run the script (drupal .make)

如果您想运行脚本(drupal .make)

drush make https://gitlab.com/api/v3/projects/<project_id>/repository/raw_blobs/<file_id>?private_token=<your_private_token> <drupal_folder>

drush make https://gitlab.com/api/v3/projects/<project_id>/repository/raw_blobs/<file_id>?private_token=<your_private_token> <drupal_folder>

(If you are here looking for a workflow to integrate GitLab with Aegir .make platforms without using tokens (maybe SSH?) please make a thread and paste here the link.)

(如果您在这里寻找一个工作流程来将 GitLab 与 Aegir .make 平台集成而不使用令牌(也许是 SSH?),请创建一个线程并将链接粘贴到此处。)

EDIT

编辑

You can get the file without the project_idby using the encodedproject name. For example the my-user-name/my-projectwill become: my-user-name%2Fmy-project

您可以project_id使用编码的项目名称获取不带 的文件。例如my-user-name/my-project将变成:my-user-name%2Fmy-project

回答by hanshenrik

Update 2018-12-25:

2018-12-25 更新:

as long as you're not downloading huge files, this should work:

只要您不下载大文件,这应该可以工作:

curl -s -H "Private-Token: <token>" "https://gitlab.com/api/v4/projects/<urlencode("gitlab_username/project_name")>/repository/files/<path/to/file>/raw?ref=<branch_name>"

, a real example, downloading the file /README.mdfrom the private repository https://gitlab.com/divinity76/Yur17, where the web download url is https://gitlab.com/divinity76/Yur17/raw/master/README.md?inline=false, is:

, 一个真实的例子,/README.md从私有存储库下载文件https://gitlab.com/divinity76/Yur17,其中 web 下载 urlhttps://gitlab.com/divinity76/Yur17/raw/master/README.md?inline=false是:

curl -s -H "Private-Token: afF2s1xgk6xcwXHy3J4C" "https://gitlab.com/api/v4/projects/divinity76%2Fyur17/repository/files/README%2Emd/raw?ref=master"

take special note of how the gitlab_username/repo_namewas url-encoded, eg /became %2F(you can check how your username & repo name is as url-encoded by opening your browser javascript terminal and write encodeURIComponent("your_username/repo_name");in the terminal and press enter.)

特别注意gitlab_username/repo_nameurl是如何编码的,例如/变成了%2F(您可以通过打开浏览器 javascript 终端并encodeURIComponent("your_username/repo_name");在终端中输入并按 Enter来检查您的用户名和存储库名称是如何作为 url 编码的。)

thanks to Jonathan Hall@ gitlab at mg.gitlab.com, and https://docs.gitlab.com/ee/api/repository_files.html, and tvlfor helping reach a solution.

感谢Jonathan Hallmg.gitlab.com 上的 @ gitlab 和https://docs.gitlab.com/ee/api/repository_files.html以及tvl帮助达成解决方案。





Update 2018-12-11: this method no longer works, now it just serves the login page, with a message saying need to log in to continue, even using HTTP 200 OK (shame on them), will update if i find out why ( @XavierStuvw claims it's security concerns related)

2018 年 12 月 11 日更新:此方法不再有效,现在它只为登录页面提供服务,并显示一条消息need to log in to continue,即使使用 HTTP 200 OK(对他们感到羞耻),如果我找出原因会更新(@XavierStuvw 声称这是安全的)相关问题)





i found a much easier way to do it than @tvl 's answer,

我找到了比@tvl 的答案更简单的方法,

first create an access token with API access here: https://gitlab.com/profile/personal_access_tokens, then do:

首先在此处创建具有 API 访问权限的访问令牌:https: //gitlab.com/profile/personal_access_tokens,然后执行:

wget --header 'PRIVATE-TOKEN: <token>' 'https://gitlab.com/<username>/<repo>/raw/master/path/to/file.ext'

wget --header 'PRIVATE-TOKEN: <token>' 'https://gitlab.com/<username>/<repo>/raw/master/path/to/file.ext'

i found the solution here.

我在这里找到了解决方案

回答by Sagar Shetty

If you would like to access a file from private GitLab, you could use the below approach which worked for me:)

如果您想从私人 GitLab 访问文件,您可以使用以下对我有用的方法:)

Construct the URL:

构造网址:

https://url/api/v4/projects/projectId/repository/files/fileName/raw?ref=master&private_token=Generated_private_token

https://url/api/v4/projects/projectId/repository/files/fileName/raw?ref=master&private_token=Generated_private_token



  • url is your Gitlab url ex: git.lab.com.
  • /api/v4/projectsis a constant.
  • projectIdis the projectId of your project, which you can find below the name of your project in gitlab.
  • /repository/filesis again a constant.
  • fileNameis the name of the file ex: sagar.txt
  • /raw?ref=is a constant and the value of ref can be master or any branch which you would like to take the file from. I am retrieving the file from Master.
  • Generated_private_tokenshould be generated from gitlab, please follow the steps in mentioned in the link : Generate Private Token
  • url 是您的 Gitlab 网址,例如:git.lab.com。
  • /api/v4/projects是一个常量。
  • projectId是你的项目的 projectId,你可以在 gitlab 中你的项目名称下面找到它。
  • /repository/files又是一个常量。
  • fileName是文件名,例如:sagar.txt
  • /raw?ref=是一个常量,ref 的值可以是 master 或您想要从中获取文件的任何分支。我正在从 Master 检索文件。
  • Generated_private_token应该从 gitlab 生成,请按照链接中提到的步骤进行操作: Generate Private Token