Skip to content

Commit

Permalink
android support
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetuska committed Oct 23, 2021
1 parent c3273cb commit 3b28ec1
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions scripts/removeGhPackageVersion.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env kotlin

@file:DependsOn("io.ktor:ktor-client-cio-jvm:1.6.3")
@file:DependsOn("io.ktor:ktor-client-gson:1.6.3")
@file:DependsOn("io.ktor:ktor-client-auth-jvm:1.6.3")

import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.features.auth.Auth
import io.ktor.client.features.auth.providers.BasicAuthCredentials
import io.ktor.client.features.auth.providers.basic
import io.ktor.client.features.defaultRequest
import io.ktor.client.features.json.GsonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.host
import io.ktor.client.request.parameter
import io.ktor.http.URLProtocol
import kotlinx.coroutines.runBlocking

val targetRepo = args.getOrNull(0) ?: error("Target repository not specified")
val targetVersion = args.getOrNull(1) ?: error("Target version not specified")
println("TargetRepo[$targetRepo] TargetVersion[$targetVersion]")

val ghClient = HttpClient(CIO) {
install(Auth) {
basic {
credentials {
BasicAuthCredentials(
username = System.getenv("GITHUB_LOGIN"),
password = System.getenv("GITHUB_OAUTH")
)
}
sendWithoutRequest { true }
}
}
install(JsonFeature) {
serializer = GsonSerializer()
}
defaultRequest {
followRedirects = true
host = "api.github.com"
url {
protocol = URLProtocol.HTTPS
}
header("Accept", "application/vnd.github.v3+json")
}
}

data class GHRepository(val id: String, val node_id: String, val name: String, val full_name: String)
data class GHPackage(val id: String, val name: String, val package_type: String, val repository: GHRepository)
data class GHPackageVersion(val id: String, val name: String)

runBlocking {
val packages = mutableListOf<GHPackage>().run {
var i = 0
while (true) {
val page = ghClient.get<List<GHPackage>>("/user/packages") {
parameter("package_type", "maven")
parameter("page", "${++i}")
}
if (page.isEmpty()) {
break
} else {
addAll(page.filter { it.repository.name == targetRepo })
}
}
toList()
}
val versions = packages.map {
it to mutableListOf<GHPackageVersion>().run {
var i = 0
while (true) {
val page = ghClient.get<List<GHPackageVersion>>("/user/packages/${it.package_type}/${it.name}/versions") {
parameter("package_type", "maven")
parameter("page", "${++i}")
}
if (page.isEmpty()) {
break
} else {
addAll(page)
}
}
toList()
}

}
versions.forEach { (pkg, ver) ->
if (ver.size > 1) {
ver.firstOrNull { it.name == targetVersion }?.let {
println("Deleting ${pkg.name}@${it.name}")
ghClient.delete<Unit>("/user/packages/${pkg.package_type}/${pkg.name}/versions/${it.id}")
}
} else if (ver.any { it.name == targetVersion }) {
println("Deleting ${pkg.name}")
ghClient.delete<Unit>("/user/packages/${pkg.package_type}/${pkg.name}")
}
}
}

0 comments on commit 3b28ec1

Please sign in to comment.