Skip to content

Instantly share code, notes, and snippets.

@halkeye
Last active December 22, 2020 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halkeye/7f30c8385f28c40f99f66967ab74b42b to your computer and use it in GitHub Desktop.
Save halkeye/7f30c8385f28c40f99f66967ab74b42b to your computer and use it in GitHub Desktop.
package io.jenkins.infra.repository_permissions_updater
import java.io.IOException;
import groovy.yaml.*
import groovy.json.*
@Grab('com.squareup.okhttp:okhttp:2.5.0')
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Credentials;
import static groovy.io.FileType.FILES
// import io.jenkins.infra.repository_permissions_updater.Definition;
// repo to permission file
repoInformation = [:];
// avoid creating several instances, should be singleon
client = new OkHttpClient();
jsonSlurper = new JsonSlurper();
// graphql body
graphql = """
{
organization(login: %s) {
repositories(first: 100, after: %s) {
pageInfo {
startCursor
hasNextPage
endCursor
}
edges {
node {
# __typename
# isArchived
# name
nameWithOwner
hasIssuesEnabled
}
}
}
}
}
"""
class GithubRequest {
String query;
}
class ProjectInfo {
File file;
Object yaml;
}
def fetchProjectPermissions() {
def permissions = new File("permissions/");
repoInformation = [:];
def files = [];
permissions.traverse(type: FILES, maxDepth: 0) { file -> {
if (file.toString().contains('/plugin-')) {
files.push(file);
}
}}
for (file in files) {
file.withReader { reader -> {
// Use parse method of YamlSlurper.
def yaml = new YamlSlurper().parse(reader)
if (yaml.github != null) {
repoInformation[yaml.github] = new ProjectInfo(file: file, yaml: yaml);
}
}}
}
}
def updateFromGithub(organization="jenkinsci") {
boolean hasNextPage = true;
String endCursor = null;
while (hasNextPage) {
def headersAsMap = [
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": Credentials.basic(System.getenv("GITHUB_USERNAME"), System.getenv("GITHUB_PASSWORD"))
]
def url = "https://api.github.com/graphql"
def jsonBodyAsText = new JsonBuilder(new GithubRequest( query: String.format(
graphql,
new JsonBuilder(organization).toPrettyString(),
new JsonBuilder(endCursor).toPrettyString(),
)
)).toString();
def okHttpRequest = new Request.Builder()
.headers(Headers.of(headersAsMap))
.url(url)
.post(RequestBody.create(MediaType.parse("application/json"), jsonBodyAsText))
.build()
def response = client.newCall(okHttpRequest).execute()
def jsonResponse = jsonSlurper.parseText(response.body().string());
if (jsonResponse.errors != null) {
throw new IOException(jsonResponse.errors.toString());
}
if (jsonResponse.message != null && jsonResponse.data == null) {
throw new IOException(jsonResponse.message);
}
def repositories = jsonResponse.data.organization.repositories
hasNextPage = repositories.pageInfo.hasNextPage;
endCursor = repositories.pageInfo.endCursor;
for (edge in repositories.edges) {
def repo = repoInformation[edge.node.nameWithOwner];
if (!repo) { continue; }
if (repo.yaml.issues) { continue; }
if (edge.node.hasIssuesEnabled) {
repo.file.write(repo.file.getText('UTF-8').trim() + "\nissues:\n type: github\n value: \"" + edge.node.nameWithOwner + "\"\n");
}
}
}
}
def updateFromJira(organization="jenkinsci") {
def headersAsMap = [
"Content-Type": "application/json",
"Accept": "application/json",
]
def url = "https://issues.jenkins.io/rest/api/2/project/JENKINS"
def okHttpRequest = new Request.Builder()
.headers(Headers.of(headersAsMap))
.url(url)
.get()
.build()
def response = client.newCall(okHttpRequest).execute()
def jsonResponse = jsonSlurper.parseText(response.body().string());
for (component in jsonResponse.components) {
def componentName = component.name.toLowerCase().replaceFirst(/-plugin$/, '') + "-plugin";
for (entry in repoInformation) {
def pluginComponentName = entry.value.yaml.name.toLowerCase().replaceFirst(/-plugin$/, "") + "-plugin";
if (componentName != pluginComponentName) { continue; }
def repo = repoInformation[entry.value.yaml.github];
if (!repo) { continue; }
if (repo.yaml.issues) { continue; }
repo.file.write(repo.file.getText('UTF-8').trim() + "\nissues:\n type: jira\n value: \"" + component.name + "\"\n");
}
}
}
def findPluginsNoIssues() {
for (entry in repoInformation) {
if (!entry.value.yaml.issues) {
println("[MISSING] " + entry.value.yaml.name + " is missing issues")
}
}
}
fetchProjectPermissions();
updateFromGithub();
fetchProjectPermissions();
updateFromJira();
fetchProjectPermissions();
findPluginsNoIssues();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment