-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use appropriate endpoint for GitHub Enterprise assets
- Loading branch information
Showing
4 changed files
with
214 additions
and
33 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/main/groovy/de/gliderpilot/gradle/semanticrelease/GhEnterpriseReleaseAssets.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package de.gliderpilot.gradle.semanticrelease | ||
|
||
import javax.ws.rs.core.HttpHeaders | ||
|
||
import com.jcabi.github.Coordinates | ||
import com.jcabi.github.Release | ||
import com.jcabi.github.ReleaseAsset | ||
import com.jcabi.github.ReleaseAssets | ||
import com.jcabi.http.Request | ||
import com.jcabi.http.response.JsonResponse | ||
import com.jcabi.http.response.RestResponse | ||
|
||
/** Adapter class for GitHub Enterprise API endpoints | ||
* | ||
* RtReleaseAssets class is declared final. Therefore this class can not be extended and | ||
* makes a wrapper/adapter class (like this) necessary. | ||
* | ||
* Contents of the methods of this class are modified versions of the RtReleaseAssets class | ||
* (with support for GitHub Enterprise servers) | ||
*/ | ||
public class GhEnterpriseReleaseAssets implements ReleaseAssets { | ||
|
||
private final Release owner | ||
private final Request entry | ||
|
||
final URI githubUploadEndpoint | ||
|
||
public GhEnterpriseReleaseAssets(final String githubBaseUrl, Release owner, Request entry) { | ||
this.githubUploadEndpoint = URI.create("${githubBaseUrl}/api/uploads") // base path for asset uploads | ||
this.entry = entry | ||
this.owner = owner | ||
} | ||
|
||
@Override | ||
public ReleaseAsset upload(final byte[] content, final String type, final String name) throws IOException { | ||
return this.get( | ||
getAssetUploadRequest(content, type, name) | ||
.fetch().as(RestResponse.class) | ||
.assertStatus(HttpURLConnection.HTTP_CREATED) | ||
.as(JsonResponse.class) | ||
.json().readObject().getInt("id") | ||
) | ||
} | ||
|
||
Request getAssetUploadRequest(final byte[] content, final String type, final String name) { | ||
return this.entry.uri() | ||
.set(this.githubUploadEndpoint) | ||
.path("/repos") | ||
.path(this.owner.repo().coordinates().user()) | ||
.path(this.owner.repo().coordinates().repo()) | ||
.path("/releases") | ||
.path(String.valueOf(this.owner.number())) | ||
.path("/assets") | ||
.queryParam("name", name) | ||
.back() | ||
.method(Request.POST) | ||
.reset(HttpHeaders.CONTENT_TYPE) | ||
.header(HttpHeaders.CONTENT_TYPE, type) | ||
.body().set(content).back() | ||
} | ||
|
||
@Override | ||
public Release release() { | ||
return owner.assets().release() | ||
} | ||
|
||
@Override | ||
public Iterable<ReleaseAsset> iterate() { | ||
return owner.assets().iterate() | ||
} | ||
|
||
@Override | ||
public ReleaseAsset get(final int number) { | ||
return owner.assets().get(number) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/groovy/de/gliderpilot/gradle/semanticrelease/GhEnterpriseReleaseAssetsSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package de.gliderpilot.gradle.semanticrelease | ||
|
||
import com.jcabi.github.Coordinates | ||
import com.jcabi.github.Release | ||
import com.jcabi.github.ReleaseAssets | ||
import com.jcabi.github.Repo | ||
import com.jcabi.http.Request | ||
import com.jcabi.http.request.FakeRequest | ||
|
||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class GhEnterpriseReleaseAssetsSpec extends Specification { | ||
@Shared | ||
@Subject | ||
GhEnterpriseReleaseAssets unit | ||
|
||
Request mockRequest | ||
Release mockRelease | ||
|
||
def setup() { | ||
def mockRepo = Mock(Repo.class) | ||
mockRepo.coordinates() >> Mock(Coordinates.class) | ||
mockRepo.coordinates().user() >> "test-user" | ||
mockRepo.coordinates().repo() >> "test-repo" | ||
|
||
mockRequest = new FakeRequest( | ||
HttpURLConnection.HTTP_CREATED, | ||
"fake request", | ||
Collections.<Map.Entry<String, String>>emptyList(), | ||
"{ \"id\": 1337 }".bytes | ||
) | ||
mockRelease = Mock() | ||
|
||
mockRelease.repo() >> mockRepo | ||
mockRelease.number() >> 1337 | ||
mockRelease.assets() >> Mock(ReleaseAssets.class) | ||
|
||
unit = new GhEnterpriseReleaseAssets("https://enterprise.github", mockRelease, mockRequest) | ||
} | ||
|
||
def "should construct correct github upload api base url"() { | ||
when: | ||
unit = new GhEnterpriseReleaseAssets("https://enterprise.github", mockRelease, mockRequest) | ||
|
||
then: | ||
unit.githubUploadEndpoint.toString() == "https://enterprise.github/api/uploads" | ||
} | ||
|
||
def "should construct correct upload request with given data"() { | ||
given: | ||
def byte[] content = "This is the content" | ||
|
||
when: | ||
def uploadRequest = unit.getAssetUploadRequest(content, "test-type", "test-name") | ||
|
||
then: | ||
uploadRequest.toString().contains "test-user" | ||
uploadRequest.toString().contains "test-repo" | ||
uploadRequest.toString().contains "test-type" | ||
uploadRequest.uri().toString().contains "test-name" | ||
uploadRequest.body().get() == "This is the content" | ||
} | ||
|
||
def "should send an asset upload request and get the asset id"() { | ||
when: | ||
unit.upload("test".bytes, "test-type", "test-name") | ||
|
||
then: | ||
1 * mockRelease.assets().get(1337) | ||
} | ||
|
||
def "should pass method calls to underlying response class"() { | ||
when: | ||
unit.release() | ||
unit.iterate() | ||
unit.get(0) | ||
|
||
then: | ||
1 * mockRelease.assets().release() | ||
1 * mockRelease.assets().iterate() | ||
1 * mockRelease.assets().get(0) | ||
} | ||
} |