Skip to content

Commit ff49fe9

Browse files
author
Fabian Sobanski
committed
Set webhook_url as url parameter instead of in config.
This pull request is based on PR NotSqrt#4. Instead of setting MATTERMOST_WEBHOOK_URL when starting the server, the url must now be passed as an url parameter in the form http://<this-project-server:port>/new_event?mattermost_webhook_url=<mattermost channel webhook url> I updated the tests and the readme, just as @NotSqrt requested in PR NotSqrt#4. This commit should fix issue NotSqrt#2.
1 parent 4a61a48 commit ff49fe9

5 files changed

Lines changed: 32 additions & 32 deletions

File tree

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: mattermost_gitlab --port $PORT $MATTERMOST_WEBHOOK_URL
1+
web: mattermost_gitlab --port $PORT

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Here's how to start:
5757
- `[sudo] pip install git+https://github.com/NotSqrt/mattermost-integration-gitlab`
5858
7. Run the server:
5959
- `mattermost_gitlab --help`
60-
- `mattermost_gitlab $MATTERMOST_WEBHOOK_URL`
60+
- `mattermost_gitlab`
6161
You will see the output similar to `Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)`. This is default IP:PORT pair
6262
the integration service will listen on. We will refer to this address as the `http://<your-mattermost-integration-URL>`). You may change the IP:PORT with the adequate command-line options (see --help)
6363
8. You may want to add a script to auto-start mattermost_gitlab at boot:
@@ -130,12 +130,12 @@ There are several environment variables that can be used to change behaviour of
130130

131131
```bash
132132
docker build -t mattermost-integration-gitlab .
133-
docker run -d -E MATTERMOST_WEBHOOK_URL=http://mattermost/hooks/hook-id mattermost-integration-gitlab
133+
docker run -d mattermost-integration-gitlab
134134
```
135135

136136

137137
3. **Connect your project to your GitLab account for outgoing webhooks**
138-
1. Log in to your GitLab account and open the project from which you want to receive updates and to which you have administrator access. From the settings menu of the project, click on **Webhooks**. In the **URL** field enter `http://<your-mattermost-integration-URL>/new_event` (notice extra `new_event` URL argument). On this address the integration service will be receiving the events from your GitLab project. Make sure your URL has a leading `http://` or `https://`.
138+
1. Log in to your GitLab account and open the project from which you want to receive updates and to which you have administrator access. From the settings menu of the project, click on **Webhooks**. In the **URL** field enter `http://<your-mattermost-integration-URL>/new_event?mattermost_webhook_url=<your-mattermost-webhook-URL>` (notice extra `new_event` URL argument). On this address the integration service will be receiving the events from your GitLab project. `/<your-mattermost-webhook-URL>` is the webhook that you configured previously in your mattermost instance. Make sure your URL has a leading `http://` or `https://`.
139139
2. On the same page, under **Trigger** select the events you want to receive, **Push events**, **Comment events**, **Issue events**, **Merge Request events** for instance.
140140
3. (Recommended but optional): Encrypt your connection from GitLab to your project by selecting **Enable SSL verification**. If this option is not available and you're not familiar with how to set it up, contact your GitLab System Administrator for help.
141141
4. Click **Add Webhook** and check that your new webhook entry is added to the **Webhooks** section below the button.
@@ -168,21 +168,20 @@ Here's how to start:
168168
5. At the top of your app dashboard, go to the **Settings** tab and scroll down to the **Domains** section. Copy the URL below **Heroku Domain** (we'll refer to this as `http://<your-heroku-domain>/` and we'll need it in the next step)
169169
6. Leave your Heroku interface open as we'll come back to it to finish the setup.
170170

