Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GV Nightly version Updated #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ l10n-repo/
.DS_Store

# jacoco.exec
jacoco.exec
jacoco.exec

#Gihub Bot Token
.github_token
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,6 @@ task printVersion {
println("version: " + Config.componentsVersion)
}
}

apply plugin: GVNightlyVersionVerifierPlugin
apply plugin: GitHubPlugin
5 changes: 5 additions & 0 deletions buildSrc/src/main/java/Extentions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import org.gradle.api.Project

fun Project.property(names: String, defaultValue: String): String {
return if (hasProperty(names)) property(names).toString() else defaultValue
}
68 changes: 68 additions & 0 deletions buildSrc/src/main/java/GVNightlyVersionVerifierPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import GeckoVersions.nightly_version
import groovy.util.Node
import groovy.util.NodeList
import groovy.util.XmlParser
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.task
import java.io.File
import java.lang.Exception

open class GVNightlyVersionVerifierPlugin : Plugin<Project> {

companion object {
private const val GV_VERSION_PATH_FILE = "buildSrc/src/main/java/Gecko.kt"
private const val MAVEN_MOZILLA_GV_URL = "https://maven.mozilla.org/maven2/org/mozilla/geckoview/geckoview-nightly-armeabi-v7a/maven-metadata.xml"
}

override fun apply(project: Project) {

project.task("updateGVNightlyVersion") {

doLast {
updateGVNightlyVersion(project)
}
}
}

private fun updateGVNightlyVersion(project: Project) {

val newVersion = getLastGeckoViewNightlyVersion(project)

if (newVersion.isNotEmpty()) {
val filePath = project.property("gvVersionFile", GV_VERSION_PATH_FILE)
updateConfigFileWithNewGVNightlyVersion(filePath, newVersion)
} else {
throw Exception("Unable to find a new version of GeckoViewNightly")
}
}

@Suppress("UNCHECKED_CAST")
private fun getLastGeckoViewNightlyVersion(project: Project): String {
val mavenURL = project.property("gvMavenVersionURL", MAVEN_MOZILLA_GV_URL)
val versioning = (XmlParser().parse(mavenURL)["versioning"] as List<Node>)
val latest = versioning[0]["latest"] as List<Node>
val value = ((latest.first().value() as NodeList)[0]).toString()

val lastVersion = if (value.isNotEmpty()) value else ""

val actualVersionMajor = nightly_version.split(".").first()
val lastVersionMajor = lastVersion.split(".").first()

return if (actualVersionMajor == lastVersionMajor) lastVersion else ""
}

private fun updateConfigFileWithNewGVNightlyVersion(path: String, newVersion: String) {
val file = File(path)
var fileContent = file.readText()
fileContent = fileContent.replace(Regex("nightly_version.*=.*"),
"nightly_version = \"$newVersion\"")
file.writeText(fileContent)
println("${file.name} file updated")
}

}
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/Gecko.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

private object GeckoVersions {
const val nightly_version = "64.0.20180905100117"
internal object GeckoVersions {
const val nightly_version = "64.0.20181003100103"
}

object Gecko {
Expand Down
58 changes: 58 additions & 0 deletions buildSrc/src/main/java/GitHubClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.StandardCharsets

class GitHubClient(token: String) {

private val tokenHeader = "Authorization" to "Bearer $token"

companion object {
const val GITHUB_BASE_API = "https://api.github.com/repos"
}

fun createPullRequest(owner: String, repoName: String, bodyJson: String): Pair<Boolean, String> {
val url = "$GITHUB_BASE_API/$owner/$repoName/pulls"

return httpPOST(url, bodyJson, tokenHeader)
}

fun createIssue(owner: String, repoName: String, bodyJson: String): Pair<Boolean, String> {
val url = "$GITHUB_BASE_API/$owner/$repoName/issues"

return httpPOST(url, bodyJson, tokenHeader)
}

private fun httpPOST(urlString: String, json: String,
vararg headers: Pair<String, String>): Pair<Boolean, String> {
val url = URL(urlString)
val http = url.openConnection() as HttpURLConnection

http.setRequestMethod("POST")
http.setDoOutput(true)
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8")

headers.forEach {
http.setRequestProperty(it.first, it.second)
}

http.outputStream.use { os ->
os.write(json.toByteArray(StandardCharsets.UTF_8))
}

var responseSuccessful = true
var textResponse: String

textResponse = try {
http.inputStream.bufferedReader().readText()
} catch (e: Exception) {
responseSuccessful = false
http.errorStream.bufferedReader().readText()

}
return responseSuccessful to textResponse
}
}
111 changes: 111 additions & 0 deletions buildSrc/src/main/java/GitHubPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import groovy.json.JsonSlurper
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.task
import java.util.Properties

open class GitHubPlugin : Plugin<Project> {

private lateinit var client: GitHubClient

companion object {
private const val REPO_OWNER = "mozilla-mobile"
private const val REPO_NAME = "android-components"
private const val BASE_BRANCH_NAME = "master"
private const val HEAD = "MickeyMoz"
private const val TOKEN_FILE_PATH = ".github_token"
}

override fun apply(project: Project) {

project.task("openPR") {

doLast {
init(project)
val title = project.property("title").toString()
val body = project.property("body", "")
val branch = project.property("branch").toString()
val baseBranch = project.property("baseBranch", BASE_BRANCH_NAME)
val owner = project.property("owner", REPO_OWNER)
val repo = project.property("repo", REPO_NAME)
val user = project.property("botUser", HEAD)

createPullRequest(title, body, branch, baseBranch, owner, repo, user)
}

}

project.task("openIssue") {

doLast {
init(project)
val title = project.property("title").toString()
val body = project.property("body", "")
val owner = project.property("owner", REPO_OWNER)
val repo = project.property("repo", REPO_NAME)
createIssue(title, body, owner, repo)
}
}
}

private fun createPullRequest(title: String, body: String, branchName: String,
baseBranch: String, owner: String,
repoName: String, user: String) {
val bodyJson = ("{\n" +
" \"title\": \" $title\",\n" +
" \"body\": \"$body\",\n" +
" \"head\": \"$user:$branchName\",\n" +
" \"base\": \"$baseBranch\"\n" +
"}")

val result = client.createPullRequest(owner, repoName, bodyJson)

val successFul = result.first
val responseData = result.second

val stringToPrint = if (successFul) {
val pullRequestUrl = getUrlFromJSONString(responseData)
"Pull Request created take a look here $pullRequestUrl"
} else {
throw Exception("Unable to create pull request \n $responseData")
}
println(stringToPrint)
}

private fun createIssue(title: String, body: String, owner: String, repoName: String) {
val bodyJson = ("{\n" +
" \"title\": \"$title\",\n" +
" \"body\": \"$body\"" +
"}")

val result = client.createIssue(owner, repoName, bodyJson)
val successFul = result.first
val responseData = result.second

val stringToPrint = if (successFul) {
val issueUrl = getUrlFromJSONString(responseData)
"Issue Create take a look here $issueUrl"
} else {
throw Exception("Unable to create issue \n $responseData")
}

println(stringToPrint)
}

private fun getUrlFromJSONString(json: String): String {
val jsonResponse = JsonSlurper().parseText(json)
return (jsonResponse as Map<*, *>)["html_url"].toString()
}

private fun init(project: Project) {
val properties = Properties()
val tokenFile = project.property("tokenFile", TOKEN_FILE_PATH)
properties.load(project.rootProject.file(tokenFile).inputStream())
val token = properties.getProperty("token")
client = GitHubClient(token)
}
}