Skip to content

Instantly share code, notes, and snippets.

@huxi
Last active February 19, 2016 12:30
Show Gist options
  • Save huxi/5622330 to your computer and use it in GitHub Desktop.
Save huxi/5622330 to your computer and use it in GitHub Desktop.
Gradle announcer for https://pushover.net/Put this into ~/.gradle/init.d/PushoverAnnouncer.gradle
import org.gradle.api.plugins.announce.Announcer
import org.gradle.api.plugins.announce.internal.AnnouncingBuildListener
import org.slf4j.Logger
import org.slf4j.LoggerFactory
def pushoverAppToken = 'YourAppToken'
def pushoverUser = 'YourUserToken'
class PushoverAnnouncer implements Announcer {
private static final Logger logger = LoggerFactory.getLogger(PushoverAnnouncer)
private final String appToken
private final String user
PushoverAnnouncer(String appToken, String user) {
this.appToken = appToken
this.user = user
}
void send(String title, String message) {
HttpURLConnection connection
try {
connection = new URL('https://api.pushover.net/1/messages.json').openConnection()
connection.requestMethod = "POST"
connection.doInput = true
connection.doOutput = true
connection.useCaches = false
String content = 'token=' + URLEncoder.encode(appToken, 'UTF-8') +
'&user=' + URLEncoder.encode(user, 'UTF-8')
if(title) {
content = content + '&title=' + URLEncoder.encode(title, 'UTF-8')
}
if(message) {
content = content + '&message=' + URLEncoder.encode(message, 'UTF-8')
}
logger.info("About to write '$content'.")
connection.outputStream.withWriter { out ->
out.write content
}
logger.info("Successfully sent '$message'.")
logger.debug connection.inputStream.getText("UTF-8")
// It seems that getText needs to be executed
// otherwise the call isn't performed.
} catch (Throwable t) {
logger.warn("Failed to notify sender: '$t'")
} finally {
connection?.disconnect()
}
}
}
gradle.projectsEvaluated {
// don't send notifications for buildSrc or in case of continuous build
if(gradle.rootProject.rootDir.name != 'buildSrc' && !gradle.startParameter.continuous) {
PushoverAnnouncer announcer = new PushoverAnnouncer(pushoverAppToken, pushoverUser)
AnnouncingBuildListener listener = new AnnouncingBuildListener(announcer)
gradle.addListener(listener)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment