Pierre
Pierre

Reputation: 2742

Using GitLab API to set external issues tracker settings?

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: GitLab external issue tracker settings
(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.

Upvotes: 3

Views: 2003

Answers (1)

Pierre
Pierre

Reputation: 2742

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.

Upvotes: 2

Related Questions