|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package asf |
| 21 | + |
| 22 | +import groovy.json.JsonException |
| 23 | +import groovy.json.JsonSlurper |
| 24 | +import java.io.FileNotFoundException |
| 25 | +import java.net.URI |
| 26 | +import org.gradle.api.Project |
| 27 | +import org.gradle.kotlin.dsl.extra |
| 28 | + |
| 29 | +class AsfProject( |
| 30 | + val apacheId: String, |
| 31 | + val name: String, |
| 32 | + val description: String, |
| 33 | + val website: String, |
| 34 | + val repository: String, |
| 35 | + val licenseUrl: String, |
| 36 | + val bugDatabase: String, |
| 37 | + val inceptionYear: Int, |
| 38 | +) { |
| 39 | + companion object { |
| 40 | + |
| 41 | + fun memoized(project: Project, asfName: String): AsfProject { |
| 42 | + val rootProject = project.rootProject |
| 43 | + return if (rootProject.extra.has("asfProject")) { |
| 44 | + unsafeCast(rootProject.extra["asfProject"]) as AsfProject |
| 45 | + } else { |
| 46 | + val asfProject = fetchProjectInformation(asfName) |
| 47 | + rootProject.extra["asfProject"] = asfProject |
| 48 | + return asfProject |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + internal fun <T : Any> unsafeCast(o: Any?): T { |
| 53 | + @Suppress("UNCHECKED_CAST") |
| 54 | + return o as T |
| 55 | + } |
| 56 | + |
| 57 | + internal fun <T : Any> parseJson(urlStr: String): T { |
| 58 | + val url = URI(urlStr).toURL() |
| 59 | + |
| 60 | + val headers = mutableMapOf<String, String>() |
| 61 | + if (url.host == "api.github.com" && url.protocol == "https") { |
| 62 | + val githubToken = System.getenv("GITHUB_TOKEN") |
| 63 | + if (githubToken != null) { |
| 64 | + // leverage the GH token to benefit from higher rate limits |
| 65 | + headers["Authorization"] = "Bearer $githubToken" |
| 66 | + // recommended for GH API requests |
| 67 | + headers["X-GitHub-Api-Version"] = "2022-11-28" |
| 68 | + headers["Accept"] = "application/vnd.github+json" |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // See org.codehaus.groovy.runtime.ResourceGroovyMethods.newReader(URL, Map) |
| 73 | + val params = mapOf("requestProperties" to headers) |
| 74 | + |
| 75 | + val slurper = JsonSlurper() |
| 76 | + var attempt = 0 |
| 77 | + while (true) { |
| 78 | + try { |
| 79 | + return unsafeCast(slurper.parse(params, url)) as T |
| 80 | + } catch (e: JsonException) { |
| 81 | + if (e.cause is FileNotFoundException) { |
| 82 | + throw e |
| 83 | + } |
| 84 | + if (attempt == 5) { |
| 85 | + throw e |
| 86 | + } |
| 87 | + Thread.sleep(1000L) |
| 88 | + } |
| 89 | + attempt++ |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** Retrieves the project name, for example `Polaris` using the lower-case project ID. */ |
| 94 | + internal fun fetchAsfProjectName(apacheId: String): String { |
| 95 | + val project = projectMap(apacheId) |
| 96 | + val isPodlingCurrent = project.containsKey("podling") && project["podling"] == "current" |
| 97 | + if (isPodlingCurrent) { |
| 98 | + val podling = podlingMap(apacheId) |
| 99 | + return podling["name"] as String |
| 100 | + } else { |
| 101 | + // top-level-project |
| 102 | + val committee = projectCommitteeMap(apacheId) |
| 103 | + return committee["display_name"] as String |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + internal fun projectCommitteeMap(apacheId: String): Map<String, Any> { |
| 108 | + val committeesAll: Map<String, Map<String, Any>> = |
| 109 | + parseJson("https://whimsy.apache.org/public/committee-info.json") |
| 110 | + val committees = unsafeCast<Map<String, Map<String, Any>>>(committeesAll["committees"]) |
| 111 | + return unsafeCast(committees[apacheId]) |
| 112 | + } |
| 113 | + |
| 114 | + internal fun projectMap(apacheId: String): Map<String, Any> { |
| 115 | + val projectsAll: Map<String, Map<String, Any>> = |
| 116 | + parseJson("https://whimsy.apache.org/public/public_ldap_projects.json") |
| 117 | + val projects = unsafeCast<Map<String, Map<String, Any>>>(projectsAll["projects"]) |
| 118 | + val project = |
| 119 | + projects[apacheId] |
| 120 | + ?: throw IllegalArgumentException( |
| 121 | + "No project '$apacheId' found in https://whimsy.apache.org/public/public_ldap_projects.json" |
| 122 | + ) |
| 123 | + return project |
| 124 | + } |
| 125 | + |
| 126 | + internal fun podlingMap(apacheId: String): Map<String, Any> { |
| 127 | + val podlingsAll: Map<String, Map<String, Any>> = |
| 128 | + parseJson("https://whimsy.apache.org/public/public_podlings.json") |
| 129 | + val podlings = unsafeCast<Map<String, Map<String, Any>>>(podlingsAll["podling"]) |
| 130 | + val podling = |
| 131 | + podlings[apacheId] |
| 132 | + ?: throw IllegalArgumentException( |
| 133 | + "No podling '$apacheId' found in https://whimsy.apache.org/public/public_podlings.json" |
| 134 | + ) |
| 135 | + return podling |
| 136 | + } |
| 137 | + |
| 138 | + internal fun fetchProjectInformation(apacheId: String): AsfProject { |
| 139 | + val project = projectMap(apacheId) |
| 140 | + val isPodlingCurrent = project.containsKey("podling") && project["podling"] == "current" |
| 141 | + |
| 142 | + val inceptionYear = |
| 143 | + (project["createTimestamp"] as String).subSequence(0, 4).toString().toInt() |
| 144 | + |
| 145 | + val projectName: String |
| 146 | + val description: String |
| 147 | + val website: String |
| 148 | + val repository: String |
| 149 | + val licenseUrl: String |
| 150 | + val bugDatabase: String |
| 151 | + if (isPodlingCurrent) { |
| 152 | + val podling = podlingMap(apacheId) |
| 153 | + projectName = podling["name"] as String |
| 154 | + description = podling["description"] as String |
| 155 | + val podlingStatus = unsafeCast(podling["podlingStatus"]) as Map<String, Any> |
| 156 | + website = podlingStatus["website"] as String |
| 157 | + // No repository for podlings?? |
| 158 | + repository = "https://github.com/apache/$apacheId.git" |
| 159 | + bugDatabase = "https://github.com/apache/$apacheId/issues" |
| 160 | + licenseUrl = "https://www.apache.org/licenses/LICENSE-2.0.txt" |
| 161 | + } else { |
| 162 | + // top-level-project |
| 163 | + val tlpPrj: Map<String, Any> = |
| 164 | + parseJson("https://projects.apache.org/json/projects/$apacheId.json") |
| 165 | + website = tlpPrj["homepage"] as String |
| 166 | + repository = (unsafeCast(tlpPrj["repository"]) as List<String>)[0] |
| 167 | + bugDatabase = tlpPrj["bug-database"] as String |
| 168 | + licenseUrl = tlpPrj["license"] as String |
| 169 | + |
| 170 | + val committee = projectCommitteeMap(apacheId) |
| 171 | + projectName = committee["display_name"] as String |
| 172 | + description = committee["description"] as String |
| 173 | + } |
| 174 | + |
| 175 | + return AsfProject( |
| 176 | + apacheId, |
| 177 | + projectName, |
| 178 | + description, |
| 179 | + website, |
| 180 | + repository, |
| 181 | + licenseUrl, |
| 182 | + bugDatabase, |
| 183 | + inceptionYear, |
| 184 | + ) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments