-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitLab support using personal access tokens (#73)
* GitLab support using personal access tokens #72 * GitLab support using personal access tokens #72 * #72 fixing PR comments * #72 fixing PR comments Co-authored-by: Satish Kolli <skkolli@users.noreply.github.com>
- Loading branch information
1 parent
c5c46fe
commit cefc258
Showing
4 changed files
with
113 additions
and
0 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
93 changes: 93 additions & 0 deletions
93
src/main/scala/com/github/simplesteph/ksm/source/GitLabSourceAcl.scala
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,93 @@ | ||
package com.github.simplesteph.ksm.source | ||
|
||
import java.io.StringReader | ||
import java.nio.charset.Charset | ||
import java.util.Base64 | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.simplesteph.ksm.parser.AclParser | ||
import com.typesafe.config.Config | ||
import org.slf4j.LoggerFactory | ||
import skinny.http.{HTTP, HTTPException, Request, Response} | ||
|
||
import scala.util.Try | ||
|
||
class GitLabSourceAcl extends SourceAcl { | ||
|
||
private val log = LoggerFactory.getLogger(classOf[GitLabSourceAcl]) | ||
|
||
override val CONFIG_PREFIX: String = "gitlab" | ||
final val REPOID_CONFIG = "repoid" | ||
final val FILEPATH_CONFIG = "filepath" | ||
final val BRANCH_CONFIG = "branch" | ||
final val HOSTNAME_CONFIG = "hostname" | ||
final val ACCESSTOKEN_CONFIG = "accesstoken" | ||
|
||
var lastModified: Option[String] = None | ||
val objectMapper = new ObjectMapper() | ||
var repoid: String = _ | ||
var filepath: String = _ | ||
var branch: String = _ | ||
var hostname: String = _ | ||
var accessToken: String = _ | ||
|
||
/** | ||
* internal config definition for the module | ||
*/ | ||
override def configure(config: Config): Unit = { | ||
repoid = config.getString(REPOID_CONFIG) | ||
filepath = config.getString(FILEPATH_CONFIG) | ||
branch = config.getString(BRANCH_CONFIG) | ||
hostname = config.getString(HOSTNAME_CONFIG) | ||
accessToken = config.getString(ACCESSTOKEN_CONFIG) | ||
} | ||
|
||
override def refresh(aclParser: AclParser): Option[SourceAclResult] = { | ||
val url = | ||
s"https://$hostname/api/v4/projects/$repoid/repository/files/$filepath?ref=$branch" | ||
val request: Request = new Request(url) | ||
|
||
// auth header | ||
request.header("PRIVATE-TOKEN", s" $accessToken") | ||
val metadata: Response = HTTP.head(request) | ||
val commitId = metadata.header("X-Gitlab-Commit-Id") | ||
|
||
log.debug(s"lastModified: ${lastModified}") | ||
log.debug(s"commitId from Head: ${commitId}") | ||
|
||
lastModified match { | ||
case `commitId` => | ||
log.info(s"No changes were detected in the ACL file ${filepath}. Skipping .... ") | ||
None | ||
case _ => | ||
val response: Response = HTTP.get(request) | ||
response.status match { | ||
case 200 => | ||
val responseJSON = objectMapper.readTree(response.textBody) | ||
lastModified = Some(responseJSON.get("commit_id").asText()) | ||
val b64encodedContent = responseJSON.get("content").asText() | ||
val data = new String( | ||
Base64.getDecoder.decode( | ||
b64encodedContent.replace("\n", "").replace("\r", "")), | ||
Charset.forName("UTF-8")) | ||
// use the CSV Parser | ||
Some(aclParser.aclsFromReader(new StringReader(data))) | ||
case _ => | ||
// we got an http error so we propagate it | ||
log.warn(response.asString) | ||
Some( | ||
SourceAclResult( | ||
Set(), | ||
List(Try( | ||
throw HTTPException(Some("Failure to fetch file"), response))))) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Close all the necessary underlying objects or connections belonging to this instance | ||
*/ | ||
override def close(): Unit = { | ||
// HTTP | ||
} | ||
} |