Skip to content

Commit

Permalink
move script to a file and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sam4t committed Jun 6, 2024
1 parent ba0d0be commit 0560085
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ on:
push

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3
- uses: ./
with:
pr_age_in_hours: 24
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# stale-bot-pr-notifier
Github Action to notify stale PRs from dependabot/renovate to the slack channel.
# Stale PR Reporter
Github Action to report stale PRs from dependabot/renovate to the slack channel.

## Overview
The **Stale Bot PR Notifier** is a GitHub Action designed to notify a Slack channel when pull requests from Dependabot and Renovate have become stale based on a specified timeframe. This action is useful for teams that want to keep track of aging PRs to ensure they are reviewed and merged in a timely manner.
The **Stale PR Reporter** is a GitHub Action designed to report Slack when pull requests from Dependabot and Renovate have become stale based on a specified timeframe. This action is useful for teams that want to keep track of aging PRs to ensure they are reviewed and merged in a timely manner.

## Features
- Checks for open pull requests from Dependabot and Renovate.
- Notifies a Slack channel if these PRs have been open longer than a specified number of hours.
- Reports to Slack if these PRs have been open longer than a specified number of hours (default: 48 hours).
- Configurable threshold for PR staleness.

## Inputs
Expand All @@ -26,14 +26,15 @@ The **Stale Bot PR Notifier** is a GitHub Action designed to notify a Slack chan
1. **Create a Slack Webhook**:
- Navigate to your Slack App settings and create a new incoming webhook.
- Copy the webhook URL which will be used to send notifications from this action.
- Add the webhook secret to SecretsManager

2. **Add the GitHub Action to Your Repository**:
- Create a directory `.github/workflows` in your repository if it doesn't already exist.
- Add a new YAML file in this directory for the workflow, e.g., `stale-pr-notifier.yml`.
- Add a new YAML file in this directory for the workflow, e.g., `stale-pr-reporter.yml`.

### Example Workflow File
Create a file named `.github/workflows/stale-pr-notifier.yml` and add the following content:
yaml name: Stale Bot PR Notifier
Create a file named `.github/workflows/stale-pr-reporter.yml` and add the following content:
yaml name: Stale PR Reporter
```
on:
schedule:
Expand All @@ -43,10 +44,11 @@ jobs:
check-open-prs:
runs-on: ubuntu-latest
steps:
- name: Notify Slack
uses: babbel/stale-bot-pr-notifier@1.0.0
- name: Report to Slack
uses: babbel/stale-pr-reporter@1.0.0
with:
slack_webhook_url: ${{ secrets.slack_webhook_url }}
pr_age_in_hours: 24
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
47 changes: 8 additions & 39 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
name: Stale Bot PR Notifier
description: GitHub Action notifies Slack about pull requests from Dependabot and Renovate that have become stale.
name: Stale PR Reporter
description: GitHub Action reports to Slack about pull requests from Dependabot and Renovate that have become stale.

inputs:
pr_age_in_hours:
description: Total number of hours since the Pull Request (PR) was opened. Default value is 48 hours
required: false
default: 48
slack_webhook_url:
description: Slack webhook
description: The Slack webhook URL used to send messages to a Slack. This URL is provided by Slack when you create a new incoming webhook in your Slack workflows. Ensure this URL is kept secure.
required: true
runs:
using: composite

steps:
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3
- name: Fetch all the open PR's from dependabot/renovate
env:
repository: ${{ github.repository }}
pr_age_in_hours: ${{inputs.pr_age_in_hours}}
shell: bash
run: |
# Install jq for JSON processing
sudo apt-get install jq -y
# Get all the open PRs in the repository
PR_LIST=$(gh pr list --repo ${{github.repository}} --state open --json "number,url,createdAt,author" | jq -c '.[]' | \
while read pr; do
author=$(echo $pr | jq -r '.author.login')
url=$(echo $pr | jq -r '.url')
pr_created_at=$(echo $pr | jq -r '.createdAt')
# Calculate the age of the PR in hours
pr_age=$(ruby -e "require 'time'; puts (Time.now.utc - Time.parse('$pr_created_at')).to_i")
time_diff=$((pr_age / 3600))
if [[ ("$author" == "app/dependabot" || "$author" == "app/lessonnine-renovate") && "$time_diff" -gt ${{inputs.pr_age_in_hours}} ]]; then
echo "$url"
fi
done)
# Check if PR_LIST is empty
if [ -z "$PR_LIST" ]; then
echo "No open PRs found."
else
# Create a JSON payload file
jq -n \
--arg repository ${{github.repository}} \
--arg pr_list "$PR_LIST" \
'{
repository: $repository,
pr_list: $pr_list,
}' > payload.json
fi
echo "PR_LIST=$PR_LIST" >> $GITHUB_ENV
- name: Notify Slack
bash ${{ github.action_path }}/bin/script.sh
- name: Report to Slack
if: ${{ env.PR_LIST != '' }}
uses: slackapi/slack-github-action@v1
with:
Expand Down
44 changes: 44 additions & 0 deletions bin/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Install jq for JSON processing
sudo apt-get install jq -y

# Get all the open PRs in the repository
PR_LIST=$(gh pr list --repo $repository --state open --json "number,url,createdAt,author" | jq -c '.[]' | \
while read pr; do
author=$(echo $pr | jq -r '.author.login')
url=$(echo $pr | jq -r '.url')
pr_created_at=$(echo $pr | jq -r '.createdAt')
# Convert PR creation date to Unix timestamp (seconds since 1970-01-01 00:00:00 UTC)
pr_created_at_unix=$(date -d "$pr_created_at" +%s)
# Get current date in Unix timestamp
current_unix=$(date +%s)
# Calculate the age of the PR in hours
pr_age=$(( (current_unix - pr_created_at_unix) / 3600 ))
if [[ ("$author" == "app/dependabot" || "$author" == "app/lessonnine-renovate") && "$pr_age" -gt $pr_age_in_hours ]]; then
echo "$url"
fi
done)
# Check if PR_LIST is empty
if [ -z "$PR_LIST" ]; then
echo "No open PRs found."
else
# Create a JSON payload file
jq -n \
--arg repository "$repository" \
--arg pr_list "$PR_LIST" \
--arg pr_age_in_hours "$pr_age_in_hours" \
'{
repository: $repository,
pr_list: $pr_list,
pr_age_in_hours: $pr_age_in_hours
}' > payload.json
PR_LIST="true"
fi
echo "PR_LIST=$PR_LIST" >> $GITHUB_ENV

0 comments on commit 0560085

Please sign in to comment.