All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gabrielittner.github.diff.MainArgs.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package com.gabrielittner.github.diff

import com.xenomachina.argparser.ArgParser
import com.xenomachina.argparser.default
import java.util.Base64

class MainArgs(parser: ArgParser) {
    val circleci by parser.flagging(help = "use CircleCI environment variables for config")

    val username by parser.storing(help = "Github username of the repository owner").default { null }
    val repository by parser.storing(help = "Github repository name").default { null }
    val commit by parser.storing(help = "Full sha-1 of the built commit").default { null }
    val prs by parser.adding("--pr", help = "Numbers of pull requests being built") { toInt() }.default { null }

    val githubToken by parser.storing("--github", help = "Github Auth token used to post comment")
    val googleCredentials by parser.storing("--datastore", help = "Base64 encoded .json credential file") { base64Decode() }
    val apks by parser.adding("--apk", help = "Path to apk. Can be repeated.")
}

fun MainArgs.toConfig(): Config {
    if (circleci) {
        val config = circleCiConfig()
        return config.copy(commit = commit ?: config.commit)
    }
    return Config(username!!, repository!!, commit!!, prs!!)
}

private fun circleCiConfig(): Config {
    return Config(
            System.getenv("CIRCLE_PROJECT_USERNAME"),
            System.getenv("CIRCLE_PROJECT_REPONAME"),
            System.getenv("CIRCLE_SHA1"),
            System.getenv("CIRCLE_PULL_REQUESTS")?.split(",")?.map { it.takeLastWhile { it != '/' }.toInt() } ?: emptyList())
}

private fun String.base64Decode(): String = Base64.getDecoder().decode(this).toString(Charsets.UTF_8)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy