Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Fix username is not linked on Mattermost
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Feb 26, 2018
1 parent 09c9e4b commit 6929491
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Run a container.
```bash
docker run --rm -p 3000:3000 \
-e SLACK_WEBHOOK=https://hooks.slack.com/... \
-e SLACK_USERNAME=JIRA \
-e SLACK_ICON_URL=https://lh3.googleusercontent.com/GkgChJMixx9JAmoUi1majtfpjg1Ra86gZR0GCehJfVcOGQI7Ict_TVafXCtJniVn3R0 \
int128/jira-to-slack
```

Expand All @@ -39,7 +37,9 @@ slack:
## Slack icon emoji
# iconEmoji: ":speech_baloon:"
## Slack icon image URL
#iconImageURL: https://lh3.googleusercontent.com/GkgChJMixx9JAmoUi1majtfpjg1Ra86gZR0GCehJfVcOGQI7Ict_TVafXCtJniVn3R0
# iconImageURL: https://lh3.googleusercontent.com/GkgChJMixx9JAmoUi1majtfpjg1Ra86gZR0GCehJfVcOGQI7Ict_TVafXCtJniVn3R0
## Slack API dialect
# dialect: slack
```

```bash
Expand All @@ -58,12 +58,13 @@ If both the JIRA server and jira-to-slack are running in the same namespace, poi
You can set the BOT username and icon in the Slack webhook settings.
Instead you can set the following environment variables to the container:

Name | Value | Example
-----|-------|--------
Name | Value | Example value
-----|-------|--------------
`SLACK_WEBHOOK` | Slack webhook URL (Required) | `https://hooks.slack.com/...`
`SLACK_USERNAME` | Username of the BOT (Optional) | `JIRA`
`SLACK_ICON_EMOJI` | Icon emoji of the BOT (Optional) | `:speech_baloon:`
`SLACK_ICON_URL` | Icon URL of the BOT (Optional) | `http://.../jira.png`
`SLACK_API_DIALECT` | Slack API dialect, defaults to `slack` | `slack` or `mattermost`

### JIRA

Expand Down
2 changes: 2 additions & 0 deletions charts/jira-to-slack/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
value: {{ .Values.slack.iconEmoji }}
- name: SLACK_ICON_URL
value: {{ .Values.slack.iconImageURL }}
- name: SLACK_API_DIALECT
value: {{ .Values.slack.dialect }}
ports:
- containerPort: {{ .Values.service.internalPort }}
livenessProbe:
Expand Down
2 changes: 2 additions & 0 deletions charts/jira-to-slack/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ slack: {}
# iconEmoji: ":speech_baloon:"
## Slack icon image URL
# iconImageURL: https://lh3.googleusercontent.com/GkgChJMixx9JAmoUi1majtfpjg1Ra86gZR0GCehJfVcOGQI7Ict_TVafXCtJniVn3R0
## Slack API dialect
# dialect: slack

replicaCount: 1
image:
Expand Down
9 changes: 6 additions & 3 deletions src/webhook_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ const slack = new Slack(
iconUrl: process.env.SLACK_ICON_URL,
});

const dialect = process.env.SLACK_API_DIALECT;

module.exports = async req => {
if (typeof req !== 'object') {
throw new TypeError(`Request must be a valid object: ${req}`);
}
const message = new WebhookMessage(req.body);
const message = new WebhookMessage(req.body, { dialect });
if (message.isValid()) {
return await slack.send({
// Use text instead of pretext in attachment,
// due to the issue that @username in pretext is not linked on Mattermost.
text: message.getPretext(),
attachments: [
{
title: message.getTitle(),
title_link: message.getTitleLink(),
pretext: message.getPretext(),
text: message.getText(),
footer: message.getFooter(),
ts: message.getUpdatedTimestamp(),
},
],
Expand Down
49 changes: 36 additions & 13 deletions src/webhook_message.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const JIRA_FIELDS_TO_NOTIFY_UPDATE = ['summary', 'description', 'assignee'];

module.exports = class WebhookMessage {
constructor(body) {
/**
* Constructor.
* @param {object} body JSON payload
*/
constructor(body, { dialect }) {
if (typeof body !== 'object') {
throw new TypeError(`Request body must be a valid object: ${body}`);
}
this._body = body;
this._dialect = dialect;
}

/**
Expand Down Expand Up @@ -50,19 +55,29 @@ module.exports = class WebhookMessage {
* @returns {string} pretext of the message (who)
*/
getPretext() {
const { webhookEvent, user, comment } = this._body;
const { webhookEvent, user, comment, issue } = this._body;
const username = this._formatUsername(user);
let verb;
switch (webhookEvent) {
case 'jira:issue_updated':
if (comment) {
return `<@${user.name}> commented:`;
verb = 'commented to';
} else {
return `<@${user.name}> updated:`;
verb = 'updated';
}
break;
case 'jira:issue_created':
return `<@${user.name}> created:`;
verb = 'created';
break;
case 'jira:issue_deleted':
return `<@${user.name}> deleted:`;
verb = 'deleted';
break;
}
let assignee = '';
if (issue.fields.assignee) {
assignee = `(assigned to ${this._formatUsername(issue.fields.assignee)}>)`;
}
return `${username} ${verb} the issue: ${assignee}`
}

/**
Expand All @@ -84,15 +99,23 @@ module.exports = class WebhookMessage {
}
}

getFooter() {
const { issue } = this._body;
if (issue.fields.assignee) {
return `Assigned to <@${issue.fields.assignee.name}>`;
getUpdatedTimestamp() {
const { timestamp } = this._body;
if (typeof timestamp === 'number') {
return timestamp / 1000;
}
}

getUpdatedTimestamp() {
const { timestamp } = this._body;
return parseInt(timestamp) / 1000;
/**
* Format the username.
* @param {object} user user object that has user.name
*/
_formatUsername(user) {
switch (this._dialect) {
case 'mattermost':
return `@${user.name}`;
default:
return `<@${user.name}>`;
}
}
}

0 comments on commit 6929491

Please sign in to comment.