Last active
April 13, 2020 19:37
-
-
Save pierre-ernst/230e312f098fd04b70213441bffd1e4c to your computer and use it in GitHub Desktop.
Mass-assignment of the "false positive" label to GitHub issues found by searching for a specific keyword in the issue title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ kotlinc -cp github-api-1.110.jar:jackson-databind-2.10.3.jar:jackson-core-2.10.3.jar:jackson-annotations-2.10.3.jar:commons-lang3-3.10.jar:commons-io-2.6.jar -script fp-assign.kts | |
import java.io.File | |
import org.kohsuke.github.GitHubBuilder | |
if (args.size != 4) { | |
System.err.println("Usage: fp-assign <org> <repo> <keyword> <comment-file.md>") | |
System.exit(1) | |
} | |
if (System.getenv("GH_PAT").isNullOrEmpty()) { | |
System.err.println("Environment variable GH_PAT not found (GitHub personal access token).") | |
System.exit(2) | |
} | |
val github = GitHubBuilder().withOAuthToken(System.getenv("GH_PAT"), args[0]).build() | |
if (!github.isCredentialValid) { | |
System.err.println("Unable to authenticate to GitHub using the provided credentials.") | |
System.exit(3) | |
} | |
val comment = File(args[3]).readText(); | |
val repo = github.getRepository("${args[0]}/${args[1]}") | |
// Ensure the 'false positive' label exists | |
if (!repo.listLabels().toList().map { it.name }.contains("false positive")) { | |
repo.createLabel("false positive", "00FF00") | |
} | |
github.searchIssues().q("org:${args[0]} repo:${args[1]} is:open label:snyk in:title ${args[2]}") | |
.list().forEach { searchResult -> | |
val issue = repo.getIssue(searchResult.number) | |
val labels = issue.labels.map { it.name } | |
if (!labels.contains("false positive")) { | |
issue.setLabels("false positive", *labels.toTypedArray()) | |
} | |
issue.comment(comment) | |
issue.close() | |
println("GitHub issue updated: ${searchResult.title} ${searchResult.htmlUrl}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment