Created
March 5, 2021 02:10
-
-
Save Hopefuls/2c6c0daaedd77f24a27bb3aaf7d49cad to your computer and use it in GitHub Desktop.
Things for github webhooks, done for Javacord
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
package org.normies.pepegalog.webhooking; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import org.javacord.api.entity.channel.ServerTextChannel; | |
import org.json.JSONObject; | |
import org.normies.pepegalog.Main; | |
import org.normies.pepegalog.utils.RequestManager; | |
import org.normies.pepegalog.utils.ResponseManager; | |
import java.io.IOException; | |
public class GitHubContext implements HttpHandler { | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
RequestManager requestManager = new RequestManager(exchange); | |
ResponseManager responseManager = new ResponseManager(exchange); | |
System.out.println("Received!! hello!"); | |
JSONObject object = requestManager.asJson(); | |
responseManager.writeResponse("thanks lol"); | |
responseManager.setResponseCode(200); | |
HandleLog.handleLogging(object, requestManager.getHeaders().getFirst("X-GitHub-Event")); | |
} | |
} |
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
package org.normies.pepegalog.webhooking; | |
import org.javacord.api.entity.channel.ServerTextChannel; | |
import org.javacord.api.entity.message.embed.EmbedBuilder; | |
import org.json.JSONObject; | |
import org.normies.pepegalog.Main; | |
import org.normies.pepegalog.webhooking.handlers.IssueComment; | |
import org.normies.pepegalog.webhooking.handlers.Issues; | |
import org.normies.pepegalog.webhooking.handlers.Repository; | |
public class HandleLog { | |
public static void handleLogging(JSONObject object, String event) { | |
ServerTextChannel channel = Main.api.getServerTextChannelById(Main.githubLog).get(); | |
EmbedBuilder eb; | |
System.out.println(event); | |
switch (event) { | |
case "issues": | |
eb = new Issues(object).handle(); | |
channel.sendMessage(eb); | |
break; | |
case "issue_comment": | |
eb = new IssueComment(object).handle(); | |
channel.sendMessage(eb); | |
break; | |
case "repository": | |
eb = new Repository(object).handle(); | |
channel.sendMessage(eb); | |
break; | |
default: | |
System.out.println("======================================"); | |
System.out.println("could not find any event"); | |
System.out.println("event: "+event); | |
} | |
} | |
public static boolean eic(String val1, String val2comp) { | |
return val1.equalsIgnoreCase(val2comp); | |
} | |
} |
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
package org.normies.pepegalog.webhooking.handlers; | |
import org.javacord.api.entity.message.embed.EmbedBuilder; | |
import org.json.JSONObject; | |
import java.awt.*; | |
public class IssueComment { | |
private final JSONObject data; | |
private final JSONObject commentData; | |
private final JSONObject orgData; | |
private final JSONObject issueData; | |
private final JSONObject commentAuthorData; | |
private final JSONObject repoData; | |
public IssueComment(JSONObject data) { | |
this.data = data; | |
this.commentData = this.data.getJSONObject("comment"); | |
this.issueData = this.data.getJSONObject("issue"); | |
this.orgData = this.data.getJSONObject("organization"); | |
this.commentAuthorData = this.commentData.getJSONObject("user"); | |
this.repoData = this.data.getJSONObject("repository"); | |
} | |
public final EmbedBuilder handle() { | |
switch (this.data.getString("action").toLowerCase()) { | |
case "created": | |
System.out.println("created"); | |
return handleCreated(); | |
default: | |
return handleDefault(); | |
} | |
} | |
private EmbedBuilder handleCreated() { | |
EmbedBuilder eb = new EmbedBuilder() | |
.setColor(Color.blue) | |
.setAuthor(this.commentAuthorData.getString("login"), | |
this.commentAuthorData.getString("html_url"), | |
this.commentAuthorData.getString("avatar_url")) | |
.setFooter(this.orgData.getString("login")+" \u2022 "+this.repoData.getString("full_name"), | |
this.orgData.getString("avatar_url")) | |
.setTitle("New comment | #"+this.issueData.getInt("number")) | |
.setUrl(this.commentData.getString("html_url")) | |
.setDescription(this.commentData.getString("body")+".."); | |
return eb; | |
} | |
private EmbedBuilder handleDefault() { | |
return new EmbedBuilder().setColor(Color.WHITE).setDescription("Default webhand"); | |
} | |
} |
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
package org.normies.pepegalog.webhooking.handlers; | |
import org.javacord.api.entity.message.embed.EmbedBuilder; | |
import org.json.JSONObject; | |
import java.awt.*; | |
public class Issues { | |
private final JSONObject data; | |
private final JSONObject orgData; | |
private final JSONObject issueData; | |
private final JSONObject authorData; | |
private final JSONObject repoData; | |
public Issues(JSONObject data) { | |
this.data = data; | |
this.issueData = this.data.getJSONObject("issue"); | |
this.orgData = this.data.getJSONObject("organization"); | |
this.authorData = this.data.getJSONObject("sender"); | |
this.repoData = this.data.getJSONObject("repository"); | |
} | |
public final EmbedBuilder handle() { | |
switch (this.data.getString("action").toLowerCase()) { | |
case "opened": | |
return handleOpened(); | |
case "closed": | |
return handleClosed(); | |
case "reopened": | |
return handleReopened(); | |
default: | |
return handleDefault(); | |
} | |
} | |
private EmbedBuilder handleOpened() { | |
EmbedBuilder eb = new EmbedBuilder() | |
.setColor(Color.green) | |
.setAuthor(this.authorData.getString("login"), | |
this.authorData.getString("html_url"), | |
this.authorData.getString("avatar_url")) | |
.setFooter(this.orgData.getString("login")+" \u2022 "+this.repoData.getString("full_name"), | |
this.orgData.getString("avatar_url")) | |
.setTitle("New issue opened | #"+this.issueData.getInt("number")) | |
.setUrl(this.issueData.getString("html_url")) | |
.setDescription(this.issueData.getString("body")); | |
return eb; | |
} | |
private EmbedBuilder handleClosed() { | |
EmbedBuilder eb = new EmbedBuilder() | |
.setColor(Color.red) | |
.setAuthor(this.authorData.getString("login"), | |
this.authorData.getString("html_url"), | |
this.authorData.getString("avatar_url")) | |
.setFooter(this.orgData.getString("login")+" \u2022 "+this.repoData.getString("full_name"), | |
this.orgData.getString("avatar_url")) | |
.setTitle("Issue closed | #"+this.issueData.getInt("number")) | |
.setUrl(this.issueData.getString("html_url")); | |
// .setDescription(this.issueData.getString("body").substring(0, this.issueData.getString("body").length()/4)+".."); | |
return eb; | |
} | |
private EmbedBuilder handleReopened() { | |
EmbedBuilder eb = new EmbedBuilder() | |
.setColor(Color.orange) | |
.setAuthor(this.authorData.getString("login"), | |
this.authorData.getString("html_url"), | |
this.authorData.getString("avatar_url")) | |
.setFooter(this.orgData.getString("login")+" \u2022 "+this.repoData.getString("full_name"), | |
this.orgData.getString("avatar_url")) | |
.setTitle("Issue reopened | #"+this.issueData.getInt("number")) | |
.setUrl(this.issueData.getString("html_url")); | |
// .setDescription(this.issueData.getString("body").substring(0, this.issueData.getString("body").length()/4)+".."); | |
return eb; | |
} | |
private EmbedBuilder handleDefault() { | |
return new EmbedBuilder().setColor(Color.WHITE).setDescription("Default webhand"); | |
} | |
} |
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
package org.normies.pepegalog.webhooking.handlers; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import org.javacord.api.entity.message.embed.EmbedBuilder; | |
import org.json.JSONObject; | |
import org.normies.pepegalog.Main; | |
import org.normies.pepegalog.utils.Misc; | |
import java.awt.*; | |
import java.io.IOException; | |
public class Repository { | |
private final JSONObject data; | |
private final JSONObject orgData; | |
private final JSONObject authorData; | |
private final JSONObject repoData; | |
public Repository(JSONObject data) { | |
this.data = data; | |
this.orgData = this.data.getJSONObject("organization"); | |
this.authorData = this.data.getJSONObject("sender"); | |
this.repoData = this.data.getJSONObject("repository"); | |
} | |
public final EmbedBuilder handle() { | |
switch (this.data.getString("action").toLowerCase()) { | |
case "created": | |
return handleCreated(); | |
default: | |
return handleDefault(); | |
} | |
} | |
private EmbedBuilder handleCreated() { | |
EmbedBuilder eb = new EmbedBuilder() | |
.setColor(Color.blue) | |
.setAuthor(this.authorData.getString("login"), | |
this.authorData.getString("html_url"), | |
this.authorData.getString("avatar_url")) | |
.setFooter(this.orgData.getString("login")+" \u2022 "+this.repoData.getString("full_name"), | |
this.orgData.getString("avatar_url")) | |
.setTitle("New Repository created | "+this.repoData.getString("name")) | |
.setUrl(this.repoData.getString("html_url")); | |
StringBuilder sb = new StringBuilder(); | |
if (this.repoData.getBoolean("fork")) { | |
Request request = new Request.Builder().url(this.repoData.getString("url")).build(); | |
try { | |
Response response = Main.client.newCall(request).execute(); | |
JSONObject object = new JSONObject(response.body().string()); | |
JSONObject parent = object.getJSONObject("parent"); | |
String repoUrl = "https://github.com/"+parent.getString("full_name"); | |
sb.append("**Forked?** "+(this.repoData.getBoolean("fork") ? Misc.getYes() : Misc.getNo())).append("from ["+parent.getString("name")+"]("+repoUrl+")").append("\n"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
sb.append("**Forked?** "+(this.repoData.getBoolean("fork") ? Misc.getYes() : Misc.getNo())).append("\n"); | |
} | |
sb.append("**Private?** "+(this.repoData.getBoolean("private")? Misc.getYes() : Misc.getNo())); | |
eb.setDescription(sb.toString()); | |
return eb; | |
} | |
private EmbedBuilder handleDefault() { | |
return new EmbedBuilder().setColor(Color.WHITE).setDescription("Default webhand"); | |
} | |
} |
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
package org.normies.pepegalog.webhooking; | |
import com.sun.net.httpserver.HttpServer; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
public class Server { | |
public static HttpServer server; | |
public static void startServer() { | |
try { | |
server = HttpServer.create(new InetSocketAddress(5858), 0); | |
server.createContext("/githook", new GitHubContext()); | |
server.start(); | |
System.out.println(server.getAddress().getHostName()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment