diff --git a/config.json b/config.json index 220562d..00fb214 100644 --- a/config.json +++ b/config.json @@ -8,9 +8,11 @@ "irc" : { "host": "worcester.irc.bnet.eu.org", "port": 6667, - "nickname": "tlangbot", + "nickname": "tlangbot1", "realname": "TLang Development Bot", - "channel": "#tlang" + "channels": { + "tlang" : "#tlang" + } }, "ntfy": { "endpoint": "http://ntfy.sh", diff --git a/source/app.d b/source/app.d index 9ec1756..68b17d3 100644 --- a/source/app.d +++ b/source/app.d @@ -48,6 +48,9 @@ void commitHandler(HTTPServerRequest request, HTTPServerResponse response) int replyCode = 200; replyJSON["output"]=""; + /* Channel to eventually send to */ + string toChannel; + try { /* Extract the received JSON */ @@ -65,9 +68,12 @@ void commitHandler(HTTPServerRequest request, HTTPServerResponse response) string authorEmail = authorBlock["email"].str(); string repositoryName = json["repository"]["full_name"].str(); + + /* Extract JUST the repository's name */ + toChannel = associations[json["repository"]["name"].str()]; string ircMessage = bold("["~repositoryName~"]")~" New commit "~commitMessage~" ("~commitID~") by "~italics(authorName)~" ("~authorEmail~") ["~underline(commitURL)~"]"; - ircBot.channelMessage(ircMessage, channelName); //TODO: Add IRC error handling + ircBot.channelMessage(ircMessage, toChannel); //TODO: Add IRC error handling /* Send message to NTFY server */ notifySH(ircMessage); @@ -88,6 +94,9 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) int replyCode = 200; replyJSON["output"]=""; + /* Channel to eventually send to */ + string toChannel; + try { /* Extract the received JSON */ @@ -102,6 +111,9 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) string issueAction = json["action"].str(); string repositoryName = issueBlock["repository"]["full_name"].str(); + /* Extract JUST the repository's name */ + toChannel = associations[json["repository"]["name"].str()]; + /* Opened a new issue */ if(cmp(issueAction, "opened") == 0) { @@ -110,7 +122,7 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) //TODO: Add IRC error handling string ircMessage = bold("["~repositoryName~"]")~setForeground(SimpleColor.GREEN)~" Opened issue"~resetForegroundBackground()~" '"~issueTitle~"' "~bold("#"~to!(string)(issueID))~" by "~italics(username)~" ["~underline(issueURL)~"]"; - ircBot.channelMessage(ircMessage, channelName); + ircBot.channelMessage(ircMessage, toChannel); /* Send message to NTFY server */ notifySH(ircMessage); @@ -123,7 +135,7 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) //TODO: Add IRC error handling string ircMessage = bold("["~repositoryName~"]")~setForeground(SimpleColor.RED)~" Closed issue"~resetForegroundBackground()~" '"~issueTitle~"' on issue "~bold("#"~to!(string)(issueID))~" by "~italics(username)~" ["~underline(issueURL)~"]"; - ircBot.channelMessage(ircMessage, channelName); + ircBot.channelMessage(ircMessage, toChannel); /* Send message to NTFY server */ notifySH(ircMessage); @@ -136,7 +148,7 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) //TODO: Add IRC error handling string ircMessage = bold("["~repositoryName~"]")~setForeground(SimpleColor.GREEN)~" Reopened issue"~resetForegroundBackground()~" '"~issueTitle~"' "~bold("#"~to!(string)(issueID))~" by "~italics(username)~" ["~underline(issueURL)~"]"; - ircBot.channelMessage(ircMessage, channelName); + ircBot.channelMessage(ircMessage, toChannel); /* Send message to NTFY server */ notifySH(ircMessage); @@ -159,7 +171,7 @@ void issueHandler(HTTPServerRequest request, HTTPServerResponse response) //TODO: Add IRC error handling string ircMessage = bold("["~repositoryName~"]")~" "~setForeground(SimpleColor.GREEN)~"New comment"~resetForegroundBackground()~" '"~italics(commentBody)~"' by "~italics(username)~" on issue "~bold("#"~to!(string)(issueID))~" ["~underline(issueURL)~"]"; - ircBot.channelMessage(ircMessage, channelName); + ircBot.channelMessage(ircMessage, toChannel); /* Send message to NTFY server */ notifySH(ircMessage); @@ -218,8 +230,8 @@ string ntfyServer, ntfyChannel; string serverHost; ushort serverPort; string nickname; -string channelName; - +string[] channels; +string[string] associations; /** * Sends a message to ntfy.sh (only if it is enabled) @@ -292,7 +304,21 @@ void main(string[] args) serverHost = ircBlock["host"].str(); serverPort = cast(ushort)(ircBlock["port"].integer()); nickname = ircBlock["nickname"].str(); - channelName = ircBlock["channel"].str(); + + /** + * Mapping between `repo -> #channel` + * + * Extract from the JSON, build the map + * and also construct a list of channels + * 9which we will use later to join + */ + JSONValue[string] channelAssociations = ircBlock["channels"].object(); + foreach(string repoName; channelAssociations.keys()) + { + associations[repoName] = channelAssociations[repoName].str(); + channels ~= associations[repoName]; + } + /* Attempt to parse ntfy.sh configuration */ @@ -345,9 +371,9 @@ void main(string[] args) //TODO: Clean this string up ircBot.command(new Message("", "USER", "giteabotweb giteabotweb irc.frdeenode.net :Tristan B. Kildaire")); - /* Join the requested channel */ + /* Join the requested channels */ Thread.sleep(dur!("seconds")(4)); - ircBot.joinChannel(channelName); + ircBot.joinChannel(channels); /* Setup the web server */