com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig Java Examples

The following examples show how to use com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitLabConfigurationTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@ConfiguredWithReadme("gitlab/README.md")
public void configure_gitlab_connection() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    final GitLabConnectionConfig gitLabConnections = jenkins.getDescriptorByType(GitLabConnectionConfig.class);
    assertEquals(1, gitLabConnections.getConnections().size());
    final GitLabConnection gitLabConnection = gitLabConnections.getConnections().get(0);
    assertEquals("gitlab_token", gitLabConnection.getApiTokenId());
    assertEquals("my_gitlab_server", gitLabConnection.getName());
    assertEquals("autodetect", gitLabConnection.getClientBuilderId());
    assertEquals("https://gitlab.com/", gitLabConnection.getUrl());
    assertEquals(20, gitLabConnection.getConnectionTimeout());
    assertEquals(10, gitLabConnection.getReadTimeout());
    assertTrue(gitLabConnection.isIgnoreCertificateErrors());
}
 
Example #2
Source File: SettingsUtils.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
static List<String> gitLabConnectionNames() {
    GitLabConnectionConfig config = connectionConfig();
    List<String> names = new ArrayList<>();

    for (GitLabConnection conn : config.getConnections()) {
        names.add(conn.getName());
    }

    return names;
}
 
Example #3
Source File: BuildWebHookAction.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void checkPermission(Permission permission, Item project) {
    if (((GitLabConnectionConfig) Jenkins.get().getDescriptor(GitLabConnectionConfig.class)).isUseAuthenticatedEndpoint()) {
        if (!project.getACL().hasPermission(authentication, permission)) {
            String message =  String.format("%s is missing the %s/%s permission", authentication.getName(), permission.group.title, permission.name);
            LOGGER.finest("Unauthorized (Did you forget to add API Token to the web hook ?)");
            throw HttpResponses.errorWithoutStack(403, message);
        }
    }
}
 
Example #4
Source File: TestUtility.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
static void setupGitLabConnections(JenkinsRule jenkins, MockServerRule mockServer) throws IOException {
    GitLabConnectionConfig connectionConfig = jenkins.get(GitLabConnectionConfig.class);
    String apiTokenId = "apiTokenId";
    for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
        if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
            List<Domain> domains = credentialsStore.getDomains();
            credentialsStore.addCredentials(domains.get(0),
                new StringCredentialsImpl(CredentialsScope.SYSTEM, apiTokenId, "GitLab API Token", Secret.fromString(TestUtility.API_TOKEN)));
        }
    }
    connectionConfig.addConnection(new GitLabConnection(TestUtility.GITLAB_CONNECTION_V3, "http://localhost:" + mockServer.getPort() + "/gitlab", apiTokenId, new V3GitLabClientBuilder(), false, 10, 10));
    connectionConfig.addConnection(new GitLabConnection(TestUtility.GITLAB_CONNECTION_V4, "http://localhost:" + mockServer.getPort() + "/gitlab", apiTokenId, new V4GitLabClientBuilder(), false, 10, 10));

}
 
Example #5
Source File: GitLabRule.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
public GitLabConnectionProperty createGitLabConnectionProperty() throws IOException {
    for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
        if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
            List<Domain> domains = credentialsStore.getDomains();
            credentialsStore.addCredentials(domains.get(0),
                new StringCredentialsImpl(CredentialsScope.SYSTEM, API_TOKEN_ID, "GitLab API Token", Secret.fromString(getApiToken())));
        }
    }

    GitLabConnectionConfig config = Jenkins.getInstance().getDescriptorByType(GitLabConnectionConfig.class);
    GitLabConnection connection = new GitLabConnection("test", url, API_TOKEN_ID, new V3GitLabClientBuilder(), true,10, 10);
    config.addConnection(connection);
    config.save();
    return new GitLabConnectionProperty(connection.getName());
}
 
Example #6
Source File: GitLabSCMBuildStatusPublisher.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private GitLabClient getClient(String connectionName) {
    GitLabConnectionConfig config = (GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
    return config != null ? config.getClient(connectionName) : null;
}
 
Example #7
Source File: SettingsUtils.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private static GitLabConnectionConfig connectionConfig() {
    return (GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
}
 
Example #8
Source File: GitLabHelper.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private static GitLabConnectionConfig connectionConfig() {
    return (GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
}
 
Example #9
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    PowerMockito.mockStatic(GitLabConnectionProperty.class);
    PowerMockito.mockStatic(Jenkins.class);
    when(Jenkins.getInstance()).thenReturn(jenkins);
       when(Jenkins.getActiveInstance()).thenReturn(jenkins);
    when(jenkins.getRootUrl()).thenReturn(JENKINS_URL);
    when(jenkins.getDescriptor(GitLabConnectionConfig.class)).thenReturn(gitLabConnectionConfig);
    when(GitLabConnectionProperty.getClient(any(Run.class))).thenReturn(client);
    when(gitLabConnectionConfig.getClient(any(String.class), any(Item.class), any(String.class))).thenReturn(client);
       when(connection.getClient()).thenReturn(client);
    when(build.getAction(BuildData.class)).thenReturn(action);
    when(action.getLastBuiltRevision()).thenReturn(lastBuiltRevision);
    when(action.getLastBuild(any(ObjectId.class))).thenReturn(lastBuild);
    when(lastBuild.getMarked()).thenReturn(revision);
    when(revision.getSha1String()).thenReturn(REVISION);
    when(build.getUrl()).thenReturn(BUILD_URL);
    when(build.getEnvironment(any(TaskListener.class))).thenReturn(environment);
    when(build.getCauses()).thenReturn(new ArrayList<Cause>(Collections.singletonList(upCauseLevel1)));
    when(upCauseLevel1.getUpstreamCauses()).thenReturn(new ArrayList<Cause>(Collections.singletonList(upCauseLevel2)));
    when(upCauseLevel2.getUpstreamCauses()).thenReturn(new ArrayList<Cause>(Collections.singletonList(gitlabCause)));
    if(Functions.isWindows()) {
        when(taskListener.getLogger()).thenReturn(new PrintStream("nul"));
    } else {
        when(taskListener.getLogger()).thenReturn(new PrintStream("/dev/null"));
    }


    causeData = causeData()
               .withActionType(CauseData.ActionType.NOTE)
               .withSourceProjectId(PROJECT_ID)
               .withTargetProjectId(PROJECT_ID)
               .withBranch("feature")
               .withSourceBranch("feature")
               .withUserName("")
               .withSourceRepoHomepage("https://gitlab.org/test")
               .withSourceRepoName("test")
               .withSourceNamespace("test-namespace")
               .withSourceRepoUrl("[email protected]:test.git")
               .withSourceRepoSshUrl("[email protected]:test.git")
               .withSourceRepoHttpUrl("https://gitlab.org/test.git")
               .withMergeRequestTitle("Test")
               .withMergeRequestId(1)
               .withMergeRequestIid(1)
               .withTargetBranch("master")
               .withTargetRepoName("test")
               .withTargetNamespace("test-namespace")
               .withTargetRepoSshUrl("[email protected]:test.git")
               .withTargetRepoHttpUrl("https://gitlab.org/test.git")
               .withTriggeredByUser("test")
               .withLastCommit(REVISION)
               .withTargetProjectUrl("https://gitlab.org/test")
               .build();

    when(gitlabCause.getData()).thenReturn(causeData);
    PowerMockito.spy(client);
}