Skip to content

Commit 5f25737

Browse files
committed
fix: better merging of blocks/attachments
1 parent 7b1db73 commit 5f25737

File tree

2 files changed

+116
-84
lines changed

2 files changed

+116
-84
lines changed

dist/index.js

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,45 +4055,45 @@ module.exports = require("util");
40554055
/***/ 676:
40564056
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
40574057

4058-
const { WebClient } = __webpack_require__(114)
4059-
const core = __webpack_require__(470)
4058+
const { WebClient } = __webpack_require__(114);
4059+
const core = __webpack_require__(470);
40604060

40614061
// Replace this tag in raw JSONs with current time value (ready for slack `ts` values)
4062-
const NOW_TS_TAG = '%NOW-TS%'
4062+
const NOW_TS_TAG = "%NOW-TS%";
40634063

40644064
async function run() {
40654065
try {
4066-
const token = core.getInput('token', { required: true })
4067-
const channel = core.getInput('channel', { required: true })
4068-
const text = core.getInput('text', { required: false })
4069-
const blocksRaw = core.getInput('blocks', { required: false })
4070-
const attachmentsRaw = core.getInput('attachments', { required: false })
4071-
const ts = core.getInput('ts', { required: false })
4072-
const appendText = core.getInput('appendAttachments', { required: false })
4073-
const appendBlocksRaw = core.getInput('appendAttachments', {
4066+
const token = core.getInput("token", { required: true });
4067+
const channel = core.getInput("channel", { required: true });
4068+
const text = core.getInput("text", { required: false });
4069+
const blocksRaw = core.getInput("blocks", { required: false });
4070+
const attachmentsRaw = core.getInput("attachments", { required: false });
4071+
const ts = core.getInput("ts", { required: false });
4072+
const appendText = core.getInput("appendAttachments", { required: false });
4073+
const appendBlocksRaw = core.getInput("appendAttachments", {
40744074
required: false,
4075-
})
4076-
const appendAttachmentsRaw = core.getInput('appendAttachments', {
4075+
});
4076+
const appendAttachmentsRaw = core.getInput("appendAttachments", {
40774077
required: false,
4078-
})
4078+
});
40794079

40804080
if (!token) {
4081-
throw new Error('Missing input `token`')
4081+
throw new Error("Missing input `token`");
40824082
}
40834083

4084-
const client = new WebClient(token)
4084+
const client = new WebClient(token);
40854085
const blocks = blocksRaw
40864086
? JSON.parse(blocksRaw.replace(NOW_TS_TAG, toSlackTS()))
4087-
: undefined
4087+
: undefined;
40884088
const attachments = attachmentsRaw
40894089
? JSON.parse(attachmentsRaw.replace(NOW_TS_TAG, toSlackTS()))
4090-
: undefined
4090+
: undefined;
40914091
const appendAttachments = appendAttachmentsRaw
40924092
? JSON.parse(appendAttachmentsRaw.replace(NOW_TS_TAG, toSlackTS()))
4093-
: undefined
4093+
: undefined;
40944094
const appendBlocks = appendBlocksRaw
40954095
? JSON.parse(appendBlocksRaw.replace(NOW_TS_TAG, toSlackTS()))
4096-
: undefined
4096+
: undefined;
40974097

40984098
if (!ts) {
40994099
// just post a new message
@@ -4102,16 +4102,16 @@ async function run() {
41024102
text,
41034103
blocks,
41044104
attachments,
4105-
}
4106-
const result = await client.chat.postMessage(message)
4105+
};
4106+
const result = await client.chat.postMessage(message);
41074107
if (result.error) {
41084108
throw new Error(
41094109
`Error happendAttachments while posting slack message: ${result.error}`
4110-
)
4110+
);
41114111
}
4112-
const { ts } = result
4113-
core.setOutput('ts', ts)
4114-
return
4112+
const { ts } = result;
4113+
core.setOutput("ts", ts);
4114+
return;
41154115
}
41164116

