integrations: Add Gocd webhook integration.

Fixes #38.
This commit is contained in:
Balaji2198
2018-01-28 22:39:08 +05:30
committed by Tim Abbott
parent a583733723
commit e1eabe286a
11 changed files with 261 additions and 1 deletions

View File

@@ -30,7 +30,7 @@ in bursts.
**Full feature changelog:**
- New integrations: ErrBot, Google Code-In, Opbeat, Groove, Raygun,
- New integrations: ErrBot, GoCD, Google Code-In, Opbeat, Groove, Raygun,
Insping, Dropbox, Front, Intercom, Statuspage.io, Flock and Beeminder.
- The local uploads backend now does the same security checks that the
S3 backend did before serving files to users.

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -315,6 +315,7 @@ WEBHOOK_INTEGRATIONS = [
stream_name='github'
),
WebhookIntegration('gitlab', ['version-control'], display_name='GitLab'),
WebhookIntegration('gocd', ['continuous-integration'], display_name='GoCD'),
WebhookIntegration('gogs', ['version-control'], stream_name='commits'),
WebhookIntegration('gosquared', ['marketing'], display_name='GoSquared'),
WebhookIntegration('greenhouse', ['hr'], display_name='Greenhouse'),

View File

View File

@@ -0,0 +1,22 @@
Zulip supports integration with GoCD and can notify you of
your build statuses.
1. {!create-stream.md!}
1. {!create-bot-construct-url-indented.md!}
1. Add the following to your `Config.XML` file.
```
<pipeline name="mypipeline">
<trackingtool link="<URL constructed above>" regex="##(\d+)"/>
...
</pipeline>
```
Push this change to your repository. For further information,
see [GoCD's documentation](https://docs.gocd.org/current/integration/).
{!congrats.md!}
![](/static/images/integrations/gocd/001.png)

View File

@@ -0,0 +1,18 @@
{
"build_details": {
"_links": {
"job": {
"href": "https://ci.example.com/go/tab/build/detail/pipelineName/pipelineCounter/stageName/stageCounter/jobName"
},
"stage": {
"href": "https://ci.example.com/go/pipelines/pipelineName/pipelineCounter/stageName/stageCounter"
},
"pipeline": {
"href": "https://ci.example.com/go/tab/pipeline/history/pipelineName"
}
},
"pipeline_name": "pipelineName",
"stage_name": "stageName",
"job_name": "jobName"
}
}

View File

@@ -0,0 +1,59 @@
{
"build_cause": {
"approver": "",
"material_revisions": [
{
"modifications": [
{
"email_address": null,
"id": 7225,
"modified_time": 1435728005000,
"user_name": "Balaji B <balaji@example.com>",
"comment": "my hola mundo changes",
"revision": "a788f1876e2e1f6e5a1e91006e75cd1d467a0edb"
}
],
"material": {
"description": "URL: https://github.com/gocd/gocd, Branch: master",
"fingerprint": "61e2da369d0207a7ef61f326eed837f964471b35072340a03f8f55d993afe01d",
"type": "Git",
"id": 4
},
"changed": true
}
],
"trigger_forced": false,
"trigger_message": "modified by Balaji <balaji@example.com>"
},
"name": "PipelineName",
"natural_order": 1,
"can_run": false,
"comment": null,
"stages": [
{
"name": "stage1",
"approved_by": "changes",
"jobs": [
{
"name": "jsunit",
"result": "Passed",
"state": "Completed",
"id": 1,
"scheduled_date": 1398332981981
}
],
"can_run": false,
"result": "Passed",
"approval_type": "success",
"counter": "1",
"id": 1,
"operate_permission": false,
"rerun_of_counter": null,
"scheduled": true
}
],
"counter": 1,
"id": 1,
"preparing_to_schedule": false,
"label": "14.1.0.1-b14a81825d081411993853ea5ea45266ced578b4"
}

View File

@@ -0,0 +1,59 @@
{
"build_cause": {
"approver": "anonymous",
"material_revisions": [
{
"modifications": [
{
"email_address": null,
"id": 1998,
"modified_time": 1434957613000,
"user_name": "User Name <username123@example.com>",
"comment": "my hola mundo changes",
"revision": "f6e7a3899c55e1682ffb00383bdf8f882bcee2141e79a8728254190a1fddcf4f"
}
],
"material": {
"description": "URL: https://github.com/gocd/gocd, Branch: master",
"fingerprint": "61e2da369d0207a7ef61f326eed837f964471b35072340a03f8f55d993afe01d",
"type": "Git",
"id": 4
},
"changed": true
}
],
"trigger_forced": false,
"trigger_message": "modified by User Name <username123@example.com>"
},
"name": "PipelineName",
"natural_order": 8,
"can_run": false,
"comment": null,
"stages": [
{
"name": "stage1",
"approved_by": "changes",
"jobs": [
{
"name": "job123",
"result": "Failed",
"state": "Completed",
"id": 21,
"scheduled_date": 1436172201081
}
],
"can_run": false,
"result": "Failed",
"approval_type": "success",
"counter": "1",
"id": 21,
"operate_permission": false,
"rerun_of_counter": null,
"scheduled": true
}
],
"counter": 1,
"id": 21,
"preparing_to_schedule": false,
"label": "6"
}

View File

@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import urllib
from typing import Text
from zerver.lib.test_classes import WebhookTestCase
from zerver.models import get_realm, get_user
class GocdHookTests(WebhookTestCase):
STREAM_NAME = 'gocd'
URL_TEMPLATE = "/api/v1/external/gocd?stream={stream}&api_key={api_key}"
FIXTURE_DIR_NAME = 'gocd'
TOPIC = 'https://github.com/gocd/gocd'
def test_gocd_message(self) -> None:
expected_message = (u"Author: Balaji B <balaji@example.com>\n"
u"Build status: Passed :thumbs_up:\n"
u"Details: [build log](https://ci.example.com"
u"/go/tab/pipeline/history/pipelineName)\n"
u"Comment: my hola mundo changes")
self.send_and_test_stream_message(
'pipeline',
self.TOPIC,
expected_message,
content_type="application/x-www-form-urlencoded"
)
def test_failed_message(self) -> None:
expected_message = (u"Author: User Name <username123@example.com>\n"
u"Build status: Failed :thumbs_down:\n"
u"Details: [build log](https://ci.example.com"
u"/go/tab/pipeline/history/pipelineName)\n"
u"Comment: my hola mundo changes")
self.send_and_test_stream_message(
'pipeline_failed',
self.TOPIC,
expected_message,
content_type="application/x-www-form-urlencoded"
)
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("gocd", fixture_name, file_type="json")

View File

@@ -0,0 +1,58 @@
# Webhooks for external integrations.
import json
import os
from typing import Any, Dict, Iterable, Optional, Text
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.validator import check_dict, check_string
from zerver.models import UserProfile
MESSAGE_TEMPLATE = (
u'Author: {}\n'
u'Build status: {} {}\n'
u'Details: [build log]({})\n'
u'Comment: {}'
)
@api_key_only_webhook_view('Gocd')
@has_request_variables
def api_gocd_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
) -> HttpResponse:
modifications = payload['build_cause']['material_revisions'][0]['modifications'][0]
result = payload['stages'][0]['result']
material = payload['build_cause']['material_revisions'][0]['material']
if result == "Passed":
emoji = ':thumbs_up:'
elif result == "Failed":
emoji = ':thumbs_down:'
build_details_file = os.path.join(os.path.dirname(__file__), 'fixtures/build_details.json')
with open(build_details_file, 'r') as f:
contents = json.load(f)
build_link = contents["build_details"]["_links"]["pipeline"]["href"]
body = MESSAGE_TEMPLATE.format(
modifications['user_name'],
result,
emoji,
build_link,
modifications['comment']
)
branch = material['description'].split(",")
topic = branch[0].split(" ")[1]
check_send_webhook_message(request, user_profile, topic, body)
return json_success()