使用GitLab API设置外部问题跟踪器设置? [英] Using GitLab API to set external issues tracker settings?
问题描述
我正在将GitLab与外部问题跟踪器(JIRA)一起使用,并且效果很好.
I'm using GitLab with an external issue tracker (JIRA), and it works well.
我的问题是,当我创建一个新的GitLab项目(使用API)时,我必须转到GitLab的项目设置,并手动选择要使用的问题跟踪工具,然后手动输入我的外部问题跟踪工具的项目ID.
My problem is when I create a new GitLab project (using API), I have to go the GitLab's project settings and manually select the issue tracker I want to use and manually enter the project's id of my external issue tracker.
此屏幕将更加雄辩:
(来源: bayimg.com )
This screen will be more eloquent:
(source: bayimg.com)
(我正在谈论的两个字段是"问题跟踪器"和"问题跟踪器中的项目名称或ID" )
(The two fields I am talking about are "Issue tracker" and "Project name or id in issues tracker")
这是我的问题:是否可以使用API或其他方法自动设置这两个字段?当前, GitLab API 没有提及有关外部问题跟踪器设置的任何内容. >
So here is my question: is there any way to set up this two fields automatically, using API or other ? Currently, GitLab API does not mention anything about external issues tracker settings.
推荐答案
此代码帮助我使用 Apache HttpClient 和 Jsoup .
这段代码绝对不是100%好的,但是它显示了主要思想,它是重新创建Web表单发送的相应POST
请求.
This code helped me to automatically set the GitLab's external issues-tracker settings, using Apache HttpClient and Jsoup.
This code is absolutely not 100% good, but it shows the main idea, wich is to recreate the corresponding POST
request that the web form sends.
// 1 - Prepare the HttpClient object :
BasicCookieStore cookieStore = new BasicCookieStore();
LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.setRedirectStrategy(redirectStrategy)
.build();
try {
// 2 - Second you need to get the "CSRF Token", from a <meta> tag in the edit page :
HttpUriRequest getCsrfToken = RequestBuilder.get()
.setUri(new URI("http://localhost/_NAMESPACE_/_PROJECT_NAME_/edit"))
.build();
CloseableHttpResponse responseCsrf = httpclient.execute(getCsrfToken);
try {
HttpEntity entity = responseCsrf.getEntity();
Document doc = Jsoup.parse(EntityUtils.toString(entity));
String csrf_token = doc.getElementsByAttributeValue("name", "csrf-token").get(0).attr("content");
// 3 - Fill and submit the "edit" form with new values :
HttpUriRequest updateIssueTracker = RequestBuilder
.post()
.setUri(new URI("http://localhost/_NAMESPACE_/_PROJECT_NAME_"))
.addParameter("authenticity_token", csrf_token)
.addParameter("private_token", "_MY_PRIVATE_TOKEN_")
.addParameter("_method", "patch")
.addParameter("commit", "Save changes")
.addParameter("utf8", "✓")
.addParameter("project[issues_tracker]", "jira")
.addParameter("project[issues_tracker_id]", "_MY_JIRA_PROJECT_NAME_")
.addParameter("project[name]", "...")
...
.build();
CloseableHttpResponse responseSubmit = httpclient.execute(updateIssueTracker, httpContext);
} finally {
responseCsrf.close();
}
} finally {
httpclient.close();
}
更改_NAMESPACE_/_PROJECT_NAME_
以使其与您的项目URL对应,用管理员帐户的令牌更改_MY_PRIVATE_TOKEN_
,并用...您的jira项目的名称更改_MY_JIRA_PROJECT_NAME_
.
Change _NAMESPACE_/_PROJECT_NAME_
to make it corresponds to your project URL, change _MY_PRIVATE_TOKEN_
with your admin account's token, and change _MY_JIRA_PROJECT_NAME_
with ... your jira project's name.
这篇关于使用GitLab API设置外部问题跟踪器设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!