如何使用Gitlab4J创建合并请求?

huangapple go评论151阅读模式
英文:

How to create a merge request using Gitlab4J?

问题

我对项目进行了一些更改,并使用 JGit 进行了提交,我正在 Gitlab 上进行工作!通常情况下,当我们手动执行此操作时,Gitlab 会在仓库顶部生成一个弹出窗口,请求创建合并请求。我使用 Gitlab4J 克隆了我的仓库,并注意到还有一个用于合并请求的 API,是否有人知道如何使用它?

英文:

I made some changes to my project and commited them using JGit, I'm wokring on Gitlab! Usually when we do this manually, Gitlab generates a popup at the top of the repository that requests for the creation of a merge request. I used Gitlab4J for cloning my repositories and I noticed that there's also an API for the merge requests, does anyone knows how to use it?

答案1

得分: 0

我找到了如何完成它,以下是代码片段:

GITLAB.getMergeRequestApi().createMergeRequest(
    projectIdOrPath, 
    sourceBranch, 
    targetBranch, 
    title, 
    description, 
    assigneeId)

如果你想要使用本地仓库来恢复 projectIdOrPath,可以添加以下代码片段,它使用 JGit 和 Gitlab4J ProjectApi:

private Project getProject(Git localRepo) throws GitLabApiException {
   return GITLAB.getProjectApi().getProjects().parallelStream()
      .filter(p -> p.getHttpUrlToRepo()
      .equals(localRepo.getRepository().getConfig().getString("remote", "origin", "url")))
      .findAny().orElse(null);
}

而对于 assigneeId,你可以使用以下代码片段来获取,它使用审批者的用户名:

GITLAB.getUserApi().getUser(approver).getId()
英文:

I figured out how to do it, here's the code snippet:

GITLAB.getMergeRequestApi().createMergeRequest(
				projectIdOrPath, 
				sourceBranch, 
				targetBranch, 
				title, 
				description, 
				assigneeId)

If you want to recover the projectIdOrPath using your local repository you can add this code snippet that uses JGit and Gitlab4J ProjectApi:

private Project getProject(Git localRepo) throws GitLabApiException {
   return GITLAB.getProjectApi().getProjects().parallelStream()
      .filter(p -> p.getHttpUrlToRepo()
      .equals(localRepo.getRepository().getConfig().getString("remote", "origin", "url")))
      .findAny().orElse(null);
}

And for the assigneeId you can get it using this code snippet which uses the approver's username:

GITLAB.getUserApi().getUser(approver).getId()

huangapple
  • 本文由 发表于 2020年10月26日 19:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64536536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定