From defef76407e6e18f97c8208af76f459ca79b7ab1 Mon Sep 17 00:00:00 2001
From: Nikhil S Kalburgi <70331875+nikhilkalburgi@users.noreply.github.com>
Date: Tue, 24 Sep 2024 18:02:37 +0530
Subject: [PATCH] add workflow for Agenda Verification and Notifications before
 Monday Meetings (#802)

* Add ocwm-pre-meeting-check.yml to workflow

* change ocwm-pre-meeting-check.yml comment

* fix: update ocwm-pre-meeting-check.yml with suggested changes

* update ocwm-pre-meeting-check.yml

* update ocwm-pre-meeting-check.yml for test

* second update ocwm-pre-meeting-check.yml for test

* third update ocwm-pre-meeting-check.yml for test

* forth update ocwm-pre-meeting-check.yml for test

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

* fix: improve ocwm-pre-meeting-check.yml

* Update ocwm-pre-meeting-check.yml

We are facing issues with this same logic in other workflows. I am just applying the same version we have in other workflows to consistently merge with master branch.

---------

Co-authored-by: Benjamin Granados <40007659+benjagm@users.noreply.github.com>
---
 .github/workflows/ocwm-pre-meeting-check.yml | 107 +++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 .github/workflows/ocwm-pre-meeting-check.yml

diff --git a/.github/workflows/ocwm-pre-meeting-check.yml b/.github/workflows/ocwm-pre-meeting-check.yml
new file mode 100644
index 00000000..cf83898f
--- /dev/null
+++ b/.github/workflows/ocwm-pre-meeting-check.yml
@@ -0,0 +1,107 @@
+name: OCWM Pre-Meeting Check
+
+on:
+  schedule:
+    - cron: '50 21 * * 1'  # Runs at 11:50 AM PT on the 3rd Monday of the month   
+
+jobs:
+  check_agenda:
+    runs-on: ubuntu-latest
+
+    steps:
+    - name: Checkout Repository
+      uses: actions/checkout@v4
+
+    - name: Set up Node 20
+      uses: actions/setup-node@v4
+      with:
+        node-version: '20'
+
+    - name: Get GitHub Token
+      uses: actions/create-github-app-token@v1
+      id: get_workflow_token
+      with:
+        app-id: ${{ vars.APP_ID }}
+        private-key: ${{ secrets.PRIVATE_KEY }}
+        
+    - name: Install Dependencies
+      run: npm install @octokit/core@5.1.0
+
+    # Step to check if today is the third Monday
+    - name: Check if today is the third Monday
+      id: check-third-monday
+      run: |
+        day=$(date +%d)
+        dow=$(date +%u)  # Day of the week (1 = Monday, ..., 7 = Sunday)
+        # Check if the day is between 15th and 21st, and if it's Monday (1)
+        if [ "$dow" -ne 1 ]; then
+          echo "Not a Monday, exiting..."
+          echo "::set-output name=is-third-monday::false"
+          exit 0
+        fi
+        if [ "$day" -ge 15 ] && [ "$day" -le 21 ]; then
+          echo "This is the third Monday of the month!"
+          echo "::set-output name=is-third-monday::true"
+        else
+          echo "Not the third Monday, exiting..."
+          echo "::set-output name=is-third-monday::false"
+          exit 0
+        fi 
+
+    - name: Check Latest OCWM Issue
+      id: check_issue
+      if: steps.check-third-monday.outputs.is-third-monday == 'true'
+      uses: actions/github-script@v7
+      env:
+        MY_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
+        OWNER: ${{ vars.ORGANISATION }}
+        REPO: 'community'
+        OCWM_LABEL: ${{ vars.OCWM_LABEL }}
+        TEMPLATE_PATH: '.github/ISSUE_TEMPLATE/open_community_working_meeting.md'
+        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_GEN_NOTIF }}
+      with:
+        script: |
+          const fs = require('fs');
+          const octokit = require('@octokit/core').Octokit;
+          const mygithub = new octokit({
+            request: { fetch: fetch, },
+            auth: process.env.MY_TOKEN
+          });
+  
+          // Read the template from the markdown file
+          const templateContent = fs.readFileSync(process.env.TEMPLATE_PATH, 'utf8');
+  
+          // Fetch the latest issue with OCWM_LABEL
+          const { data: issues } = await mygithub.request(`GET /repos/${process.env.OWNER}/${process.env.REPO}/issues?labels=${encodeURIComponent(process.env.OCWM_LABEL)}&per_page=1`);
+  
+          if (issues.length === 0) {
+            console.log("No open community working meeting issues found.");
+            return;
+          }
+  
+          const latestIssue = issues[0];
+          const issueBody = latestIssue.body;
+  
+          // Check if the issue body matches the template
+          if (issueBody.includes(templateContent)) {
+            console.log("Template matched, cancelling the meeting.");
+  
+            // Add a comment to the issue
+            await mygithub.request(`POST /repos/${process.env.OWNER}/${process.env.REPO}/issues/${latestIssue.number}/comments`, {
+              body: "The meeting has been cancelled as there is no agenda for today. Thanks everyone!"
+            });
+            
+            // Send a notification to Slack
+            const slackPayload = {
+              text: `The meeting has been cancelled as there is no agenda for today. Thanks everyone!`
+            };
+  
+            await fetch(process.env.SLACK_WEBHOOK, {
+              method: 'POST',
+              headers: { 'Content-Type': 'application/json' },
+              body: JSON.stringify(slackPayload)
+            });
+  
+          } else {
+            console.log("Agenda found. Meeting will proceed.");
+          }