41174117
// retrive the original message
@@ -4121,44 +4121,60 @@ async function run() {
41214121
latest: ts,
41224122
inclusive: true,
41234123
limit: 1,
4124-
})
4124+
});
41254125

4126-
const [message] = response.messages
4126+
const [message] = response.messages;
41274127

41284128
const result = await client.chat.update({
41294129
// required refs
41304130
channel,
41314131
ts,
41324132
// new attributes
41334133
text: appendText
4134-
? [message.text, appendText].filter(Boolean).join('')
4134+
? [message.text, appendText].filter(Boolean).join("")
41354135
: text || message.text,
4136-
blocks: appendBlocks
4137-
? [...(message.blocks || []), ...appendBlocks].filter(Boolean)
4138-
: blocks || message.blocks,
4139-
attachments: appendAttachments
4140-
? [...(message.attachments || []), ...appendAttachments].filter(Boolean)
4141-
: attachments || message.attachments,
4142-
})
4136+
blocks: mergeItems(message.blocks, blocks, appendBlocks),
4137+
attachments: mergeItems(
4138+
message.attachments,
4139+
attachments,
4140+
appendAttachments
4141+
),
4142+
});
41434143

41444144
if (result.error) {
41454145
throw new Error(
41464146
`Error happend while updating slack message: ${result.error}`
4147-
)
4147+
);
41484148
}
4149-
core.setOutput('ts', ts)
4149+
core.setOutput("ts", ts);
41504150
} catch (error) {
4151-
core.debug(error.message)
4151+
core.debug(error.message);
41524152
core.setFailed(
41534153
`Something went wrong while sending a message to slack: ${error.message}`
4154-
)
4154+
);
41554155
}
41564156
}
41574157

4158-
run()
4158+
run();
41594159

41604160
function toSlackTS(timestamp = Date.now()) {
4161-
return `${Math.floor(timestamp / 1000)}`
4161+
return `${Math.floor(timestamp / 1000)}`;
4162+
}
4163+
4164+
function mergeItems(originalItems, newItems, appenedItems) {
4165+
if (!originalItems) {
4166+
return newItems || appenedItems;
4167+
}
4168+
4169+
if (!appenedItems) {
4170+
return newItems;
4171+
}
4172+
4173+
const items = [...originalItems, ...appenedItems];
4174+
if (!items.lenght) {
4175+
return undefined;
4176+
}
4177+
return items;
41624178
}
41634179

41644180

src/index.js

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
const { WebClient } = require('@slack/web-api')
2-
const core = require('@actions/core')
1+
const { WebClient } = require("@slack/web-api");
2+
const core = require("@actions/core");
33

44
// Replace this tag in raw JSONs with current time value (ready for slack `ts` values)
5-
const NOW_TS_TAG = '%NOW-TS%'
5+
const NOW_TS_TAG = "%NOW-TS%";
66

