Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipelines integartion #24

Open
lessless opened this issue Aug 6, 2016 · 14 comments
Open

Pipelines integartion #24

lessless opened this issue Aug 6, 2016 · 14 comments

Comments

@lessless
Copy link

lessless commented Aug 6, 2016

Hello,

Thank you for useful plugin. It will be really wonderful to be able to use it with pipelines

https://jenkins.io/blog/2016/05/25/update-plugin-for-pipeline/
https://github.com/jenkinsci/pipeline-plugin/blob/master/DEVGUIDE.md

@danielsaf
Copy link

danielsaf commented Oct 29, 2016

@lessless Hi, Could you find solution for Flowdoc with pipelines?

Thanks for answer:)

@lessless
Copy link
Author

@danielsaf no luck so far, I'm planning to open a topic on the CA support forums, because of supporting point&click interface is quite resourceful.

@lessless
Copy link
Author

lessless commented Nov 2, 2016

@danielsaf I was advised to leave a suggestion in customer support where other users can vote on it too
https://flowdock.uservoice.com/forums/36827-general/suggestions/16919152-add-pipelines-support-to-jenkins-plugin

@ndobson
Copy link

ndobson commented Jan 11, 2017

@lessless This is what I'm doing for now, assuming you have a unix node with curl installed, it replicates most of the functionality of flowdock inbox notifications. Add to your Jenkinsfile:

import groovy.json.JsonOutput
import java.net.URLEncoder
import hudson.model.Result
def notifyFlowdockInbox(apiToken, tags) {
    tags = tags.replaceAll("\\s","")
    // build status of null means successful
    def buildStatus =  currentBuild.result ? currentBuild.result : 'SUCCESS'
    def subject = "${env.JOB_BASE_NAME} build ${currentBuild.displayName.replaceAll("#", "")}"
    def fromAddress = ''
    switch (buildStatus) {
      case 'SUCCESS':
        def prevResult = currentBuild.getPreviousBuild() != null ? currentBuild.getPreviousBuild().getResult() : null;
        if (Result.FAILURE.toString().equals(prevResult) || Result.UNSTABLE.toString().equals(prevResult)) {
          subject += ' was fixed'
          fromAddress = '[email protected]'
          break
        }
        subject += ' was successful'
        fromAddress = '[email protected]'
        break
      case 'FAILURE':
        subject += ' failed'
        fromAddress = '[email protected]'
        break
      case 'UNSTABLE':
        subject += ' was unstable'
        fromAddress = '[email protected]'
        break
      case 'ABORTED':
        subject += ' was aborted'
        fromAddress = '[email protected]'
        break
      case 'NOT_BUILT':
        subject += ' was not built'
        fromAddress = '[email protected]'
      case 'FIXED':
        subject = ' was fixed'
        fromAddress = '[email protected]'
        break
    }
    StringBuilder content = new StringBuilder();
    content.append("<h3>").append(env.JOB_BASE_NAME).append("</h3>");
    content.append("Build: ").append(currentBuild.displayName).append("<br />");
    content.append("Result: <strong>").append(buildStatus).append("</strong><br />");
    content.append("URL: <a href=\"").append(env.BUILD_URL).append("\">").append(currentBuild.fullDisplayName).append("</a>").append("<br />");
    def flowdockURL = "https://api.flowdock.com/v1/messages/team_inbox/${apiToken}"
    def payload = JsonOutput.toJson([source : "Jenkins",
                                     project : env.JOB_BASE_NAME,
                                     from_address: fromAddress,
                                     from_name: 'CI',
                                     subject: subject,
                                     tags: tags,
                                     content: content,
                                     link: env.BUILD_URL
      								 ])
    sh """#!/bin/bash
echo "Sending Flowdock notification..."
curl -H \"Content-Type: application/json\" -X POST -s -d \'${payload}\' ${flowdockURL}
"""
}

Call it like this:
notifyFlowdockInbox("someflowtoken", "#atag, #anothertag")

@lessless
Copy link
Author

Awesome @danielsaf! Thank you for bringing that into life 👍
Do I have to install Groovy and any additional packages on the node?

@ndobson
Copy link

ndobson commented Jan 11, 2017

No it should just work like that in your Jenkinsfile

@lessless
Copy link
Author

@ndobson I'm afraid the script that we are using is prone to RCE exploitation because of notification is sent via bash script and StringBuilder doesn't escape quotes.
Something like ' rm -rf / in the displayName

@mletterle
Copy link

@lessless You can use the httpRequest step from the HTTP Request Plugin rather than shell out to curl, i.e.:

resp = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: payload, url: flowdockURL, validResponseCodes: "100:599"

See https://github.com/jenkinsci/http-request-plugin

@lessless
Copy link
Author

@mletterle thanks a lot!

@dleske
Copy link

dleske commented Jul 15, 2017

I hacked together a pretty basic shared library to make the function reusable across builds without copying/pasting. Comments etc. welcome.

It would be better to extend the Flowdock notification plugin to work with Pipeline, but that's for another day perhaps. :)

My thanks to the people who've contributed to this thread, especially @lessless for raising the issue and @ndobson for most of the code I used.

@cybertiger
Copy link

Thanks @dleske, not that useful for a lot of people because you slapped a GPL license on it (and it's not even your code) which would require anyone using it to slap a GPL license on all their Jenkinsfiles.

@dleske
Copy link

dleske commented May 8, 2018

@cybertiger: The intent wasn't to steal the ideas or code of the earlier posters. The minor work I did added some code and under the project I was on, any code written was obliged to be released under the GPL. The original code can still be used and modified, and I'm not sure using the library requires the users' Jenkinsfiles to be GPLed.

@cybertiger
Copy link

cybertiger commented May 8, 2018

Yeh no worries, the pipeline library is a cool thing.
Brushing up on my license knowledge, probably fine for internal / private use, not sure about if you publish anything though, the GPL isn't really intended for this sort of thing (it barely copes with Java libraries).
Yes, restrictions on licenses imposed by work contracts is an annoying thing.

@H2wk
Copy link

H2wk commented Nov 1, 2019

Note: that the original API this uses is depreciated. https://www.flowdock.com/api/team-inbox

I have added a pull request updating this functionality. Using the REST API & added in the ability to differentiate between inbox & flow messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants