@@ -532,148 +532,3 @@ jobs:
532
532
}
533
533
_post "long_message.json" || _post "short_message.json"
534
534
535
- build-sentinel :
536
- if : always()
537
- needs :
538
- - build
539
- - docker-build
540
- - acceptance-tests-basic
541
- - acceptance-tests-extra
542
- - publish-docker
543
- runs-on : ubuntu-18.04
544
- steps :
545
- - uses : actions/checkout@v2
546
- - uses : actions/setup-node@v2
547
- with :
548
- node-version : 14
549
- - run : npm install @slack/web-api
550
- - uses : actions/github-script@v4
551
- env :
552
- SLACK_TOKEN : " ${{ secrets.BUILD_SENTINEL_SLACK_TOKEN }}"
553
- CHANNEL_NAME : " ${{ secrets.BUILD_SENTINEL_CHANNEL_NAME }}"
554
- # The value of the WORKFLOW_ID variable must be equal of the workflow filename, in this case build.yml
555
- WORKFLOW_ID : build.yml
556
- with :
557
- script : |
558
- const { WebClient } = require('@slack/web-api')
559
-
560
- const {
561
- SLACK_TOKEN,
562
- CHANNEL_NAME,
563
- WORKFLOW_ID
564
- } = process.env;
565
-
566
- const { data: { workflow_runs: runs } } = await github.actions.listWorkflowRuns({
567
- owner: context.repo.owner,
568
- repo: context.repo.repo,
569
- workflow_id: WORKFLOW_ID
570
- });
571
-
572
- const runId = "${{ github.run_id }}"
573
- const isLatestRun = runs[0].id == runId;
574
-
575
- // Only the latest run may publish a new message
576
- if (!isLatestRun) return;
577
-
578
- const slackClient = new WebClient(SLACK_TOKEN);
579
-
580
- const repository = "${{ github.repository }}";
581
-
582
- const pipelineUrl = `https://github.com/${repository}/actions/runs/${runId}`;
583
- const pipelineLinkBlock = {
584
- "type": "actions",
585
- "elements": [
586
- {
587
- "type": "button",
588
- "url": pipelineUrl,
589
- "text": {
590
- "type": "plain_text",
591
- "text": "Visit pipeline",
592
- "emoji": true
593
- }
594
- }
595
- ]
596
- };
597
-
598
- const successEmoji = ":large_green_circle:";
599
- const successBlocks = [
600
- {
601
- "type": "section",
602
- "text": {
603
- "type": "plain_text",
604
- "text": `${successEmoji} The build on ${repository} is back to green!`,
605
- "emoji": true
606
- }
607
- },
608
- pipelineLinkBlock
609
- ];
610
-
611
- const failureEmoji = ":red_circle:";
612
- const failureBlocks = [
613
- {
614
- "type": "section",
615
- "text": {
616
- "type": "plain_text",
617
- "text": `${failureEmoji} A build job on ${repository} has failed!`,
618
- "emoji": true
619
- }
620
- },
621
- pipelineLinkBlock
622
- ];
623
-
624
- const results = [
625
- "${{ needs['build'].result }}",
626
- "${{ needs['docker-build'].result }}",
627
- "${{ needs['acceptance-tests-1'].result }}",
628
- "${{ needs['acceptance-tests-2'].result }}",
629
- "${{ needs['publish-docker'].result }}",
630
- ];
631
- const isCurrentRunSuccessful = results.every(r => r === "success");
632
-
633
- // Find channel
634
- const { channels } = await slackClient.conversations.list({ types: "private_channel" });
635
- const channel = channels.find(c => c.name === CHANNEL_NAME);
636
-
637
- // Find latest bot message among the 100 latest channel messages
638
- // The number of loaded messages may be increased
639
- const { messages } = await slackClient.conversations.history({
640
- channel: channel.id,
641
- });
642
- const latestBotMessage = messages.find(m => {
643
- const isWrittenByTheBot = Boolean(m.bot_profile);
644
- if (!isWrittenByTheBot) return false;
645
- const notificationText = m.text || "";
646
- return notificationText.includes(repository) && (notificationText.startsWith(successEmoji) || notificationText.startsWith(failureEmoji));
647
- });
648
-
649
- if (!latestBotMessage) {
650
- /**
651
- * The bot is optimistic, if the latest message has not been found
652
- * and the run is successful, we assume the situation is fine
653
- * if the run is not successful, we publish (possibly again) an error message
654
- */
655
- if (!isCurrentRunSuccessful) {
656
- await slackClient.chat.postMessage({
657
- channel: channel.id,
658
- text: failureBlocks[0].text.text,
659
- blocks: failureBlocks
660
- })
661
- }
662
- return;
663
- }
664
-
665
- const isInSuccessState = latestBotMessage.text.startsWith(successEmoji);
666
-
667
- // If no change occurs, do not post any message
668
- if (isInSuccessState === isCurrentRunSuccessful) return;
669
-
670
- // Else, create the message and post it
671
- const blocks = isCurrentRunSuccessful
672
- ? successBlocks
673
- : failureBlocks;
674
-
675
- await slackClient.chat.postMessage({
676
- channel: channel.id,
677
- text: blocks[0].text.text,
678
- blocks
679
- })
0 commit comments