-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
136 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/io/codeka/gaia/registries/AbstractRegistryApi.kt
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,63 @@ | ||
package io.codeka.gaia.registries | ||
|
||
import io.codeka.gaia.teams.User | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.client.RestTemplate | ||
import java.util.* | ||
|
||
abstract class AbstractRegistryApi<K>(val restTemplate: RestTemplate, | ||
private val registryType: RegistryType, | ||
private val registryFileClass: Class<K>, | ||
private val registryListFileClass: Class<Array<K>>): RegistryApi<K> { | ||
|
||
private fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T { | ||
val headers = HttpHeaders() | ||
headers.add("Authorization", "Bearer $token") | ||
|
||
val requestEntity = HttpEntity<Any>(headers) | ||
|
||
val response = restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
requestEntity, | ||
responseType) | ||
if(response.statusCode == HttpStatus.OK) { | ||
return response.body | ||
} | ||
else { | ||
TODO("error code mgmt") | ||
} | ||
} | ||
|
||
override fun getRepositories(user: User): List<K> { | ||
// fetching repositories | ||
val url = registryType.repositoriesUrl | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
return callWithAuth(url, token, registryListFileClass).toList() | ||
} | ||
|
||
override fun getRepository(user: User, projectId: String): K { | ||
// fetching repositories | ||
val url = registryType.repositoryUrl.format(projectId) | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
return callWithAuth(url, token, registryFileClass) | ||
} | ||
|
||
override fun getFileContent(user: User, projectId: String, filename: String): String { | ||
val url = registryType.fileContentUrl.format(projectId, filename) | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
val file = callWithAuth(url, token, RegistryFile::class.java) | ||
|
||
// removing trailing \n | ||
return String(Base64.getDecoder().decode(file.content.replace("\n", ""))) | ||
} | ||
} |
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
75 changes: 7 additions & 68 deletions
75
src/main/java/io/codeka/gaia/registries/github/GithubRegistryApi.kt
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 |
---|---|---|
@@ -1,79 +1,18 @@ | ||
package io.codeka.gaia.registries.github | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming | ||
import io.codeka.gaia.registries.RegistryApi | ||
import io.codeka.gaia.registries.RegistryFile | ||
import io.codeka.gaia.registries.AbstractRegistryApi | ||
import io.codeka.gaia.registries.RegistryType | ||
import io.codeka.gaia.registries.gitlab.GitlabRepository | ||
import io.codeka.gaia.teams.User | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
import java.util.* | ||
|
||
@Service | ||
class GithubRegistryApi(val restTemplate: RestTemplate): RegistryApi<GithubRepository> { | ||
|
||
fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T{ | ||
val headers = HttpHeaders() | ||
if(token != null) { | ||
headers.add("Authorization", "Bearer $token") | ||
} | ||
|
||
val requestEntity = HttpEntity<Any>(headers) | ||
|
||
val response = restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
requestEntity, | ||
responseType) | ||
if(response.statusCode == HttpStatus.OK) { | ||
return response.body | ||
} | ||
else { | ||
TODO("error code mgmt") | ||
} | ||
} | ||
|
||
override fun getRepositories(user: User): List<GithubRepository> { | ||
// fetching repositories | ||
val url = RegistryType.GITHUB.repositoriesUrl | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
val repos = callWithAuth(url, token, Array<GithubRepository>::class.java) | ||
|
||
return repos.toList() | ||
} | ||
|
||
override fun getRepository(user: User, projectId: String): GithubRepository { | ||
// fetching repositories | ||
val url = RegistryType.GITHUB.repositoryUrl.format(projectId) | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
return callWithAuth(url, token, GithubRepository::class.java) | ||
} | ||
|
||
override fun getFileContent(user: User, projectId: String, filename: String): String { | ||
val url = RegistryType.GITHUB.fileContentUrl.format(projectId, filename) | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
val file = callWithAuth(url, token, RegistryFile::class.java) | ||
|
||
// removing trailing \n | ||
return String(Base64.getDecoder().decode(file.content.replace("\n", ""))) | ||
} | ||
|
||
} | ||
class GithubRegistryApi(restTemplate: RestTemplate): AbstractRegistryApi<GithubRepository>( | ||
restTemplate, | ||
RegistryType.GITHUB, | ||
GithubRepository::class.java, | ||
Array<GithubRepository>::class.java) | ||
|
||
data class GithubRepository( | ||
@JsonAlias("full_name") val fullName: String, | ||
@JsonAlias("html_url") val htmlUrl: String) | ||
@JsonAlias("html_url") val htmlUrl: String) |
72 changes: 6 additions & 66 deletions
72
src/main/java/io/codeka/gaia/registries/gitlab/GitlabRegistryApi.kt
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