Skip to content

Commit

Permalink
feat: GitHub Enterprise support
Browse files Browse the repository at this point in the history
Added support for GitHub Enterprise. The additional properties `ghBaseUrl` and `ghApi` can be used to point to a GitHub Enterprise server.
  • Loading branch information
error418 committed Oct 11, 2017
1 parent 8b5995e commit 1862958
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import groovy.transform.Memoized
import groovy.transform.PackageScope
import org.ajoberstar.grgit.Grgit

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import com.jcabi.http.request.ApacheRequest;
import com.jcabi.http.wire.AutoRedirectingWire;

import java.util.regex.Matcher

class GithubRepo extends GitRepo {
Expand All @@ -29,34 +34,69 @@ class GithubRepo extends GitRepo {

private Github github

private String ghBaseUrl

private String ghApi

private String ghToken

@PackageScope
Github getGithub() {
github
}

GithubRepo(Grgit grgit) {
this.ghBaseUrl = "https://github.com"
this.grgit = grgit
}

void setGhToken(String token) {
if (token)
github = new RtGithub(token)
void setGhBaseUrl(String ghBaseUrl) {
this.ghBaseUrl = ghBaseUrl.replaceAll("/+\$", "") // remove trailing slashes
}

void setGhApi(String githubApi) {
this.ghApi = githubApi.replaceAll("/+\$", "") // remove trailing slashes
github = buildGithubReference()
}

void setGhToken(String githubToken) {
this.ghToken = githubToken
github = buildGithubReference()
}

@PackageScope
@Memoized
String getMnemo() {
String repositoryUrl = grgit.remote.list().find { it.name == 'origin' }.url
Matcher matcher = repositoryUrl =~ /.*github.com[\/:]((?:.+?)\/(?:.+?))(?:\.git)/
Matcher matcher = repositoryUrl =~ /.*[\/:](.*\/.*)(?:\.git)$/
if (!matcher)
return null
return matcher.group(1)
}

private RtGithub buildGithubReference() {
if (this.ghApi != null) { // for github enterprise repositories
def request = new ApacheRequest(this.ghApi)
.header(HttpHeaders.USER_AGENT, RtGithub.USER_AGENT)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)

if (this.ghToken != null) { // also add authentication token, if available
request = request.header(HttpHeaders.AUTHORIZATION, "token ${ghToken}")
}

request = request.through(AutoRedirectingWire.class)

return new RtGithub(request)
} else if (this.ghToken) { // for github.com repositories
return new RtGithub(this.ghToken)
}
}

private String repositoryUrl(String suffix) {
if (!mnemo)
return null
return "https://github.com/${mnemo}/$suffix"
return "${ghBaseUrl}/${mnemo}/$suffix"
}

String diffUrl(String previousTag, String currentTag) {
Expand All @@ -71,4 +111,4 @@ class GithubRepo extends GitRepo {
repositoryUrl("commit/${abbreviatedId}")
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class GithubRepoSpec extends Specification {

@Shared
@Subject
GithubRepo repo = new GithubRepo(grgit)
GithubRepo repo

def setup() {
repo = new GithubRepo(grgit)
}

def "creates github service upon setting ghToken"() {
when:
Expand All @@ -39,6 +43,33 @@ class GithubRepoSpec extends Specification {
repo.github instanceof RtGithub
}

def "creates github service upon setting ghToken with api endpoint"() {
when:
repo.ghToken = '12345'
repo.ghApi = 'https://github.enterprise/api/v3'

then:
repo.github instanceof RtGithub
repo.github.entry().uri.equals("https://github.enterprise/api/v3")
}

@Unroll
def "diffUrl for #tag1 and #tag2 is #expectedUrl when setting ghBaseUrl"() {
when:
repo.ghBaseUrl = 'https://github.enterprise/'
String diffUrl = repo.diffUrl(tag1, tag2)

then:
diffUrl == expectedUrl

where:
tag1 | tag2 | expectedUrl
"v1.0.0" | "v1.1.0" | "https://github.enterprise/${repo.mnemo}/compare/v1.0.0...v1.1.0"
"v1.0.0" | null | null
null | "v1.1.0" | null

}

@Unroll
def "diffUrl for #tag1 and #tag2 is #expectedUrl"() {
when:
Expand Down

0 comments on commit 1862958

Please sign in to comment.