From b65580b6360cb3ae70a4a09b58431a654f9797ae Mon Sep 17 00:00:00 2001 From: cristianpela Date: Fri, 22 Oct 2021 11:08:09 +0300 Subject: [PATCH 1/2] #1254 GitlabRepo enable issues placeholder. --- .../java/com/selfxdsd/core/EmptyIssues.java | 66 +++++++++++++++++++ .../java/com/selfxdsd/core/GitlabRepo.java | 15 +++-- .../com/selfxdsd/core/GitlabRepoTestCase.java | 17 +++++ 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java b/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java new file mode 100644 index 00000000..884a6da0 --- /dev/null +++ b/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2020-2021, Self XDSD Contributors + * All rights reserved. + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to read the Software only. Permission is hereby NOT GRANTED to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software. + *

+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.selfxdsd.core; + +import com.selfxdsd.api.Issue; +import com.selfxdsd.api.Issues; + +import javax.json.JsonObject; +import java.util.Collections; +import java.util.Iterator; + +/** + * Empty issues representation. + * @author criske + * @version $Id$ + * @since 0.0.95 + */ +final class EmptyIssues implements Issues { + + @Override + public Issue getById(final String issueId) { + return null; + } + + @Override + public Issue received(final JsonObject issue) { + return null; + } + + @Override + public Issue open(final String title, + final String body, + final String... labels) { + return null; + } + + @Override + public Issues search(final String text, final String... labels) { + return null; + } + + @Override + public Iterator iterator() { + return Collections.emptyIterator(); + } +} \ No newline at end of file diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java b/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java index 603c3d78..dac02757 100644 --- a/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java +++ b/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java @@ -25,6 +25,8 @@ import com.selfxdsd.api.*; import com.selfxdsd.api.Labels; import com.selfxdsd.api.storage.Storage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.json.JsonObject; import java.net.URI; @@ -34,12 +36,16 @@ * @author criske * @version $Id$ * @since 0.0.1 - * @todo #1246:60min Implement enabling of Issues for a GitLab repository. - * If GitLab doesn't support this functionality, the method should not do - * anything. */ final class GitlabRepo extends BaseRepo { + /** + * Logger. + */ + private static final Logger LOG = LoggerFactory.getLogger( + GitlabRepo.class + ); + /** * Constructor. * @param resources Gitlab's JSON Resources. @@ -151,7 +157,8 @@ public String fullName() { @Override public Issues enableIssues() { - throw new UnsupportedOperationException("Not yet implemented."); + LOG.warn("Gitlab doesn't support enabling issues..."); + return new EmptyIssues(); } @Override diff --git a/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java b/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java index fa4ba656..b99cc0a2 100644 --- a/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java +++ b/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java @@ -238,4 +238,21 @@ public void returnsCommits() { ) ); } + + /** + * Gitlab doesn't support enabling issues. + */ + @Test + public void doesNotSupportEnableIssues(){ + final Repo repo = new GitlabRepo( + Mockito.mock(JsonResources.class), + URI.create("https://gitlab.com/api/v4/projects/1"), + Mockito.mock(User.class), + Mockito.mock(Storage.class) + ); + MatcherAssert.assertThat( + repo.enableIssues(), + Matchers.instanceOf(EmptyIssues.class) + ); + } } From 4f9e654a21f29a6a941867dcf1ee1ee76a7cfa64 Mon Sep 17 00:00:00 2001 From: cristianpela Date: Fri, 22 Oct 2021 12:09:40 +0300 Subject: [PATCH 2/2] #1254 GitlabRepo enable issues. --- .../java/com/selfxdsd/core/EmptyIssues.java | 66 ------------------- .../java/com/selfxdsd/core/GitlabRepo.java | 34 ++++++---- .../com/selfxdsd/core/GitlabRepoTestCase.java | 55 ++++++++++++++-- 3 files changed, 74 insertions(+), 81 deletions(-) delete mode 100644 self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java b/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java deleted file mode 100644 index 884a6da0..00000000 --- a/self-core-impl/src/main/java/com/selfxdsd/core/EmptyIssues.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright (c) 2020-2021, Self XDSD Contributors - * All rights reserved. - *

- * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to read the Software only. Permission is hereby NOT GRANTED to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software. - *

- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package com.selfxdsd.core; - -import com.selfxdsd.api.Issue; -import com.selfxdsd.api.Issues; - -import javax.json.JsonObject; -import java.util.Collections; -import java.util.Iterator; - -/** - * Empty issues representation. - * @author criske - * @version $Id$ - * @since 0.0.95 - */ -final class EmptyIssues implements Issues { - - @Override - public Issue getById(final String issueId) { - return null; - } - - @Override - public Issue received(final JsonObject issue) { - return null; - } - - @Override - public Issue open(final String title, - final String body, - final String... labels) { - return null; - } - - @Override - public Issues search(final String text, final String... labels) { - return null; - } - - @Override - public Iterator iterator() { - return Collections.emptyIterator(); - } -} \ No newline at end of file diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java b/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java index dac02757..0292e5b2 100644 --- a/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java +++ b/self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java @@ -25,10 +25,10 @@ import com.selfxdsd.api.*; import com.selfxdsd.api.Labels; import com.selfxdsd.api.storage.Storage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import javax.json.Json; import javax.json.JsonObject; +import java.net.HttpURLConnection; import java.net.URI; /** @@ -39,13 +39,6 @@ */ final class GitlabRepo extends BaseRepo { - /** - * Logger. - */ - private static final Logger LOG = LoggerFactory.getLogger( - GitlabRepo.class - ); - /** * Constructor. * @param resources Gitlab's JSON Resources. @@ -155,10 +148,29 @@ public String fullName() { return this.json().getString("path_with_namespace"); } + /** + * {@inheritDoc} + *
+ * Gitlab doc: link + */ @Override public Issues enableIssues() { - LOG.warn("Gitlab doesn't support enabling issues..."); - return new EmptyIssues(); + final Resource response = this.resources().put( + this.repoUri(), + Json.createObjectBuilder() + .add("issues_access_level", "enabled") + .build() + ); + if(response.statusCode() == HttpURLConnection.HTTP_OK) { + return this.issues(); + } else { + throw new IllegalStateException( + "Could not enable Issues for Repo [" + + this.repoUri() + "]. Received Status is " + + response.statusCode() + ", while Status 200 OK" + + " was expected." + ); + } } @Override diff --git a/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java b/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java index b99cc0a2..68d3601c 100644 --- a/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java +++ b/self-core-impl/src/test/java/com/selfxdsd/core/GitlabRepoTestCase.java @@ -11,6 +11,7 @@ import org.mockito.Mockito; import javax.json.Json; +import javax.json.JsonValue; import java.math.BigDecimal; import java.net.URI; @@ -240,19 +241,65 @@ public void returnsCommits() { } /** - * Gitlab doesn't support enabling issues. + * GitlabRepo can enable issues. */ @Test - public void doesNotSupportEnableIssues(){ + public void canEnableIssues(){ + final MockJsonResources res = new MockJsonResources( + request -> new MockJsonResources.MockResource( + 200, + Json.createObjectBuilder().build() + ) + ); final Repo repo = new GitlabRepo( - Mockito.mock(JsonResources.class), + res, URI.create("https://gitlab.com/api/v4/projects/1"), Mockito.mock(User.class), Mockito.mock(Storage.class) ); + MatcherAssert.assertThat( repo.enableIssues(), - Matchers.instanceOf(EmptyIssues.class) + Matchers.instanceOf(Issues.class) + ); + + final MockJsonResources.MockRequest request = res.requests().first(); + MatcherAssert.assertThat( + request.getUri(), + Matchers.equalTo(URI.create("https://gitlab.com/api/v4/projects/1")) + ); + MatcherAssert.assertThat( + request.getMethod(), + Matchers.equalTo("PUT") + ); + MatcherAssert.assertThat( + request.getBody(), + Matchers.equalTo( + Json.createObjectBuilder() + .add("issues_access_level", "enabled") + .build() + ) + ); + } + + /** + * GitlabRepo throws IllegalStateException if enableIssues returns status + * != 200 OK. + */ + @Test(expected = IllegalStateException.class) + public void throwsIllegalStateExceptionIfEnableIssueNotOk() { + final JsonResources res = new MockJsonResources( + request -> new MockJsonResources.MockResource( + 410, + JsonValue.EMPTY_JSON_OBJECT + ) + ); + final Repo repo = new GitlabRepo( + res, + URI.create("https://gitlab.com/api/v4/projects/1"), + Mockito.mock(User.class), + Mockito.mock(Storage.class) ); + repo.enableIssues(); } }