-
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.
Merge pull request #198 from CodeKaio/github_module_import
✨ : github module import
- Loading branch information
Showing
29 changed files
with
721 additions
and
60 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
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
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,15 @@ | ||
package io.codeka.gaia.registries | ||
|
||
import io.codeka.gaia.registries.github.GithubRepository | ||
import io.codeka.gaia.teams.User | ||
import org.springframework.web.client.RestTemplate | ||
|
||
interface RegistryApi<T> { | ||
|
||
fun getRepositories(user: User) : List<String> | ||
|
||
fun getRepository(user: User, owner: String, repo: String): T | ||
|
||
fun getFileContent(user: User, projectId: String, filename: String): 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
54 changes: 54 additions & 0 deletions
54
src/main/java/io/codeka/gaia/registries/controller/GithubRegistryController.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.github.GithubRepository | ||
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/github") | ||
@Secured | ||
class GithubRegistryController( | ||
private val githubRegistryApi: RegistryApi<GithubRepository>, | ||
private val hclParser: HclParser, | ||
private val cliRepository: TerraformCLIRepository, | ||
private val moduleRepository: TerraformModuleRepository) { | ||
|
||
@GetMapping("/repositories") | ||
fun getRepositories(user: User): List<String> { | ||
return this.githubRegistryApi.getRepositories(user) | ||
} | ||
|
||
@GetMapping("/repositories/{owner}/{repo}/import") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
fun importRepository(@PathVariable owner: String, @PathVariable repo: String, user: User): TerraformModule { | ||
val module = TerraformModule() | ||
module.id = UUID.randomUUID().toString() | ||
|
||
val githubRepository = githubRegistryApi.getRepository(user, owner, repo) | ||
|
||
module.gitRepositoryUrl = githubRepository.htmlUrl | ||
module.gitBranch = "master" | ||
module.name = githubRepository.fullName | ||
module.cliVersion = cliRepository.listCLIVersion().first() | ||
|
||
module.registryDetails = RegistryDetails(RegistryType.GITHUB, githubRepository.fullName) | ||
module.createdBy = user | ||
|
||
// get variables | ||
val variablesFile = githubRegistryApi.getFileContent(user, "$owner/$repo", "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
79 changes: 79 additions & 0 deletions
79
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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.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", ""))) | ||
} | ||
|
||
} | ||
|
||
data class GithubRepository( | ||
@JsonProperty("full_name") val fullName: String, | ||
@JsonProperty("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
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 |
---|---|---|
|
@@ -2274,7 +2274,7 @@ i.icon { | |
/** button section **/ | ||
|
||
.btn { | ||
font-size: 14px; | ||
/*font-size: 14px;*/ | ||
} | ||
|
||
.button_sction { | ||
|
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
Oops, something went wrong.