-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from gaia-app/gitlab_module_import
✨️ : gitlab module import
- Loading branch information
Showing
24 changed files
with
534 additions
and
295 deletions.
There are no files selected for viewing
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
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
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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/io/codeka/gaia/registries/controller/GitlabRegistryController.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,54 @@ | ||
package io.codeka.gaia.registries.controller | ||
|
||
import io.codeka.gaia.hcl.HclParser | ||
import io.codeka.gaia.modules.bo.TerraformModule | ||
import io.codeka.gaia.modules.repository.TerraformCLIRepository | ||
import io.codeka.gaia.modules.repository.TerraformModuleRepository | ||
import io.codeka.gaia.registries.RegistryApi | ||
import io.codeka.gaia.registries.RegistryDetails | ||
import io.codeka.gaia.registries.RegistryType | ||
import io.codeka.gaia.registries.gitlab.GitlabRepository | ||
import io.codeka.gaia.teams.User | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.security.access.annotation.Secured | ||
import org.springframework.web.bind.annotation.* | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/api/registries/gitlab") | ||
@Secured | ||
class GitlabRegistryController( | ||
private val gitlabRegistryApi: RegistryApi<GitlabRepository>, | ||
private val hclParser: HclParser, | ||
private val cliRepository: TerraformCLIRepository, | ||
private val moduleRepository: TerraformModuleRepository) { | ||
|
||
@GetMapping("/repositories") | ||
fun getRepositories(user: User): List<GitlabRepository> { | ||
return this.gitlabRegistryApi.getRepositories(user) | ||
} | ||
|
||
@GetMapping("/repositories/{projectId}/import") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
fun importRepository(@PathVariable projectId: String, user: User): TerraformModule { | ||
val module = TerraformModule() | ||
module.id = UUID.randomUUID().toString() | ||
|
||
val gitlabRepository = gitlabRegistryApi.getRepository(user, projectId) | ||
|
||
module.gitRepositoryUrl = gitlabRepository.htmlUrl | ||
module.gitBranch = "master" | ||
module.name = gitlabRepository.fullName | ||
module.cliVersion = cliRepository.listCLIVersion().first() | ||
|
||
module.registryDetails = RegistryDetails(RegistryType.GITLAB, gitlabRepository.id) | ||
module.createdBy = user | ||
|
||
// get variables | ||
val variablesFile = gitlabRegistryApi.getFileContent(user, projectId, "variables.tf") | ||
module.variables = hclParser.parseVariables(variablesFile) | ||
|
||
// saving module ! | ||
return moduleRepository.save(module) | ||
} | ||
} |
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
77 changes: 8 additions & 69 deletions
77
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.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<String> { | ||
// fetching repositories | ||
val url = "https://api.github.com/user/repos?visibility=public" | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
val repos = callWithAuth(url, token, Array<GithubRepository>::class.java) | ||
|
||
return repos.map { it.fullName }.toList() | ||
} | ||
|
||
override fun getRepository(user: User, owner: String, repo: String): GithubRepository { | ||
// fetching repositories | ||
val url = "https://api.github.com/repos/$owner/$repo" | ||
|
||
val token = user.oAuth2User?.token!! | ||
|
||
return callWithAuth(url, token, GithubRepository::class.java) | ||
} | ||
|
||
override fun getFileContent(user: User, projectId: String, filename: String): String { | ||
val url = "https://api.github.com/repos/$projectId/contents/$filename?ref=master"; | ||
|
||
val token = user.oAuth2User?.token!!; | ||
|
||
val file = callWithAuth(url, token, RegistryFile::class.java) | ||
|
||
// removing trailing \n | ||
println(file.content.replace("\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( | ||
@JsonProperty("full_name") val fullName: String, | ||
@JsonProperty("html_url") val htmlUrl: String) | ||
@JsonAlias("full_name") val fullName: String, | ||
@JsonAlias("html_url") val htmlUrl: String) |
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
19 changes: 19 additions & 0 deletions
19
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.codeka.gaia.registries.gitlab | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias | ||
import io.codeka.gaia.registries.AbstractRegistryApi | ||
import io.codeka.gaia.registries.RegistryType | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Service | ||
class GitlabRegistryApi(restTemplate: RestTemplate): AbstractRegistryApi<GitlabRepository>( | ||
restTemplate, | ||
RegistryType.GITLAB, | ||
GitlabRepository::class.java, | ||
Array<GitlabRepository>::class.java) | ||
|
||
data class GitlabRepository( | ||
@JsonAlias("id") val id: String, | ||
@JsonAlias("path_with_namespace") val fullName: String, | ||
@JsonAlias("web_url") val htmlUrl: String) |
Oops, something went wrong.