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

Commit

Permalink
Add Ability to Send Logs To Separate Channels (#39)
Browse files Browse the repository at this point in the history
* Add Ability to Send Logs To Separate Channels

* Reset To Default Array
  • Loading branch information
therynamo authored Jun 10, 2019
1 parent 5575218 commit f8ec1bc
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 413 deletions.
4 changes: 4 additions & 0 deletions docs/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ github_tag_id: 'v([0-9]+-release)$'
```yaml
teams:
- name: Team 1
channels:
- my-first-channel
- my-second-channel
color: '#f06d06'
emoji: '🐶'
mentions: '<@sam.i.am>'
Expand All @@ -80,6 +83,7 @@ teams:
```

- `name` - this value will be used to identify the particular team name in the output
- `channels` - this list allows you to send your teams log to individual channels outside of the default channel for Captain's Log. These can be either channel names or channel IDs ([read more here](https://api.slack.com/methods/chat.postMessage)). Note, this feature is only available when using "slack tokens" for authentication **along side of** or **in place** of slack urls. If you're sending a message to a private room, be sure the Slack app (associated token) has access to send messages to that room, or Captain's Log will not be able to send a message to that channel.
- `color` - this will be the side strip color of the team's output
- `emoji` - this will be the emoji next to the team name
- `mentions` - this value is used to mention any people or groups about this section of the log. You will need to wrap all mentions in `<>` due to slack conventions. You can mention groups by using the following format: `<!subteam^1234ASDF|super-cool-team>` where **super-cool-team** is the group and `1234ASDF` is the unique group identifier, which you can find as follows:
Expand Down
2 changes: 2 additions & 0 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ my-release-log-step:
github_repo: react-media
teams:
- name: Team1
channels:
- my-teams-private-channel
color: "#FFDC18"
emoji: "✨"
mentions: "<@person1> <@person2>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@octokit/graphql": "^2.0.1",
"@octokit/rest": "^15.8.1",
"@slack/client": "^4.3.1",
"@slack/web-api": "^5.0.1",
"common-tags": "^1.8.0",
"idx": "^2.3.0",
"jest": "^23.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/SlackConnector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { WebClient } = require('@slack/client');
const { WebClient } = require('@slack/web-api');
const config = require('../config');

// An access token (from your Slack app or custom integration - xoxp, xoxb, or xoxa)
Expand Down
11 changes: 7 additions & 4 deletions src/facades/ReleaseCommunicationFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,18 @@ class ReleaseCommunication {
* async sendMessage - Send slack message to desired channel
*
* @param {String} text text to send to channel
* @param {String} channelOverride desired channel if it is not what exists in the constructor
* @param {Array} attachments list of attachments to send to a room
* @param {String} channel desired channel if it is not what exists in the constructor
* @param {Boolean} sendToChannelOnly if you wish to ignore the channelURL (which usually needs a channel) set to true
*
* @return {Object} response object from the posted message https://slackapi.github.io/node-slack-sdk/web_api
*/
async sendMessage(text, attachments) {
async sendMessage(text, attachments, channel, sendToChannelOnly) {
const response = await postMessageHandler({
channel: this.channel,
channel: channel || this.channel,
text,
attachments,
channelUrl: this.channelUrl,
channelUrl: sendToChannelOnly ? null : this.channelUrl,
});

return response;
Expand Down
1 change: 1 addition & 0 deletions src/factories/Team/__tests__/Team.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Team', () => {
const team = Team();

expect(team).toEqual(expect.objectContaining({
channels: [],
color: expect.any(String),
emoji: expect.any(String),
mentions: expect.any(String),
Expand Down
6 changes: 4 additions & 2 deletions src/factories/Team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { truncate } = require('../../utils');
const Team = function Team(team = {}) {
const {
color = '#3ef2c5',
channels = [],
emoji = '🌱',
issueTracking = {},
mentions = '',
Expand Down Expand Up @@ -66,11 +67,12 @@ const Team = function Team(team = {}) {

return {
addMessage,
name,
channels,
color,
emoji,
messageMatch,
mentions,
messageMatch,
name,
teamMessages: this.teamMessages,
teamTitles: this.teamTitles,
...trackers,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/postMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const postOptions = (attachments, text, channel) => ({
});

module.exports = async function postMessage({
attachments, channel, text, channelUrl,
attachments, channel = null, text, channelUrl,
}) {
let response = {};

Expand Down
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const teamList = teams.length ? teams.map(team => Team(team)) : [];

const createAttachment = (hasMessages, { owner, repo }) => {
let message = EMPTY_MESSAGE(owner, repo);
let attachments = {};
let attachments = [];
let subChannelAttachments = [];

if (!hasMessages) {
return { message, attachments };
Expand All @@ -22,6 +23,8 @@ const createAttachment = (hasMessages, { owner, repo }) => {
// add all the PRs if there are any
message = DEFAULT_HEADING(owner, repo);
attachments = [];
// team sub-channel attachments
subChannelAttachments = [];

const teamsToAttach = [...teamList, defaultTeam];

Expand All @@ -31,10 +34,14 @@ const createAttachment = (hasMessages, { owner, repo }) => {

if (attachment) {
attachments.push(attachment);

// if a team has subchannels, generate attatchments to send
// to those channels here.
team.channels.forEach(channel => subChannelAttachments.push({ channel, attachment }));
}
});

return { message, attachments };
return { message, attachments, subChannelAttachments };
};

module.exports = async function App(config) {
Expand Down Expand Up @@ -74,9 +81,15 @@ module.exports = async function App(config) {

populateMessages(defaultTeam)(teamList, sortedMessages);

const { message, attachments } = createAttachment(messages.length, { owner, repo });
const { message, attachments, subChannelAttachments } = createAttachment(messages.length, { owner, repo });

logger.info(`\n Slack Formatter Url. CMD+Click to open in your default browser \n \n ${generateSlackFormatterUrl(attachments)}`);

await releaseCommunication.sendMessage(message, attachments);

// Send all individual attachments to their respective channels per team.
if (subChannelAttachments.length) {
await Promise.all(subChannelAttachments.map(({ attachment, channel: subChannel }) =>
releaseCommunication.sendMessage(message, [attachment], subChannel, true)));
}
};
Loading

0 comments on commit f8ec1bc

Please sign in to comment.