KocproZ / jenkins-discord

A post-build plugin that sends the build status to a Discord channel.
MIT License
39 stars 36 forks source link

notifications are not working #6

Closed andyczerwonka closed 7 years ago

andyczerwonka commented 7 years ago

I'd like to get a webhook notifying our Discord channel, how would we go about installing this into our Jenkins instance?

andyczerwonka commented 7 years ago

Ah, I found this release. Will report back if it works for us.

https://github.com/jammehcow/jenkins-discord/releases

andyczerwonka commented 7 years ago

It did not send a notification on a successful build - does it only send out notification on a state change?

andyczerwonka commented 7 years ago

It looks like it's not working at all. Given there is only one field to fill in I'm not sure how to debug it.

jupjohn commented 7 years ago

It works for me and I've tested it on OS X and Debian builds of Jenkins. I assume you're able to get access to the Jenkins CLI so check what you're getting when running a webhook-linked build. If it throws an exception the build should fail, otherwise check your webhook URL.

andyczerwonka commented 7 years ago

Ok, great, I'll try that. This version of Jenkins is on Windows unfortunately, but who knows, maybe we'll find something interesting. Are notifications supposed to be sent out on every state change?

jupjohn commented 7 years ago

Once a build is finished you'll get sent a notification. It's only on build completion.

andyczerwonka commented 7 years ago

On successful completion only? Or any build result?

jupjohn commented 7 years ago

Any build result. Will only show a build status as "Failure" or "Success" even if the failure is more specific.

andyczerwonka commented 7 years ago

Hi @jammehcow, here is the exception that is being thrown. Looks like a bad cast?

https://github.com/jammehcow/jenkins-discord/blob/c25a09d741c74dbef5cfb38a70c3e0613a96e598/src/main/java/nz/co/jammehcow/jenkinsdiscord/util/EmbedDescription.java#L46

ERROR: Build step failed with exception
java.lang.ClassCastException: java.lang.String cannot be cast to hudson.model.Run$Artifact
    at nz.co.jammehcow.jenkinsdiscord.util.EmbedDescription.<init>(EmbedDescription.java:46)
    at nz.co.jammehcow.jenkinsdiscord.WebhookPublisher.perform(WebhookPublisher.java:58)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:735)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:676)
    at hudson.model.Build$BuildExecution.cleanUp(Build.java:196)
    at hudson.model.Run.execute(Run.java:1782)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:405)
Build step 'Discord Notifier' marked build as failure
andyczerwonka commented 7 years ago

I'm running it on v2.60.3

andyczerwonka commented 7 years ago

A little analysis...

Here the artifactsList is declared as a list of String https://github.com/jammehcow/jenkins-discord/blob/c25a09d741c74dbef5cfb38a70c3e0613a96e598/src/main/java/nz/co/jammehcow/jenkinsdiscord/util/EmbedDescription.java#L19

and where the exception is thrown you are looping through that list, returning opaque Object and doing a hard cast to Run.Artifact. https://github.com/jammehcow/jenkins-discord/blob/c25a09d741c74dbef5cfb38a70c3e0613a96e598/src/main/java/nz/co/jammehcow/jenkinsdiscord/util/EmbedDescription.java#L45-L48

I'm guessing you're probably wanting to iterate over the artifacts, not the artifactsList.

andyczerwonka commented 7 years ago

Any suggestions on what I can do to diagnose? Is there maybe a logger I can use? I'm not sure why it's not sending anything and I'm not familiar with the Jenkins API. I could try anything you suggest.

andyczerwonka commented 7 years ago

I added some logging. The webhook URL is:

https://discordapp.com/api/webhooks/350304817333403650/BLANKED

It is the correct webhook URL on my Discord server. The payload that is being sent is:

{
  "avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png",
  "embeds": [{
    "color": 1681177,
    "footer": {
      "text": "Jenkins v2.60.3, Discord Notifier v1.0.0"
    },
    "description": "**Build:**  #273\n**Status:**  success\n**Changes:**\n\n*No changes.*\n\n**Artifacts:**\n - http://cgybuild1:8080/job/perspectives-ce%20develop/273/artifact/PrismDeployment/gui-app-3.3.0-base-5-410-ge02d7fc.zip\n - http://cgybuild1:8080/job/perspectives-ce%20develop/273/artifact/PrismDeployment/sp-server-3.3.0-base-5-410-ge02d7fc.zip\n - http://cgybuild1:8080/job/perspectives-ce%20develop/273/artifact/PrismDeployment/web-api-3.3.0-base-5-410-ge02d7fc.zip\n",
    "title": "perspectives-ce develop #273",
    "url": "http://cgybuild1:8080/job/perspectives-ce%20develop/273/"
  }],
  "username": "Jenkins"
}

I am also logging the response. I'm getting a 400 Bad Request, and body is looks like this:

{
    "embeds": [
        "0"
    ]
}

I've tried Postman, and I'm getting the same response. Is there anything in there that jumps out as something Discord can't handle? If not, anything else I should log to help with debugging?

andyczerwonka commented 7 years ago

Just to make sure the webhook works on the Discord side, I sent this message:

{
  "avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png",
  "content": "Testing from Postmans to make sure the Webhook works",
  "username": "Jenkins"
}

I successfully shows up in Discord. Response is 204 No Content. selection_307

andyczerwonka commented 7 years ago

Well it took me a while to figure this out. I wasn't getting notifications via a webhook. After hours of debugging, it turns out that the url field in the embeds structure must be externally visible. In our particular case, our Jenkins server is internal. There is technically no reason to limit this, so it's unfortunate. I've hacked your plugin to exclude it for now and I will report this to the Discord API channel.

Thanks for building this plugin.

andyczerwonka commented 7 years ago

Given the following discussion, it may make sense to extend the plugin to check for an internal URL. Or just make it an explicit option and render accordingly.

selection_308

A lot more discussion on the API thread now... most are tending to agree that it's maybe not a bug per say, but an issue as nobody can necessarily see why they would block it given we already get the spooky link handling.

jupjohn commented 7 years ago

My mistake. The URL does need to be externally accessible and I can't answer why they'd block it per say, but I assume it's passed through a regex pattern before the webhook is accepted and passed through. I'll add a checkbox for disabling sending the Jenkins URL which means artifacts will be listed by filename instead. Simple fix really.

andyczerwonka commented 7 years ago

Artifacts and content embedded links work just fine. It's only the title that isn't rendered as a link when the URL is missing. It's really a bug on their side, it should be let through. If I were you the only change I would make is to not set the URL.

andyczerwonka commented 7 years ago

I was going to send you a pull request to suppress the artifacts anyway, as it's not something that we need and just makes the notification little longer. I also understand the markdown links in the description, which I might take advantage of. I'd be happy to help out.

jupjohn commented 7 years ago

I'd be happy to add both the option to suppress sending a linked title (which won't be sent if it doesn't match a pattern anyways) and to change the artifacts list to a single link. Add a little bit more choice. Cheers.

andyczerwonka commented 7 years ago

There was more discussion late last night on the forum. Basically, it's not that the URL needs to be external, it needs to be valid. They deem address that is not domain + TLD as invalid, even if most internal Intranet names don't have it. This might influence your UI design in terms of the description, but I think the feature is still very useful.

andyczerwonka commented 7 years ago

Just an FYI, I threw in a temporary hack to make it work for us. No rush for the change, but when you release your next version I'll replace it with yours.

andyczerwonka/jenkins-discord#1

I also use Markdown links in the description.

selection_310