Skip to content

Commit

Permalink
[CI] Add an action to notify about "freezed" PRs. (#83)
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram authored Aug 15, 2024
1 parent 1507497 commit 09f1caf
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/scripts/fetch_prs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Octokit } from '@octokit/core';
import axios from 'axios';
import moment from 'moment';

const owner = 'deckhouse';
const repo = 'sds-node-configurator';
const project = 'SNC'

const octokit = new Octokit({ auth: process.env.PR_SUMMARY_TOKEN });


async function fetchPullRequests() {
const pulls = await octokit.request('GET /repos/{owner}/{repo}/pulls', {
owner,
repo,
state: 'all'
});
return pulls.data.filter(pr => !pr.draft);
}

const recentDays = 2;

function generateSummary(prs) {
const now = moment();
const reviewRequired = prs.filter(pr => pr.state === 'open' && pr.requested_reviewers.length > 0);
const recent = reviewRequired.filter(pr => moment().diff(moment(pr.created_at), 'days') <= recentDays);
const lasting = reviewRequired.filter(pr => moment().diff(moment(pr.created_at), 'days') > recentDays);

let summary = `## ${project} PRs ${now.format('YYYY-MM-DD')}\n\n`;

if (reviewRequired.length == 0) {
summary += `:tada: No review required for today\n`;
return summary;
}

if (recent.length > 0) {
summary += `### Recent PRs requiring review
${recent.map(pr => `- [${pr.title}](${pr.html_url}) (Created: ${moment(pr.created_at).fromNow()})`).join('\n')}
`;
}

if (lasting.length > 0) {
summary += `### PRs requiring review
${lasting.map(pr => `- [${pr.title}](${pr.html_url}) (Created: ${moment(pr.created_at).fromNow()})`).join('\n')}
`;
}

return summary
}

async function sendSummaryToLoop(summary) {
const url = process.env.LOOP_WEBHOOK_URL;
await axios.post(url, {
text: summary
});
}

async function run() {
try {
const prs = await fetchPullRequests();
const summary = generateSummary(prs);
await sendSummaryToLoop(summary);
} catch (error) {
console.error(error);
process.exit(1);
}
}

run();
11 changes: 11 additions & 0 deletions .github/scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "github-action-pr-summary",
"version": "1.0.0",
"main": ".github/scripts/fetch_prs.mjs",
"dependencies": {
"@octokit/core": "^3.2.5",
"axios": "^0.24.0",
"moment": "^2.29.1"
},
"type": "module"
}
47 changes: 47 additions & 0 deletions .github/workflows/prs-summary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: PR Summary

on:
schedule:
- cron: "0 7 * * 1-5" # Runs on every day-of-week from Monday through Friday at 7 AM UTC (10 AM MSK)
workflow_dispatch:

defaults:
run:
shell: bash

jobs:
pr_summary:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: "18" # Use Node.js 18

- name: Install dependencies
run: npm install -S .github/scripts

- name: Fetch PR Data and Send Summary
run: |
node ./.github/scripts/fetch_prs.mjs
env:
GITHUB_TOKEN: ${{ secrets.PR_SUMMARY_TOKEN }}
LOOP_WEBHOOK_URL: ${{ secrets.LOOP_WEBHOOK_URL }}

0 comments on commit 09f1caf

Please sign in to comment.