77
async function run() {
88
try {
9-
const token = core.getInput('token', { required: true })
10-
const channel = core.getInput('channel', { required: true })
11-
const text = core.getInput('text', { required: false })
12-
const blocksRaw = core.getInput('blocks', { required: false })
13-
const attachmentsRaw = core.getInput('attachments', { required: false })
14-
const ts = core.getInput('ts', { required: false })
15-
const appendText = core.getInput('appendAttachments', { required: false })
16-
const appendBlocksRaw = core.getInput('appendAttachments', {
9+
const token = core.getInput("token", { required: true });
10+
const channel = core.getInput("channel", { required: true });
11+
const text = core.getInput("text", { required: false });
12+
const blocksRaw = core.getInput("blocks", { required: false });
13+
const attachmentsRaw = core.getInput("attachments", { required: false });
14+
const ts = core.getInput("ts", { required: false });
15+
const appendText = core.getInput("appendAttachments", { required: false });
16+
const appendBlocksRaw = core.getInput("appendAttachments", {
1717
required: false,
18-
})
19-
const appendAttachmentsRaw = core.getInput('appendAttachments', {
18+
});
19+
const appendAttachmentsRaw = core.getInput("appendAttachments", {
2020
required: false,
21-
})
21+
});
2222

2323
if (!token) {
24-
throw new Error('Missing input `token`')
24+
throw new Error("Missing input `token`");
2525
}
2626

27-
const client = new WebClient(token)
27+
const client = new WebClient(token);
2828
const blocks = blocksRaw
2929
? JSON.parse(blocksRaw.replace(NOW_TS_TAG, toSlackTS()))
30-
: undefined
30+
: undefined;
3131
const attachments = attachmentsRaw
3232
? JSON.parse(attachmentsRaw.replace(NOW_TS_TAG, toSlackTS()))
33-
: undefined
33+
: undefined;
3434
const appendAttachments = appendAttachmentsRaw
3535
? JSON.parse(appendAttachmentsRaw.replace(NOW_TS_TAG, toSlackTS()))
36-
: undefined
36+
: undefined;
3737
const appendBlocks = appendBlocksRaw
3838
? JSON.parse(appendBlocksRaw.replace(NOW_TS_TAG, toSlackTS()))
39-
: undefined
39+
: undefined;
4040

4141
if (!ts) {
4242
// just post a new message
@@ -45,16 +45,16 @@ async function run() {
4545
text,
4646
blocks,
4747
attachments,
48-
}
49-
const result = await client.chat.postMessage(message)
48+
};
49+
const result = await client.chat.postMessage(message);
5050
if (result.error) {
5151
throw new Error(
5252
`Error happendAttachments while posting slack message: ${result.error}`
53-
)
53+
);
5454
}
55-
const { ts } = result
56-
core.setOutput('ts', ts)
57-
return
55+
const { ts } = result;
56+
core.setOutput("ts", ts);
57+
return;
5858
}
5959

6060
// retrive the original message
@@ -64,42 +64,58 @@ async function run() {
6464
latest: ts,
6565
inclusive: true,
6666
limit: 1,
67-
})
67+
});
6868

69-
const [message] = response.messages
69+
const [message] = response.messages;
7070

7171
const result = await client.chat.update({
7272
// required refs
7373
channel,
7474
ts,
7575
// new attributes
7676
text: appendText
77-
? [message.text, appendText].filter(Boolean).join('')
77+
? [message.text, appendText].filter(Boolean).join("")
7878
: text || message.text,
79-
blocks: appendBlocks
80-
? [...(message.blocks || []), ...appendBlocks].filter(Boolean)
81-
: blocks || message.blocks,
82-
attachments: appendAttachments
83-
? [...(message.attachments || []), ...appendAttachments].filter(Boolean)
84-
: attachments || message.attachments,
85-
})
79+
blocks: mergeItems(message.blocks, blocks, appendBlocks),
80+
attachments: mergeItems(
81+
message.attachments,
82+
attachments,
83+
appendAttachments
84+
),
85+
});
8686

8787
if (result.error) {
8888
throw new Error(
8989
`Error happend while updating slack message: ${result.error}`
90-
)
90+
);
9191
}
92-
core.setOutput('ts', ts)
92+
core.setOutput("ts", ts);
9393
} catch (error) {
94-
core.debug(error.message)
94+
core.debug(error.message);
9595
core.setFailed(
9696
`Something went wrong while sending a message to slack: ${error.message}`
97-
)
97+
);
9898
}
9999
}
100100

101-
run()
101+
run();
102102

103103
function toSlackTS(timestamp = Date.now()) {
104-
return `${Math.floor(timestamp / 1000)}`
104+
return `${Math.floor(timestamp / 1000)}`;
105+
}
106+
107+
function mergeItems(originalItems, newItems, appenedItems) {
108+
if (!originalItems) {
109+
return newItems || appenedItems;
110+
}
111+
112+
if (!appenedItems) {
113+
return newItems;
114+
}
115+
116+
const items = [...originalItems, ...appenedItems];
117+
if (!items.lenght) {
118+
return undefined;
119+
}
120+
return items;
105121
}

0 commit comments

Comments
 (0)