From 385fa9559e009ed03a2967c4f538424f781f3155 Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Tue, 11 Jul 2023 19:50:41 -0700 Subject: [PATCH] Bug 1842904 - Remove unused GitHubPlugin and GitHubClient --- mobile/android/android-components/build.gradle | 2 - .../buildSrc/src/main/java/Extentions.kt | 8 -- .../buildSrc/src/main/java/GitHubClient.kt | 55 ---------- .../buildSrc/src/main/java/GitHubPlugin.kt | 118 --------------------- 4 files changed, 183 deletions(-) delete mode 100644 mobile/android/android-components/buildSrc/src/main/java/Extentions.kt delete mode 100644 mobile/android/android-components/buildSrc/src/main/java/GitHubClient.kt delete mode 100644 mobile/android/android-components/buildSrc/src/main/java/GitHubPlugin.kt diff --git a/mobile/android/android-components/build.gradle b/mobile/android/android-components/build.gradle index e70e30e97e5b..0ed6c25e352f 100644 --- a/mobile/android/android-components/build.gradle +++ b/mobile/android/android-components/build.gradle @@ -352,8 +352,6 @@ tasks.register("ktlint", JavaExec) { args "components/**/*.kt" , "samples/**/*.kt", "!**/build/**/*.kt", "buildSrc/**/*.kt", "--baseline=ktlint-baseline.xml" } -apply plugin: GitHubPlugin - tasks.register("ktlintFormat", JavaExec) { group = "formatting" description = "Fix Kotlin code style deviations." diff --git a/mobile/android/android-components/buildSrc/src/main/java/Extentions.kt b/mobile/android/android-components/buildSrc/src/main/java/Extentions.kt deleted file mode 100644 index 2b9dbb09efd4..000000000000 --- a/mobile/android/android-components/buildSrc/src/main/java/Extentions.kt +++ /dev/null @@ -1,8 +0,0 @@ -/* 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 org.gradle.api.Project - -fun Project.property(names: String, defaultValue: String): String { - return if (hasProperty(names)) property(names).toString() else defaultValue -} diff --git a/mobile/android/android-components/buildSrc/src/main/java/GitHubClient.kt b/mobile/android/android-components/buildSrc/src/main/java/GitHubClient.kt deleted file mode 100644 index 3c5263f843fa..000000000000 --- a/mobile/android/android-components/buildSrc/src/main/java/GitHubClient.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* 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 { - val url = "$GITHUB_BASE_API/$owner/$repoName/pulls" - - return httpPOST(url, bodyJson, tokenHeader) - } - - fun createIssue(owner: String, repoName: String, bodyJson: String): Pair { - val url = "$GITHUB_BASE_API/$owner/$repoName/issues" - - return httpPOST(url, bodyJson, tokenHeader) - } - - @Suppress("TooGenericExceptionCaught") - private fun httpPOST(urlString: String, json: String, vararg headers: Pair): Pair { - val url = URL(urlString) - val http = url.openConnection() as HttpURLConnection - - http.requestMethod = "POST" - http.doOutput = 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 - val textResponse = try { - http.inputStream.bufferedReader().readText() - } catch (e: Exception) { - responseSuccessful = false - http.errorStream.bufferedReader().readText() - } - return responseSuccessful to textResponse - } -} diff --git a/mobile/android/android-components/buildSrc/src/main/java/GitHubPlugin.kt b/mobile/android/android-components/buildSrc/src/main/java/GitHubPlugin.kt deleted file mode 100644 index cdd10b0cd646..000000000000 --- a/mobile/android/android-components/buildSrc/src/main/java/GitHubPlugin.kt +++ /dev/null @@ -1,118 +0,0 @@ -/* 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 java.util.Properties - -open class GitHubPlugin : Plugin { - - 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 = "main" - private const val HEAD = "MickeyMoz" - private const val TOKEN_FILE_PATH = ".github_token" - } - - override fun apply(project: Project) { - project.tasks.register("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.tasks.register("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) - } - } - } - - @Suppress("TooGenericExceptionThrown", "LongParameterList") - 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) - } - - @Suppress("TooGenericExceptionThrown") - 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) - } -} -- 2.11.4.GIT