From b567335fa00dba81cd5297c96e60d7a0bea66fe5 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 19 Oct 2021 23:48:26 +0200 Subject: [PATCH] A small utility to add the Hacktoberfest to your repositories --- kotlin-github-hacktoberfest-tags/.gitignore | 71 +++++++++++++++++ .../build.gradle.kts | 21 +++++ .../src/main/kotlin/Hacktoberfest.kt | 78 +++++++++++++++++++ sample-firebase/src/main/kotlin/Client.kt | 12 +-- .../index.firebase.module_firebase_compat.kt | 1 + sample-firebase/test | 2 +- settings.gradle.kts | 3 +- 7 files changed, 177 insertions(+), 11 deletions(-) create mode 100644 kotlin-github-hacktoberfest-tags/.gitignore create mode 100644 kotlin-github-hacktoberfest-tags/build.gradle.kts create mode 100644 kotlin-github-hacktoberfest-tags/src/main/kotlin/Hacktoberfest.kt diff --git a/kotlin-github-hacktoberfest-tags/.gitignore b/kotlin-github-hacktoberfest-tags/.gitignore new file mode 100644 index 0000000..b8dc315 --- /dev/null +++ b/kotlin-github-hacktoberfest-tags/.gitignore @@ -0,0 +1,71 @@ +.DS_Store +.idea/shelf +/confluence/target +/dependencies/repo +/android.tests.dependencies +/dependencies/android.tests.dependencies +/dist +/local +/gh-pages +/ideaSDK +/clionSDK +/android-studio/sdk +out/ +/tmp +/intellij +workspace.xml +*.versionsBackup +/idea/testData/debugger/tinyApp/classes* +/jps-plugin/testData/kannotator +/js/js.translator/testData/out/ +/js/js.translator/testData/out-min/ +/js/js.translator/testData/out-pir/ +.gradle/ +build/ +!**/src/**/build +!**/test/**/build +*.iml +!**/testData/**/*.iml +.idea/libraries/Gradle*.xml +.idea/libraries/Maven*.xml +.idea/artifacts/PILL_*.xml +.idea/artifacts/KotlinPlugin.xml +.idea/modules +.idea/runConfigurations/JPS_*.xml +.idea/runConfigurations/PILL_*.xml +.idea/runConfigurations/_FP_*.xml +.idea/runConfigurations/_MT_*.xml +.idea/libraries +.idea/modules.xml +.idea/gradle.xml +.idea/compiler.xml +.idea/inspectionProfiles/profiles_settings.xml +.idea/.name +.idea/artifacts/dist_auto_* +.idea/artifacts/dist.xml +.idea/artifacts/ideaPlugin.xml +.idea/artifacts/kotlinc.xml +.idea/artifacts/kotlin_compiler_jar.xml +.idea/artifacts/kotlin_plugin_jar.xml +.idea/artifacts/kotlin_jps_plugin_jar.xml +.idea/artifacts/kotlin_daemon_client_jar.xml +.idea/artifacts/kotlin_imports_dumper_compiler_plugin_jar.xml +.idea/artifacts/kotlin_main_kts_jar.xml +.idea/artifacts/kotlin_compiler_client_embeddable_jar.xml +.idea/artifacts/kotlin_reflect_jar.xml +.idea/artifacts/kotlin_stdlib_js_ir_* +.idea/artifacts/kotlin_test_js_ir_* +.idea/artifacts/kotlin_stdlib_wasm_* +.idea/jarRepositories.xml +.idea/csv-plugin.xml +.idea/libraries-with-intellij-classes.xml +node_modules/ +.rpt2_cache/ +libraries/tools/kotlin-test-js-runner/lib/ +local.properties +buildSrcTmp/ +distTmp/ +outTmp/ +/test.output +/kotlin-native/dist +kotlin-ide/ \ No newline at end of file diff --git a/kotlin-github-hacktoberfest-tags/build.gradle.kts b/kotlin-github-hacktoberfest-tags/build.gradle.kts new file mode 100644 index 0000000..41a2cc2 --- /dev/null +++ b/kotlin-github-hacktoberfest-tags/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + kotlin("jvm") version "1.5.31" + kotlin("plugin.serialization") version "1.5.31" +} + +group = "nl.jlengrand" +version = "1.0-SNAPSHOT" +val ktorVersion = "1.6.4" + +repositories { + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib")) + + implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-cio:$ktorVersion") + implementation("io.ktor:ktor-client-serialization:$ktorVersion") + +} \ No newline at end of file diff --git a/kotlin-github-hacktoberfest-tags/src/main/kotlin/Hacktoberfest.kt b/kotlin-github-hacktoberfest-tags/src/main/kotlin/Hacktoberfest.kt new file mode 100644 index 0000000..25bdc1c --- /dev/null +++ b/kotlin-github-hacktoberfest-tags/src/main/kotlin/Hacktoberfest.kt @@ -0,0 +1,78 @@ +import io.ktor.client.* +import io.ktor.client.features.json.* +import io.ktor.client.features.json.serializer.* +import io.ktor.client.request.* +import io.ktor.http.* +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +suspend fun main(){ + val user = "adyen-examples" + val repoType = RepoType.orgs + + val repositories = getRepos(user, repoType) + repositories.forEach { applyTag(it.fullName) } + + client.close() +} + +val token: String = System.getenv("GITHUB_HACKTOBERFEST_TOKEN") +val client = HttpClient(){ install(JsonFeature) { + serializer = KotlinxSerializer(kotlinx.serialization.json.Json { + prettyPrint = true + isLenient = true + ignoreUnknownKeys = true + }) + } +} + +enum class RepoType { users, orgs } + +@Serializable +data class Tags(val names : List){ + private val hacktoberfest = "hacktoberfest" + fun isFun() = this.names.contains(hacktoberfest) + fun addHacktoberfest() = if (isFun()) this else Tags(this.names + hacktoberfest) +} + +@Serializable +data class Repository(val id : Long, @SerialName("full_name") val fullName : String ) + +suspend fun getRepos(owner: String, repoType: RepoType): List { + val repos: List = client.get("https://api.github.com/$repoType/${owner}/repos?type=owner&sort=updated&per_page=10"){ + headers { + append(HttpHeaders.Accept, "application/vnd.github.mercy-preview+json") + } + } + return repos +} + +suspend fun applyTag(ownerAndRepo: String){ + val tags = readTags(ownerAndRepo) + if(!tags.isFun()) { + println("Applying tag to $ownerAndRepo") + println(setTags(ownerAndRepo, tags.addHacktoberfest())) + } + else println("Tag already found. Skipping $ownerAndRepo") +} + +suspend fun readTags(ownerAndRepo: String): Tags { + val tags: Tags = client.get("https://api.github.com/repos/$ownerAndRepo/topics"){ + headers { + append(HttpHeaders.Accept, "application/vnd.github.mercy-preview+json") + } + } + return tags +} + +suspend fun setTags(ownerAndRepo: String, tags: Tags): Tags { + val newTags: Tags = client.put("https://api.github.com/repos/$ownerAndRepo/topics"){ + headers { + append(HttpHeaders.Accept, "application/vnd.github.mercy-preview+json") + append(HttpHeaders.ContentType, ContentType.Application.Json) + append(HttpHeaders.Authorization, "token $token") + } + body = tags + } + return newTags +} \ No newline at end of file diff --git a/sample-firebase/src/main/kotlin/Client.kt b/sample-firebase/src/main/kotlin/Client.kt index c1fc973..7b39996 100644 --- a/sample-firebase/src/main/kotlin/Client.kt +++ b/sample-firebase/src/main/kotlin/Client.kt @@ -24,19 +24,13 @@ external fun sorted(a: Array): Boolean fun main() { // startFirebase() window.onload = { + val firebaseConfig: Json = json() + val fire = initializeApp(firebaseConfig) + console.log(fire) console.log(sorted(arrayOf(1, 2, 3))) - startFirebase(); document.body?.sayHello() } } -fun startFirebase(){ - val firebaseConfig: Json = json( - ) - - val fire = initializeApp(firebaseConfig) - console.log(fire) -} - fun Node.sayHello() { append { div { diff --git a/sample-firebase/src/main/kotlin/index.firebase.module_firebase_compat.kt b/sample-firebase/src/main/kotlin/index.firebase.module_firebase_compat.kt index 1350a80..157c7e5 100644 --- a/sample-firebase/src/main/kotlin/index.firebase.module_firebase_compat.kt +++ b/sample-firebase/src/main/kotlin/index.firebase.module_firebase_compat.kt @@ -1,4 +1,5 @@ @file:JsQualifier("firebase") +@file:JsNonModule @file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS") import kotlin.js.* diff --git a/sample-firebase/test b/sample-firebase/test index a2814b3..e5d14b0 160000 --- a/sample-firebase/test +++ b/sample-firebase/test @@ -1 +1 @@ -Subproject commit a2814b311ee9112ae5a40e00591ba64da52019f3 +Subproject commit e5d14b0654d433de65c610e032c9960441eac2e0 diff --git a/settings.gradle.kts b/settings.gradle.kts index c36d4cc..2c1b88f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1,3 @@ rootProject.name = "kotlin-samples" -include("sample-firebase") \ No newline at end of file +include("sample-firebase") +include("kotlin-github-hacktoberfest-tags")