This repository was archived by the owner on Apr 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Add new command of 'add github labels to REPO' to bot (WIP) #72
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||||||||||||||
| import com.atlassian.jira.rest.client.api.domain.input.FieldInput; | ||||||||||||||||||||||||||||||||
| import com.atlassian.jira.rest.client.api.domain.input.TransitionInput; | ||||||||||||||||||||||||||||||||
| import com.atlassian.util.concurrent.Promise; | ||||||||||||||||||||||||||||||||
| import com.google.common.base.Strings; | ||||||||||||||||||||||||||||||||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||||||||||||||||||||||||||||||||
| import java.io.FileOutputStream; | ||||||||||||||||||||||||||||||||
| import java.io.OutputStreamWriter; | ||||||||||||||||||||||||||||||||
|
|
@@ -23,6 +24,7 @@ | |||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||
| import org.apache.commons.lang.StringUtils; | ||||||||||||||||||||||||||||||||
| import org.jenkinsci.backend.ircbot.fallback.FallbackMessage; | ||||||||||||||||||||||||||||||||
| import org.kohsuke.github.GHLabel; | ||||||||||||||||||||||||||||||||
| import org.kohsuke.github.GHOrganization.Permission; | ||||||||||||||||||||||||||||||||
| import org.kohsuke.github.GHTeamBuilder; | ||||||||||||||||||||||||||||||||
| import org.pircbotx.*; | ||||||||||||||||||||||||||||||||
|
|
@@ -36,14 +38,18 @@ | |||||||||||||||||||||||||||||||
| import org.kohsuke.github.GitHub; | ||||||||||||||||||||||||||||||||
| import org.pircbotx.output.OutputChannel; | ||||||||||||||||||||||||||||||||
| import org.pircbotx.output.OutputIRC; | ||||||||||||||||||||||||||||||||
| import org.yaml.snakeyaml.Yaml; | ||||||||||||||||||||||||||||||||
| import org.yaml.snakeyaml.constructor.Constructor; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import javax.net.ssl.HttpsURLConnection; | ||||||||||||||||||||||||||||||||
| import javax.net.ssl.SSLContext; | ||||||||||||||||||||||||||||||||
| import javax.net.ssl.TrustManager; | ||||||||||||||||||||||||||||||||
| import javax.net.ssl.X509TrustManager; | ||||||||||||||||||||||||||||||||
| import java.io.File; | ||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||
| import java.net.HttpURLConnection; | ||||||||||||||||||||||||||||||||
| import java.net.URI; | ||||||||||||||||||||||||||||||||
| import java.net.URL; | ||||||||||||||||||||||||||||||||
| import java.security.GeneralSecurityException; | ||||||||||||||||||||||||||||||||
| import java.security.cert.X509Certificate; | ||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +63,7 @@ | |||||||||||||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||||||||||||
| import java.util.regex.Matcher; | ||||||||||||||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import static java.util.Arrays.asList; | ||||||||||||||||||||||||||||||||
| import static java.util.Collections.emptyList; | ||||||||||||||||||||||||||||||||
|
|
@@ -77,6 +84,25 @@ public class IrcListener extends ListenerAdapter { | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private final Random random = new Random(System.currentTimeMillis()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private static final RepoLabels labelsYAML; | ||||||||||||||||||||||||||||||||
| static { | ||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| URL url = new URL("https://raw.githubusercontent.com/jenkinsci/.github/4f59a6219705df14067688df4f2a503e143cfa92/labels.yaml"); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||||||||||||||||||||||||||||||||
| con.setRequestMethod("GET"); | ||||||||||||||||||||||||||||||||
| Yaml yaml = new Yaml(new Constructor(RepoLabels.class)); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| labelsYAML = yaml.load(con.getInputStream()); | ||||||||||||||||||||||||||||||||
| con.disconnect(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||||||||||
| e.printStackTrace(); | ||||||||||||||||||||||||||||||||
| throw new ExceptionInInitializerError(e); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Map from the issue number to the time it was last mentioned. | ||||||||||||||||||||||||||||||||
| * Used so that we don't repeatedly mention the same issues. | ||||||||||||||||||||||||||||||||
|
|
@@ -169,6 +195,12 @@ private void handleDirectCommand(Channel channel, User sender, String payload) { | |||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| m = Pattern.compile("add github labels to (\\S+)",CASE_INSENSITIVE).matcher(payload); | ||||||||||||||||||||||||||||||||
| if (m.matches()) { | ||||||||||||||||||||||||||||||||
| addGithubLabels(channel, sender, m.group(1)); | ||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| m = Pattern.compile("fork (?:https://github\\.com/)?(\\S+)/(\\S+)(?: on github)?(?: as (\\S+))?",CASE_INSENSITIVE).matcher(payload); | ||||||||||||||||||||||||||||||||
| if (m.matches()) { | ||||||||||||||||||||||||||||||||
| forkGitHub(channel, sender, m.group(1),m.group(2),m.group(3), emptyList()); | ||||||||||||||||||||||||||||||||
|
|
@@ -892,6 +924,60 @@ private void renameGitHubRepo(Channel channel, User sender, String repo, String | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * @param repoName | ||||||||||||||||||||||||||||||||
| * Name of repo | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| boolean addGithubLabels(Channel channel, User sender, String repoName) { | ||||||||||||||||||||||||||||||||
| boolean result = false; | ||||||||||||||||||||||||||||||||
| OutputChannel out = channel.send(); | ||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| if (!isSenderAuthorized(channel,sender)) { | ||||||||||||||||||||||||||||||||
| insufficientPermissionError(channel); | ||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| GitHub github = GitHub.connect(); | ||||||||||||||||||||||||||||||||
| GHOrganization org = github.getOrganization(IrcBotConfig.GITHUB_ORGANIZATION); | ||||||||||||||||||||||||||||||||
| GHRepository repo = org.getRepository(repoName); | ||||||||||||||||||||||||||||||||
| if(repo == null) { | ||||||||||||||||||||||||||||||||
| out.message("Repository with name "+repoName+" does not exist "+IrcBotConfig.GITHUB_ORGANIZATION); | ||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| Map<String, GHLabel> ghLabels = repo.listLabels().asList().stream().collect(Collectors.toMap(GHLabel::getName, l -> l)); | ||||||||||||||||||||||||||||||||
| out.message("Setting labels for "+repoName); | ||||||||||||||||||||||||||||||||
| for (RepoLabels.Label label : labelsYAML.labels) { | ||||||||||||||||||||||||||||||||
| if (!Strings.isNullOrEmpty(label.oldname) && ghLabels.containsKey(label.oldname)) { // ex fix => bugfix | ||||||||||||||||||||||||||||||||
| if (ghLabels.containsKey(label.name)) { | ||||||||||||||||||||||||||||||||
| out.message("Old label of " + label.oldname + " still exists, so can't rename to " + label.name); | ||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } else if (ghLabels.containsKey(label.name)) { | ||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||
| * when updateLabel is published | ||||||||||||||||||||||||||||||||
| * repo.updateLabel( | ||||||||||||||||||||||||||||||||
| * Strings.isNullOrEmpty(label.oldname) ? label.name : label.oldname, | ||||||||||||||||||||||||||||||||
| * label.name, // newname | ||||||||||||||||||||||||||||||||
| * label.color, | ||||||||||||||||||||||||||||||||
| * label.description | ||||||||||||||||||||||||||||||||
| * ) | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
|
Comment on lines
+956
to
+964
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From hub4j/github-api#724 :
Suggested change
|
||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| repo.createLabel(label.name, label.color, label.description); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| out.message("Done Setting labels for "+repoName); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| result = true; | ||||||||||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||||||||||
| out.message("Failed to set labels a repository: "+e.getMessage()); | ||||||||||||||||||||||||||||||||
| e.printStackTrace(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * @param newName | ||||||||||||||||||||||||||||||||
| * If not null, rename a repository after a fork. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
26 changes: 26 additions & 0 deletions
26
src/main/java/org/jenkinsci/backend/ircbot/RepoLabels.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.jenkinsci.backend.ircbot; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| class RepoLabels { | ||
| public static class Label { | ||
| public final String name; | ||
| public final String oldname; | ||
| public final String color; | ||
| public final String description; | ||
|
|
||
| public Label() { | ||
| name = ""; | ||
| oldname = ""; | ||
| color = ""; | ||
| description = ""; | ||
| } | ||
| } | ||
|
|
||
| public final List<Label> labels; | ||
|
|
||
| public RepoLabels() { | ||
| this.labels = new ArrayList<>(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point to master branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
master doesn't have the file so it would 404, but yea the plan would be master