diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index c52cb39ad0ad8446d07adb561c23a94924865c2a..6710ff6b7223c6c7ee4bfcfced9366eae0c220d2 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -2,6 +2,8 @@ package org.gitlab4j.api; import java.util.Map; +import org.gitlab4j.api.models.Session; + /** * This class is provides a simplified interface to a GitLab API server, and divides the API up into * a separate API class for each concern. @@ -20,6 +22,8 @@ public class GitLabApi { private SessionApi sessoinApi; private UserApi userApi; + private Session session; + /** * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance using returned private token * @@ -30,8 +34,19 @@ public class GitLabApi { * @throws GitLabApiException GitLabApiException if any exception occurs during execution */ public static GitLabApi create(String url, String username, String password) throws GitLabApiException { - String token = new SessionApi(new GitLabApi(url, null)).login(username, null, password).getPrivateToken(); - return new GitLabApi(url, token); + SessionApi sessionApi = new SessionApi(new GitLabApi(url, (String)null)); + Session session = sessionApi.login(username, null, password); + return (new GitLabApi(url, session)); + } + + /** + * If this instance was created with {@link #create(String, String, String)} this method will + * return the Session instance returned by the GitLab API on login, otherwise returns null. + * + * @return the Session instance + */ + public Session getSession() { + return session; } /** @@ -45,6 +60,18 @@ public class GitLabApi { this(hostUrl, privateToken, null); } + /** + * Constructs a GitLabApi instance set up to interact with the GitLab server + * specified by hostUrl. + * + * @param hostUrl the URL of the GitLab server + * @param session the Session instance obtained by logining into the GitLab server + */ + public GitLabApi(String hostUrl, Session session) { + this(hostUrl, session.getPrivateToken(), null); + this.session = session; + } + /** * Constructs a GitLabApi instance set up to interact with the GitLab server * specified by hostUrl. diff --git a/src/test/java/org/gitlab4j/api/TestGitLabSession.java b/src/test/java/org/gitlab4j/api/TestGitLabSession.java index 01004991ce459235b0691fbd3798450ed3eb7d38..a350558c3bfb67f7bd2fa547a52c235db8bb3a3a 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabSession.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabSession.java @@ -1,5 +1,6 @@ package org.gitlab4j.api; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assume.assumeTrue; @@ -13,11 +14,13 @@ import org.junit.Test; * TEST_HOST_URL * TEST_USERNAME * TEST_PASSWORD + * TEST_PRIVATE_TOKEN * * If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * use a command line similar to: * - * mvn test -DTEST_HOST_URL=https://gitlab.com -DTTEST_USERNAME=your_username -DTEST_PASSWORD=your_strong_password + * mvn test -DTEST_HOST_URL=https://gitlab.com -DTTEST_USERNAME=your_username \ + * -DTEST_PASSWORD=your_strong_password -DTEST_PRIVATE_TOKEN=your_private_token */ public class TestGitLabSession { @@ -25,10 +28,12 @@ public class TestGitLabSession { private static final String TEST_USERNAME; private static final String TEST_PASSWORD; private static final String TEST_HOST_URL; + private static final String TEST_PRIVATE_TOKEN; static { TEST_USERNAME = System.getProperty("TEST_USERNAME"); TEST_PASSWORD = System.getProperty("TEST_PASSWORD"); TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); + TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN"); } private static String problems = ""; @@ -54,6 +59,10 @@ public class TestGitLabSession { problems += "TEST_HOST_URL cannot be empty\n"; } + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) { + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; + } + if (!problems.isEmpty()) { System.err.print(problems); } @@ -66,7 +75,10 @@ public class TestGitLabSession { @Test public void testSession() throws GitLabApiException { - GitLabApi gitlabApi = GitLabApi.create(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); - assertNotNull(gitlabApi); + + GitLabApi gitLabApi = GitLabApi.create(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); + assertNotNull(gitLabApi); + assertNotNull(gitLabApi.getSession()); + assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken()); } }