1- const { getInput, setFailed } = require ( '@actions/core' )
1+ const { getInput, setFailed, info } = require ( '@actions/core' )
22const github = require ( '@actions/github' )
33const request = require ( 'request-promise' )
44
@@ -72,6 +72,14 @@ async function getFullName (githubUsername) {
7272 }
7373}
7474
75+ function getTeamIdFromWebhookUrl ( webhookUrl ) {
76+ // Webhook URLs look something like
77+ // https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
78+ // We need this: ^^^^^^^^^
79+ const teamId = webhookUrl . substring ( 33 , 42 )
80+ return teamId . startsWith ( 'T' ) ? teamId : '' // Simple sanity check
81+ }
82+
7583async function run ( ) {
7684 try {
7785 const { context : { eventName } } = github
@@ -88,6 +96,8 @@ async function run () {
8896 sha,
8997 workflow,
9098 actor : githubUsername ,
99+ runId,
100+ runNumber,
91101 payload : {
92102 action, // Activity Type from https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
93103 ref,
@@ -107,8 +117,6 @@ async function run () {
107117 const branch = ( eventName === 'push' )
108118 ? ref . slice ( 'refs/heads/' . length ) // ref = 'refs/heads/master'
109119 : github . context . payload . pull_request . head . ref // 'master'
110- const runNumber = process . env . GITHUB_RUN_NUMBER
111- const runId = process . env . GITHUB_RUN_ID
112120
113121 const message = ( eventName === 'push' )
114122 ? headCommitMessage . split ( '\n' ) [ 0 ]
@@ -177,6 +185,12 @@ async function run () {
177185 body,
178186 json : true
179187 } )
188+
189+ const channelQuery = `?channel=${ encodeURIComponent ( channel . replace ( / ^ # / , '' ) ) } ` // Can be a channel ID or name (without #)
190+ const teamId = getTeamIdFromWebhookUrl ( webhookUrl )
191+ const teamQuery = teamId ? `&team=${ encodeURIComponent ( teamId ) } ` : '' // Will try to use default Slack workspace if not provided
192+ const linkToChannel = `https://slack.com/app_redirect${ channelQuery } ${ teamQuery } `
193+ info ( `Slack notification posted to ${ channel } ! 🎉\n\nLink to channel: ${ linkToChannel } ` )
180194 } catch ( e ) {
181195 setFailed ( e . message || e )
182196 }
0 commit comments