Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.112</version>
<version>1.116</version>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
Expand Down Expand Up @@ -163,6 +163,12 @@
<version>3.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>

</dependencies>

<repositories>
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/org/jenkinsci/backend/ircbot/IrcListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point to master branch?

Copy link
Copy Markdown
Member Author

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


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.
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From hub4j/github-api#724 :

Suggested change
/*
* when updateLabel is published
* repo.updateLabel(
* Strings.isNullOrEmpty(label.oldname) ? label.name : label.oldname,
* label.name, // newname
* label.color,
* label.description
* )
*/
ghLabels.get(Strings.isNullOrEmpty(label.oldname) ? label.name : label.oldname)
.update()
.name(label.name) // newname
.color(label.color)
.description(label.description)
.done();

} 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.
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/jenkinsci/backend/ircbot/RepoLabels.java
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<>();
}
}