Fetch All Open and Recently Closed Pull Requests using condition using cron job #140
This file contains 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
name: Fetch All Open Pull Requests using condition using cron job | |
on: | |
schedule: | |
- cron: '30 15 * * 1-5' | |
jobs: | |
fetch_all_pull_requests_and_notify_using_condition_and_cron: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Restore cache | |
id: cache-restore | |
uses: actions/cache@v2 | |
with: | |
path: cache | |
key: ${{ runner.os }}-pr-cache-${{ github.run_number }} | |
restore-keys: | | |
${{ runner.os }}-pr-cache- | |
- name: Ensure cache directory exists | |
run: mkdir -p cache | |
- name: Check if cache restored | |
run: | | |
if [ -f cache/notified_prs.json ]; then | |
echo "Cache restored successfully." | |
cat cache/notified_prs.json | |
else | |
echo "Cache not restored or file does not exist." | |
fi | |
- name: Fetch all opened pull request details using condition | |
id: fetch_all_pull_requests_using_condition | |
run: | | |
pr_infos=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/vaisakhkannan/liberty-tools-intellij/pulls?state=open&sort=created&direction=desc&per_page=100") | |
echo "API Data ------$pr_infos" | |
# Load previous PR data if exists | |
if [ -f cache/notified_prs.json ]; then | |
previous_prs=$(cat cache/notified_prs.json) | |
echo "Previous PRs -------$previous_prs" | |
else | |
previous_prs="[]" | |
fi | |
pr_list="" | |
new_notified_prs="[]" | |
notify=false | |
for pr_info in $(echo "$pr_infos" | jq -r '.[] | @base64'); do | |
_jq() { | |
echo "$pr_info" | base64 --decode | jq -r "${1}" | |
} | |
pr_number=$(_jq '.number') | |
pr_title=$(_jq '.title') | |
pr_user=$(_jq '.user.login') | |
pr_url=$(_jq '.html_url') | |
pr_draft=$(_jq '.draft') | |
pr_created_at=$(_jq '.created_at') | |
pr_updated_at=$(_jq '.updated_at') | |
pr_data=$(jq -n --arg number "$pr_number" --arg updated_at "$pr_updated_at" '{number: $number, updated_at: $updated_at}') | |
new_notified_prs=$(echo "$new_notified_prs" | jq --argjson pr_data "$pr_data" '. += [$pr_data]') | |
echo "Whole PR data ------- $pr_data" | |
echo "New Notified PR List------ $new_notified_prs" | |
# Check if the PR is new or updated | |
previous_pr=$(echo "$previous_prs" | jq --arg number "$pr_number" '.[] | select(.number == $number)') | |
echo "Checking PR is new or not --- $previous_pr" | |
if [ -z "$previous_pr" ] || [ "$(echo "$previous_pr" | jq -r '.updated_at')" != "$pr_updated_at" ]; then | |
pr_list="${pr_list}\n*Pull Request* #${pr_number}: ${pr_title}\n*Created by*: ${pr_user}\n*URL*: ${pr_url}\n*Draft*: ${pr_draft}\n*Created At*: ${pr_created_at}\n*Last Updated At*: ${pr_updated_at}\n" | |
echo "New PR List ------- $pr_list" | |
notify=true | |
echo "Boolean Value for notify ------- $notify" | |
fi | |
done | |
# Save current PR data for future comparison | |
echo "$new_notified_prs" > cache/notified_prs.json | |
echo "Latest Modified ------" | |
cat cache/notified_prs.json # Ensure the JSON file is updated and print its content for debugging | |
if [ "$notify" = true ]; then | |
echo -e "$pr_list" > pr_list.txt | |
echo "::set-output name=notify::true" | |
else | |
echo "::set-output name=notify::false" | |
fi | |
- name: Debug Notify Value | |
run: | | |
echo "Notify value: ${{ steps.fetch_all_pull_requests_using_condition.outputs.notify }}" | |
- name: Send Slack notification | |
if: success() && steps.fetch_all_pull_requests_using_condition.outputs.notify == 'true' | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
run: | | |
echo "Condition value ------: ${{ steps.fetch_all_pull_requests_using_condition.outputs.notify }}" | |
pr_list=$(cat pr_list.txt) | |
payload=$(cat <<-EOF | |
{ | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": "List of Open Pull Requests using condition latest cron job" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "${pr_list}" | |
} | |
} | |
] | |
} | |
EOF | |
) | |
echo "$payload" # Debugging: Print payload to verify its formatt | |
curl -X POST -H 'Content-type: application/json' --data "$payload" $SLACK_WEBHOOK_URL || echo "Slack notification failed with status code: $?" | |
- name: Verify Cache Save | |
if: always() | |
run: | | |
echo "Checking saved cache content..." | |
ls -l cache/ | |
cat cache/notified_prs.json |