171-
3. **Connect your project to your GitLab account for outgoing webhooks**
172-
1. Log in to GitLab account and open the project from which you want to receive updates and to which you have administrator access. From the left side of the project screen, click on **Settings** > **Web Hooks**. In the **URL** field enter `http://<your-heroku-domain>/` from the previous step, plus the word `new_event` to create an entry that reads **`http://<your-heroku-domain>/new_event`** so events from your GitLab project are sent to your Heroku server. Make sure your URL has a leading `http://` or `https://`.
173-
2. On the same page, under **Trigger** select **Push events**, **Comment events**, **Issue events**, **Merge Request events**
174-
3. (Recommended but optional): Encrypt your connection from GitLab to your project by selecting **Enable SSL verification**. If this option is not available and you're not familiar with how to set it up, contact your GitLab System Administrator for help.
175-
3. Click **Add Web Hook** and check that your new webhook entry is added to the **Web hooks** section below the button.
176-
4. Leave this page open as we'll come back to it to test that everything is working.
177-
178-
4. **Set up your Mattermost instance to receive incoming webhooks**
171+
3. **Set up your Mattermost instance to receive incoming webhooks**
179172
1. Log in to your Mattermost account. Click the three dot menu at the top of the left-hand side and go to **Integrations** > **Incoming Webhooks**.
180173
2. Click **Add incoming webhook**.
181174
3. Specify the name of the webhook and select the channel in which you want GitLab notifications to appear.
182175
4. Copy the contents next to **URL** of the new webhook you just created (we'll refer to this as `https://<your-mattermost-webhook-URL>` and add it to your Heroku server).
183176
5. Go back to your Heroku app dashboard under the **Settings** tab. Under the **Config Variables** section, click **Reveal Config Vars**
184-
1. Type `MATTERMOST_WEBHOOK_URL` in the **KEY** field and paste `https://<your-mattermost-webhook-URL>` into the **VALUE** field, then click **Add**.
185-
2. In the second **KEY** field, type `PUSH_TRIGGER` and in the corresponding **VALUE** field, type `True`.
177+
1. In the **KEY** field, type `PUSH_TRIGGER` and in the corresponding **VALUE** field, type `True`.
178+
179+
4. **Connect your project to your GitLab account for outgoing webhooks**
180+
1. Log in to GitLab account and open the project from which you want to receive updates and to which you have administrator access. From the left side of the project screen, click on **Settings** > **Web Hooks**. In the **URL** field enter `http://<your-heroku-domain>` from the previous step, plus the word `new_event` plus the `mattermost_webhook_url` parameter to create an entry that reads **`http://<your-heroku-domain>/new_event?mattermost_webhook_url=<your-mattermost-webhook-URL>`** so events from your GitLab project are sent to your Heroku server. Make sure your URL has a leading `http://` or `https://`.
181+
2. On the same page, under **Trigger** select **Push events**, **Comment events**, **Issue events**, **Merge Request events**
182+
3. (Recommended but optional): Encrypt your connection from GitLab to your project by selecting **Enable SSL verification**. If this option is not available and you're not familiar with how to set it up, contact your GitLab System Administrator for help.
183+
3. Click **Add Web Hook** and check that your new webhook entry is added to the **Web hooks** section below the button.
184+
4. Leave this page open as we'll come back to it to test that everything is working.
186185

187186
5. **Test your webhook integration**
188187
1. If your GitLab project is in active development, return to the **Settings** > **Web Hooks** page of your GitLab project and click **Test Hook** to send a test message about one of your recent updates from your GitLab project to Mattermost. You should see a notification on the Gitlab page that the hook was successfully executed. In Mattermost, go to the channel which you specified when creating the URL for your incoming webhook and make sure that the message delivered successfully.

entrypoint.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,11 @@ for POSSIBLE_EVENT in "${NEGATIVE_EVENTS[@]}"; do
3838
fi
3939
done
4040

41-
if [ -z "${MATTERMOST_WEBHOOK_URL}" ]; then
42-
echo "Missing Mattermost WEBHOOK url !" >&2
43-
exit 1
44-
fi
45-
46-
4741
if [ ! -x "/usr/local/bin/mattermost_gitlab" ]; then
4842
echo "Missing application executable !" >&2
4943
exit 1
5044
fi
5145

5246
echo "Starting: "
53-
echo "/usr/local/bin/mattermost_gitlab ${PLUGIN_ARGS} '${MATTERMOST_WEBHOOK_URL}'"
54-
/usr/local/bin/mattermost_gitlab ${PLUGIN_ARGS} "${MATTERMOST_WEBHOOK_URL}"
47+
echo "/usr/local/bin/mattermost_gitlab ${PLUGIN_ARGS}"
48+
/usr/local/bin/mattermost_gitlab ${PLUGIN_ARGS}

mattermost_gitlab/server.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ def new_event():
3737
if request.json is None:
3838
print('Invalid Content-Type')
3939
return 'Content-Type must be application/json and the request body must contain valid JSON', 400
40+
elif 'mattermost_webhook_url' not in request.args:
41+
print('Mattermost Webhook URL not set')
42+
return 'Mattermost Webhook URL must be set', 400
4043

4144
try:
4245
event = event_formatter.as_event(request.json)
46+
mattermost_webhook_url = request.args['mattermost_webhook_url']
4347

4448
if event.should_report_event(app.config['REPORT_EVENTS']):
4549
text = event.format()
46-
post_text(text)
50+
post_text(text, mattermost_webhook_url)
4751
except Exception:
4852
import traceback
4953
traceback.print_exc()
@@ -60,21 +64,25 @@ def new_ci_event():
6064
if request.json is None:
6165
print('Invalid Content-Type')
6266
return 'Content-Type must be application/json and the request body must contain valid JSON', 400
67+
elif 'mattermost_webhook_url' not in request.args:
68+
print('Mattermost Webhook URL not set')
69+
return 'Mattermost Webhook URL must be set', 400
6370

6471
try:
6572
event = event_formatter.CIEvent(request.json)
73+
mattermost_webhook_url = request.args['mattermost_webhook_url']
6674

6775
if event.should_report_event(app.config['REPORT_EVENTS']):
6876
text = event.format()
69-
post_text(text)
77+
post_text(text, mattermost_webhook_url)
7078
except Exception:
7179
import traceback
7280
traceback.print_exc()
7381

7482
return 'OK'
7583

7684

77-
def post_text(text):
85+
def post_text(text, mattermost_webhook_url):
7886
"""
7987
Mattermost POST method, posts text to the Mattermost incoming webhook URL
8088
"""
@@ -89,15 +97,14 @@ def post_text(text):
8997
data['channel'] = app.config['CHANNEL']
9098

9199
headers = {'Content-Type': 'application/json'}
92-
resp = requests.post(app.config['MATTERMOST_WEBHOOK_URL'], headers=headers, data=json.dumps(data), verify=app.config['VERIFY_SSL'])
100+
resp = requests.post(mattermost_webhook_url, headers=headers, data=json.dumps(data), verify=app.config['VERIFY_SSL'])
93101

94102
if resp.status_code is not requests.codes.ok:
95-
print('Encountered error posting to Mattermost URL %s, status=%d, response_body=%s' % (app.config['MATTERMOST_WEBHOOK_URL'], resp.status_code, resp.json()))
103+
print('Encountered error posting to Mattermost URL %s, status=%d, response_body=%s' % (mattermost_webhook_url, resp.status_code, resp.json()))
96104

97105

98106
def parse_args(args=None):
99107
parser = argparse.ArgumentParser()
100-
parser.add_argument('MATTERMOST_WEBHOOK_URL', help='The Mattermost webhook URL you created')
101108

102109
server_options = parser.add_argument_group("Server")
103110
server_options.add_argument('-p', '--port', type=int, default=5000)

tests/test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ def test_get(self):
4646
class ServerTestMixin(MockHttpServerMixin, FlaskMixin):
4747

4848
port = get_available_port()
49-
url = '/new_event'
49+
url = "/new_event?mattermost_webhook_url=http://127.0.0.1:{}".format(port)
5050

5151
def setUp(self):
5252
super(ServerTestMixin, self).setUp()
5353

54-
mattermost_webhook_url = "http://127.0.0.1:{}".format(self.port)
55-
_, _, options = server.parse_args([mattermost_webhook_url, "--tag", "--push"])
54+
_, _, options = server.parse_args(["--tag", "--push"])
5655
server.app.config.update(options)
5756

5857
def post(self, name):
@@ -153,7 +152,8 @@ def test_tag(self):
153152

154153
class BuildTest(ServerTestMixin):
155154

156-
url = "/new_ci_event"
155+
port = get_available_port()
156+
url = "/new_ci_event?mattermost_webhook_url=http://127.0.0.1:{}".format(port)
157157

158158
def test_create_build(self):
159159
self.assertResponse("gitlab/build/create_build_1")

0 commit comments

Comments
 (0)