-
Notifications
You must be signed in to change notification settings - Fork 30
Added GitHub Actions Slack notification. #1369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martnpaneq
wants to merge
5
commits into
master
Choose a base branch
from
i/4450-gh-actions-slack-notification
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a8da16
Added GitHub Actions Slack notification.
martnpaneq fb7dfba
Added changelog entry.
martnpaneq 8d7a936
Honored `GITHUB_API_URL` in `formatMessage` so commit detail lookups …
martnpaneq 6e05617
Made `formatMessage` potentially GitHub-Enterprise-safe.
martnpaneq 2c64124
Normalized `apiUrl` and `serverUrl`.
martnpaneq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
.changelog/20260522154014_i_4450_gh_actions_slack_notification.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| type: Other | ||
| scope: | ||
| - ckeditor5-dev-ci | ||
| see: | ||
| - https://github.com/ckeditor/ckeditor5-internal/issues/4450 | ||
| --- | ||
|
|
||
| Added `ckeditor5-dev-ci-notify-github-actions-status`, a GitHub Actions equivalent of `ckeditor5-dev-ci-notify-circle-status` that posts a Slack notification summarizing a failed workflow. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/ckeditor5-dev-ci/bin/notify-github-actions-status.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md. | ||
| */ | ||
|
|
||
| import { parseArgs } from 'node:util'; | ||
| import slackNotify from 'slack-notify'; | ||
| import formatMessage from '../lib/format-message.js'; | ||
|
|
||
| // This script assumes that it is being executed in a GitHub Actions runner. | ||
| // The step using it should be conditional on the workflow having failed | ||
| // (for example via `if: failure()` or a dedicated job depending on the failed one), | ||
| // since the script itself does not check whether the workflow failed before sending | ||
| // the notification. | ||
| // | ||
| // Described environment variables starting with "CKE5" must be added by the integrator. | ||
|
|
||
| const { | ||
| /** | ||
| * Required. Token to a GitHub account with the `repo` scope. It is required for obtaining | ||
| * the author of the commit that triggered the failed workflow run. The repository can be | ||
| * private, so the public, unauthenticated API cannot be used. | ||
| */ | ||
| CKE5_GITHUB_TOKEN, | ||
|
|
||
| /** | ||
| * Required. Webhook URL of the Slack channel where the notification should be sent. | ||
| */ | ||
| CKE5_SLACK_WEBHOOK_URL, | ||
|
|
||
| // Variables that are available by default in the GitHub Actions environment. | ||
| // See: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables. | ||
| GITHUB_REPOSITORY, | ||
| GITHUB_REF_NAME, | ||
| GITHUB_SHA, | ||
| GITHUB_RUN_ID, | ||
| GITHUB_RUN_ATTEMPT, | ||
| GITHUB_WORKFLOW, | ||
| GITHUB_SERVER_URL, | ||
| GITHUB_API_URL | ||
| } = process.env; | ||
|
|
||
| const { values: cliArguments } = parseArgs( { | ||
| options: { | ||
| /** | ||
| * Optional. If both are defined, the script will use the URL as the commit URL. | ||
| * Otherwise, the URL will be constructed using the current repository data. | ||
| */ | ||
| 'trigger-repository-slug': { | ||
| type: 'string', | ||
| default: process.env.CKE5_TRIGGER_REPOSITORY_SLUG | ||
| }, | ||
| 'trigger-commit-hash': { | ||
| type: 'string', | ||
| default: process.env.CKE5_TRIGGER_COMMIT_HASH | ||
| }, | ||
|
|
||
| /** | ||
| * Optional. If set to "true" or "1", commit author will be hidden. | ||
| * See: https://github.com/ckeditor/ckeditor5/issues/9252. | ||
| */ | ||
| 'hide-author': { | ||
| type: 'string', | ||
| default: process.env.CKE5_SLACK_NOTIFY_HIDE_AUTHOR | ||
| } | ||
| } | ||
| } ); | ||
|
|
||
| notifyGitHubActionsStatus().catch( err => { | ||
| console.error( err ); | ||
| process.exit( 1 ); | ||
| } ); | ||
|
|
||
| async function notifyGitHubActionsStatus() { | ||
| assertRequiredEnvironmentVariables(); | ||
|
|
||
| const serverUrl = ( GITHUB_SERVER_URL || 'https://github.com' ).replace( /\/$/, '' ); | ||
| const apiUrl = ( GITHUB_API_URL || 'https://api.github.com' ).replace( /\/$/, '' ); | ||
| const [ repositoryOwner, repositoryName ] = GITHUB_REPOSITORY.split( '/' ); | ||
| const runAttempt = GITHUB_RUN_ATTEMPT ? Number( GITHUB_RUN_ATTEMPT ) : 1; | ||
|
|
||
| const runData = await getWorkflowRunData( { apiUrl, repositoryOwner, repositoryName } ); | ||
| const buildUrl = [ serverUrl, GITHUB_REPOSITORY, 'actions', 'runs', GITHUB_RUN_ID, 'attempts', runAttempt ].join( '/' ); | ||
| const startedAtIso = runData.run_started_at || runData.created_at; | ||
|
|
||
| const message = await formatMessage( { | ||
| slackMessageUsername: 'GitHub Actions', | ||
| iconUrl: 'https://avatars.githubusercontent.com/in/15368?s=80&v=4', | ||
| repositoryOwner, | ||
| repositoryName, | ||
| branch: GITHUB_REF_NAME, | ||
| buildTitle: GITHUB_WORKFLOW || 'Workflow run', | ||
| buildUrl, | ||
| buildId: `#${ GITHUB_RUN_ID }${ runAttempt > 1 ? ` (attempt ${ runAttempt })` : '' }`, | ||
| githubToken: CKE5_GITHUB_TOKEN, | ||
| triggeringCommitUrl: getTriggeringCommitUrl( serverUrl ), | ||
| apiUrl, | ||
| startTime: startedAtIso ? Math.ceil( new Date( startedAtIso ).getTime() / 1000 ) : null, | ||
| endTime: Math.ceil( Date.now() / 1000 ), | ||
| shouldHideAuthor: isTrueLike( cliArguments[ 'hide-author' ] ) | ||
| } ); | ||
|
|
||
| return slackNotify( CKE5_SLACK_WEBHOOK_URL ) | ||
| .send( message ) | ||
| .catch( err => console.log( 'API error occurred:', err ) ); | ||
| } | ||
|
|
||
| function assertRequiredEnvironmentVariables() { | ||
| const required = { | ||
| CKE5_GITHUB_TOKEN, | ||
| CKE5_SLACK_WEBHOOK_URL, | ||
| GITHUB_REPOSITORY, | ||
| GITHUB_REF_NAME, | ||
| GITHUB_SHA, | ||
| GITHUB_RUN_ID | ||
| }; | ||
|
|
||
| for ( const [ name, value ] of Object.entries( required ) ) { | ||
| if ( !value ) { | ||
| throw new Error( `Missing environment variable: ${ name }` ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function getWorkflowRunData( { apiUrl, repositoryOwner, repositoryName } ) { | ||
| const fetchUrl = [ apiUrl, 'repos', repositoryOwner, repositoryName, 'actions', 'runs', GITHUB_RUN_ID ].join( '/' ); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| const fetchOptions = { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Accept': 'application/vnd.github+json', | ||
| 'Authorization': `Bearer ${ CKE5_GITHUB_TOKEN }`, | ||
| 'X-GitHub-Api-Version': '2022-11-28' | ||
| } | ||
| }; | ||
|
|
||
| const response = await fetch( fetchUrl, fetchOptions ); | ||
|
|
||
| if ( !response.ok ) { | ||
| throw new Error( `Failed to fetch workflow run ${ GITHUB_RUN_ID }: HTTP ${ response.status }.` ); | ||
| } | ||
|
|
||
| return response.json(); | ||
| } | ||
|
|
||
| function getTriggeringCommitUrl( serverUrl ) { | ||
| const cliRepoSlug = cliArguments[ 'trigger-repository-slug' ]; | ||
| const cliCommitHash = cliArguments[ 'trigger-commit-hash' ]; | ||
|
|
||
| const repoSlug = cliRepoSlug && cliCommitHash ? cliRepoSlug.trim() : GITHUB_REPOSITORY; | ||
| const hash = cliRepoSlug && cliCommitHash ? cliCommitHash.trim() : GITHUB_SHA; | ||
|
|
||
| return [ serverUrl, repoSlug, 'commit', hash ].join( '/' ); | ||
| } | ||
|
|
||
| function isTrueLike( value ) { | ||
| return value === true || value === 1 || value === '1' || value === 'true'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.