Skip to content

Instantly share code, notes, and snippets.

@samitha9125
Last active October 7, 2023 07:23
Show Gist options
  • Save samitha9125/68c5f6a14dcd13306d89e3d6d3f47111 to your computer and use it in GitHub Desktop.
Save samitha9125/68c5f6a14dcd13306d89e3d6d3f47111 to your computer and use it in GitHub Desktop.
GitHub Action JS Script for Slack Reminders
import { IncomingWebhook } from '@slack/webhook';
import github from '@actions/github';
import messages from './messages.js';
import { NIGHTLY_SLACK } from './slackConfig.js';
import Version from '../version/index.js';
/**
* This function serves as a reminder mechanism for nightly builds.
* It checks the state of the GitHub workflow and sends a reminder message to a Slack channel
* 2 hours prior to the build, urging the review and merge of any pending Pull Requests (PRs).
*
* Requirement:
* 1. **Nightly Build Reminder**: To ensure that the latest features are included in the nightly build,
* and any pending PRs are reviewed and merged, this reminder is crucial.
*
* Environment Variables:
* All these environment variables are passed to this script from the github workflow.
*
* Note:
* - This script is meant to be executed as a GitHub action based on a cron schedule and actions/github-script.
* - The reminder will not be sent if the nightly build workflow is disabled.
* - A custom function `Version` (not included in this gist) is used to determine the next version of the nightly build.
* - As an additional feature, if the version is not an alpha, the nightly build workflow will be automatically disabled.
* - Custom messages are not included in this gist.
*/
export default async function Reminder() {
const octokit = github.getOctokit(process.env.TOKEN); // GitHub Octokit client
const [owner, repo] = process.env.REPO.split('/'); // Repository owner and name
const workflow = await octokit.rest.actions.getWorkflow({ // Use to find the details of the workflow
owner,
repo,
workflow_id: process.env.WORKFLOW_ID // WORKFLOW_ID is the workflow file name in the default branch.
});
// Proceed only if the Nightly workflow is active
if (workflow.data.state === 'active') {
let message = '';
const slack = new IncomingWebhook(process.env.SLACK, NIGHTLY_SLACK); // Slack webhook client
const nextVersion = await Version(); // Obtain the next version
// Determine the message and action based on the version
if (nextVersion.includes('alpha')) {
message = messages.slack.alpha.replace('XXX', nextVersion); // Alpha version message
} else {
message = messages.slack.other; // Standard message
await octokit.rest.actions.disableWorkflow({
owner,
repo,
workflow_id: process.env.WORKFLOW_ID
}); // Disable the workflow for non-alpha versions
}
await slack.send({ text: message }); // Send the message to Slack
}
}
const NIGHTLY_SLACK = {
username: 'Nightly Build Reminder', // Name of the Slack App
icon_url: 'https link to the icon.' // Link to the Slack App Icon
};
export { NIGHTLY_SLACK };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment