@@ -2,8 +2,12 @@ name: "Slack Pull Request Activity"
22description : " A GitHub action to notify Slack about pull request activities."
33inputs :
44 pathToSlackStreamMapping :
5- description : " Mapping of file paths to Slack webhook URLs"
6- required : true
5+ description : " Mapping of file paths to Slack webhook URLs for dynamic routing"
6+ required : false
7+ type : string
8+ slackStream :
9+ description : " Direct Slack webhook URL to send notifications to"
10+ required : false
711 type : string
812runs :
913 using : " composite"
1418
1519 - name : Notify Slack
1620 uses : actions/github-script@v7
21+ env :
22+ SLACK_STREAM : ${{ inputs.slackStream }}
23+ PATH_TO_SLACK_STREAM_MAPPING : ${{ inputs.pathToSlackStreamMapping }}
1724 with :
1825 script : |
19- const pathToSlackStreamMapping = JSON.parse(`${{ inputs.pathToSlackStreamMapping }}`);
2026 const slackifyMarkdown = require('slackify-markdown');
2127 const pr = context.payload.pull_request;
2228 const action = context.payload.action;
@@ -40,27 +46,43 @@ runs:
4046 }
4147 };
4248 const eventData = prEvents[action] || { color: "#36A54F", message: action };
43- const slackStreams = new Set();
44- const streamPathMap = new Map();
4549
46- const files = await github.paginate(github.rest.pulls.listFiles, {
47- owner: context.repo.owner,
48- repo: context.repo.repo,
49- pull_number: pr.number,
50- });
51- console.log(`Found ${files.length} changed files.`);
52- for (const file of files) {
53- for (const [path, slackStream] of Object.entries(pathToSlackStreamMapping)) {
54- if (file.filename.startsWith(path)) {
55- console.log(`Add stream for: ${file.filename}`);
56- slackStreams.add(slackStream);
57- streamPathMap.set(slackStream, path);
50+ // Support both pathToSlackStreamMapping and direct slackStream
51+ let slackStreams = new Set();
52+ let streamPathMap = new Map();
53+
54+ const slackStream = process.env.SLACK_STREAM;
55+ if (slackStream) {
56+ slackStreams.add(slackStream);
57+ }
58+
59+ const pathToSlackStreamMapping = process.env.PATH_TO_SLACK_STREAM_MAPPING;
60+ if (pathToSlackStreamMapping) {
61+ try {
62+ const parsedMapping = JSON.parse(pathToSlackStreamMapping);
63+ const files = await github.paginate(github.rest.pulls.listFiles, {
64+ owner: context.repo.owner,
65+ repo: context.repo.repo,
66+ pull_number: pr.number,
67+ });
68+ console.log(`Found ${files.length} changed files.`);
69+ for (const file of files) {
70+ for (const [path, slackStream] of Object.entries(parsedMapping)) {
71+ if (file.filename.startsWith(path)) {
72+ console.log(`Add stream for: ${file.filename}`)
73+ slackStreams.add(slackStream);
74+ streamPathMap.set(slackStream, path);
75+ }
76+ }
5877 }
78+ } catch (error) {
79+ console.error('Failed to parse pathToSlackStreamMapping JSON:', error);
5980 }
6081 }
6182
6283 if (slackStreams.size === 0) {
63- console.log("No matching files found for Slack notification.");
84+ console.log("No Slack streams configured for notification.");
85+ return;
6486 } else {
6587 console.log(`Notifying ${slackStreams.size} Slack streams.`);
6688 }
@@ -97,13 +119,13 @@ runs:
97119 body: JSON.stringify(payload),
98120 });
99121
100- const matchedPath = streamPathMap.get(slackStream);
101-
102122 if (!response.ok) {
103123 console.error(`Slack notification failed: ${response.statusText}`);
104124 anyFailures = true;
105125 } else {
106- console.log(`Notification sent to Slack webhook: ${slackStream} (path matched: ${matchedPath})`);
126+ const matchedPath = streamPathMap.get(slackStream);
127+ const pathInfo = matchedPath ? ` (path matched: ${matchedPath})` : "";
128+ console.log(`Notification sent to Slack webhook: ${slackStream}${pathInfo}`);
107129 }
108130 } catch (error) {
109131 console.error(`Error sending notification to Slack webhook: ${slackStream}`, error);
0 commit comments