Skip to content

Commit

Permalink
Merge pull request #1261 from criske/#1254
Browse files Browse the repository at this point in the history
#1254 Gitlab enable issues.
  • Loading branch information
amihaiemil committed Oct 22, 2021
2 parents 8765cd0 + 4f9e654 commit bca7b70
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
27 changes: 23 additions & 4 deletions self-core-impl/src/main/java/com/selfxdsd/core/GitlabRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
import com.selfxdsd.api.Labels;
import com.selfxdsd.api.storage.Storage;

import javax.json.Json;
import javax.json.JsonObject;
import java.net.HttpURLConnection;
import java.net.URI;

/**
* A Gitlab repository.
* @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 {

Expand Down Expand Up @@ -149,9 +148,29 @@ public String fullName() {
return this.json().getString("path_with_namespace");
}

/**
* {@inheritDoc}
* <br/>
* Gitlab doc: <a href="https://docs.gitlab.com/ee/api/projects.html#edit-project">link</a>
*/
@Override
public Issues enableIssues() {
throw new UnsupportedOperationException("Not yet implemented.");
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.mockito.Mockito;

import javax.json.Json;
import javax.json.JsonValue;
import java.math.BigDecimal;
import java.net.URI;

Expand Down Expand Up @@ -238,4 +239,67 @@ public void returnsCommits() {
)
);
}

/**
* GitlabRepo can enable issues.
*/
@Test
public void canEnableIssues(){
final MockJsonResources res = new MockJsonResources(
request -> new MockJsonResources.MockResource(
200,
Json.createObjectBuilder().build()
)
);
final Repo repo = new GitlabRepo(
res,
URI.create("https://gitlab.com/api/v4/projects/1"),
Mockito.mock(User.class),
Mockito.mock(Storage.class)
);

MatcherAssert.assertThat(
repo.enableIssues(),
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();
}
}

0 comments on commit bca7b70

Please sign in to comment.