GitHub → Slack: Show service channel
How does this help you?
- Quickly find the Slack channel associated with this repository
- Jump straight into Slack via a deep link
High level approach
We assume a local data file maps repositories to Slack channels. The integration reads the mapping, finds the channel for the current repo (tag key repository, value from the GitHub page), and renders a link to Slack.
Example data file (slack-channels.data.json):
{
"mappings": {
"company/webshop": "#webshop",
"company/notifier": "#alerts-notifier"
}
}
tip
The prefered way of obtaining this information is if you use a Service/Inventory Catalog solution, which you can read this information from via API access.
Prerequisites
No external auth required. Add a data file (e.g., slack-channels.data.json) to your data directory. Learn more in Data files.
Code
github.slack.channel.integration.example.js
module.exports = {
metadata: {
name: 'github-slack-channel',
description: 'Show the Slack channel mapped to this repository via data files',
match: {
contextType: 'github',
context: {
'page.repository': { startsWith: 'my-org/' }
}
},
requiredSecrets: [],
cache: 300
},
run: async function (context, secrets = {}, dataFiles = []) {
const repo = context.page.repository; // e.g., "company/webshop"
// Expect a data file like slack-channels.data.json with shape:
// { "mappings": { "company/webshop": "#webshop" } }
const df = dataFiles.find(f => f.id === 'slack-channels');
if (!df || !df.data || !df.data.mappings) {
return [];
}
let channel = df.data.mappings[repo];
if (!channel || typeof channel !== 'string') {
return [];
}
// Normalize channel value (accepts "#name", "name", or channel ID)
const normalized = channel.startsWith('#') ? channel.slice(1) : channel;
// Slack app redirect supports channel by name or ID
const slackUrl = `https://slack.com/app_redirect?channel=${encodeURIComponent(normalized)}`;
return [
{ type: 'link', content: `#${normalized}`, icon: 'slack', href: slackUrl },
];
}
};