Using GitLab API to set external issues tracker settings?

白昼怎懂夜的黑 提交于 2019-12-23 17:22:54

问题


I'm using GitLab with an external issue tracker (JIRA), and it works well.

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.

This screen will be more eloquent:
(source: bayimg.com)

(The two fields I am talking about are "Issue tracker" and "Project name or id in issues tracker")

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.


回答1:


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();
}

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.



来源:https://stackoverflow.com/questions/23514118/using-gitlab-api-to-set-external-issues-tracker-settings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!