GitLab API 标签创建错误

4

您好,我正在尝试使用GitLab API为项目创建标签,但它一直显示标签名称无效。我甚至尝试了GitLab API文档中的示例。

这是我的尝试:

➜  /tmp  curl -X POST -d @body.json https://mygitlabserver.com/api/v3/projects/9733/repository/tags --header "Content-Type:application/json" -H "PRIVATE-TOKEN:sNW8AGtLMdSGAJiGQ-gV"
{"message":"Tag name invalid"}% 

➜  /tmp  cat body.json 
{
    "commit": {
        "author_email": "john@example.com",
        "author_name": "John Smith",
        "authored_date": "2012-05-28T04:42:42-07:00",
        "committed_date": "2012-05-28T04:42:42-07:00",
        "committer_email": "jack@example.com",
        "committer_name": "Jack Smith",
        "id": "2695effb5807a22ff3d138d593fd856244e155e7",
        "message": "Initial commit",
        "parents_ids": [
            "2a4b78934375d7f53875269ffd4f45fd83a84ebe"
        ]
    },
    "message": null,
    "name": "v1.0.0",
    "release": {
        "description": "Amazing release. Wow",
        "tag_name": "1.0.0"
    }
}  
2个回答

4
我可以为您进行翻译。以下是对内容的翻译:

我用以下方法使其正常工作。

这是一个POST请求:

curl -X POST -k -H 'PRIVATE-TOKEN: XXXXXXX' \
'https://mygitlabserver.com/api/v3/projects/9733/repository/tags?tag_name=0.0.9&ref=develop'

1
在我的答案中看到了你对 ref 参数的好发现。+1 - VonC

1

GiLab创建新标签的API位于lib/api/tags.rb中。

  # Create tag
  #
  # Parameters:
  #   id (required) - The ID of a project
  #   tag_name (required) - The name of the tag
  #   ref (required) - Create tag from commit sha or branch
  #   message (optional) - Specifying a message creates an annotated tag.
  # Example Request:
  #   POST /projects/:id/repository/tags
  post ':id/repository/tags' do
    authorize_push_project
    message = params[:message] || nil
    result = CreateTagService.new(user_project, current_user).
    execute(params[:tag_name], params[:ref], message, params[:release_description])

它调用 app/services/create_tag_service.rb

valid_tag = Gitlab::GitRefValidator.validate(tag_name)

实际上,lib/gitlab/git_ref_validator.rb 中包装了对 git check-ref-format 的调用:

def validate(ref_name)
      Gitlab::Utils.system_silent(
        %W(#{Gitlab.config.git.bin_path} check-ref-format refs/#{ref_name}))
end

由于其中一条规则是:

它们必须至少包含一个 /。这强制存在类别,例如 heads/tags/ 等,但实际名称没有限制。

为了测试,请尝试使用以 tags/xxx 开头的标签名。

如果可以正常工作,则说明验证 tag_name 的方式存在错误。


谢谢,但还是一样的。 ➜ /tmp curl -X POST -d @body.json https://mygitlabserver.com/api/v3/projects/9733/repository/tags --header "Content-Type:application/json" -H "PRIVATE-TOKEN:sNW8AGtLMdSGAJiGQ-gV" {"message":"标签名称无效"}% ➜ /tmp cat body.json { "message": null, "name": "tags/v1.0.0" }我猜这就是你要我尝试的吧?如果不是,请详细说明一下。 - Pramod Setlur
@PramodSetlur 请尝试修改 https://github.com/gitlabhq/gitlabhq/blob/91fa250038e9182988319f088fb84741b6e2efc9/lib/gitlab/git_ref_validator.rb,以添加更多跟踪信息,以便查看运行的确切 git 命令。 - VonC
没有访问托管API的盒子。 :/ - Pramod Setlur
1
@PramodSetlur 好的。尝试在您的桌面上安装一个,并使用curl调用本地主机进行测试:这样您就可以修改任何想要的内容了。 - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接