Cron Job #456
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: Cron Job | |
on: | |
workflow_dispatch: | |
schedule: | |
# The job runs at 9:30 AM UTC every Monday through Friday, which is 3:00 PM IST and 4:30 AM EST. | |
# Ref - https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onschedule | |
- cron: '30 9 * * 1-5' | |
jobs: | |
fetch_all_pull_request_shas: | |
runs-on: ubuntu-latest | |
outputs: | |
pr_details: ${{ steps.extract.outputs.pr_details }} | |
is_empty: ${{ steps.extract.outputs.is_empty }} | |
env: | |
API_URL: https://api.github.com/repos/redhat-developer/lsp4ij/pulls | |
name: PR Details | |
steps: | |
- name: Extract PR numbers and merge_commit_shas | |
shell: bash | |
id: extract | |
run: | | |
# Fetch PR details from the GitHub API | |
pr_infos=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "${{ env.API_URL }}") | |
# Extract PR numbers and merge_commit_sha values, excluding draft pull requests | |
pr_numbers=$(echo "$pr_infos" | jq -r '.[] | select(.draft == false) | {number: .number, sha: .merge_commit_sha} | @base64') | |
# Array to store PRs that are not drafts and have no merge conflicts | |
declare -a valid_prs=() | |
for pr in $pr_numbers; do | |
# Decode the base64 encoded JSON string | |
pr=$(echo "$pr" | base64 --decode) | |
# Extract PR number | |
number=$(echo "$pr" | jq -r '.number') | |
url="${{ env.API_URL }}/$number" | |
mergeable=null | |
attempts=0 | |
max_attempts=5 | |
# Retry loop for checking mergeability | |
while [[ "$mergeable" == "null" && $attempts -lt $max_attempts ]]; do | |
pr_detail=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$url") | |
mergeable=$(jq -r '.mergeable' <<< "$pr_detail") | |
echo "PR Number $number Mergeable value now : $mergeable" | |
if [[ "$mergeable" == "null" ]]; then | |
echo "Mergeable status is null, retrying in 5 seconds..." | |
sleep 5 | |
fi | |
attempts=$((attempts + 1)) | |
done | |
if [[ "$mergeable" == "null" ]]; then | |
echo "::warning file=::PR #$(jq -r '.number' <<< "$pr_detail") still has Mergeable value of 'null' after $max_attempts attempts." | |
fi | |
if [[ "$mergeable" != "false" ]]; then | |
pr_number=$(jq -r '.number' <<< "$pr_detail") | |
pr_sha=$(jq -r '.merge_commit_sha' <<< "$pr_detail") | |
pr_link=$(jq -r '.html_url' <<< "$pr_detail") | |
valid_prs+=("{\"number\": \"$pr_number\", \"sha\": \"$pr_sha\", \"link\": \"$pr_link\"}") | |
else | |
echo "::warning file=::PR #$(jq -r '.number' <<< "$pr_detail") has conflicts. See : $(jq -r '.html_url' <<< "$pr_detail")" | |
fi | |
done | |
# Create a JSON string from the array | |
pr_details_array=$(IFS=,; echo "[${valid_prs[*]}]") | |
# Print Pr number and SHA values | |
echo "PRs having no merge conflicts" | |
echo "$pr_details_array" | jq '.[]' | |
# Set the output for further steps | |
echo "pr_details=$pr_details_array" >> $GITHUB_OUTPUT | |
# Check if pr_details_array is empty | |
if [ $(echo "$pr_details_array" | jq length) -eq 0 ]; then | |
echo "::warning file=::There are no open PRs, or all the existing PRs are either drafts or have merge conflicts. Skipping further actions." | |
echo "is_empty=true" >> $GITHUB_OUTPUT | |
else | |
echo "is_empty=false" >> $GITHUB_OUTPUT | |
fi | |
# Run the LTI Tests against each open lsp4ij PRs | |
call-build-workflow-for-each-merge-commit-sha: | |
needs: fetch_all_pull_request_shas | |
if: ${{ needs.fetch_all_pull_request_shas.outputs.is_empty == 'false' }} | |
uses: ./.github/workflows/build.yaml | |
strategy: | |
fail-fast: false | |
matrix: | |
# Existing LTI release tags and branches can be added to obtain build results.If the tag array is empty, it will default to 'main'. However, if there is at least one tag or branch in the tag array and you need to run on 'main' as well, make sure to add 'main' to the array. | |
tag: [ 'main' ] # Can specify tags or branches such as '24.0.6' and 'main' | |
pr_details: ${{ fromJson(needs.fetch_all_pull_request_shas.outputs.pr_details) }} | |
with: | |
useLocalPlugin: true | |
refLsp4ij: ${{ matrix.pr_details.sha }} | |
lsp4ijBranch: PR-${{ matrix.pr_details.number }} | |
refLTITag: ${{ matrix.tag }} | |
name: Run PR | |
# Run the LTI Tests against lsp4ij main branch | |
call-build-workflow-for-lsp4ij-main-branch: | |
uses: ./.github/workflows/build.yaml | |
strategy: | |
fail-fast: false | |
matrix: | |
# Existing LTI release tags and branches can be added to obtain build results.If the tag array is empty, it will default to 'main'. However, if there is at least one tag or branch in the tag array and you need to run on 'main' as well, make sure to add 'main' to the array. | |
tag: [ 'main' ] | |
with: | |
useLocalPlugin: true | |
refLsp4ij: main | |
lsp4ijBranch: main | |
refLTITag: ${{ matrix.tag }} | |
name: Run Lsp4ij Main | |
# Send slack notification for build results | |
call-build-workflow-slack-notification: | |
runs-on: ubuntu-latest | |
needs: [ call-build-workflow-for-lsp4ij-main-branch, call-build-workflow-for-each-merge-commit-sha ] | |
if: always() | |
steps: | |
- name: Send Slack Notification | |
run: | | |
STATUS="failure" | |
JOB_STATUS_MAIN="${{ needs.call-build-workflow-for-lsp4ij-main-branch.result }}" | |
JOB_STATUS_PR="${{ needs.call-build-workflow-for-each-merge-commit-sha.result }}" | |
if [[ "$JOB_STATUS_MAIN" == "success" ]] && [[ "$JOB_STATUS_PR" = "success" ]]; then | |
STATUS="success" | |
fi | |
echo "Final status: $STATUS" | |
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"Workflow ${{ github.workflow }} triggered by branch *${{ github.ref }}*. Build results: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}. Status: *$STATUS*\"}" $SLACK_WEBHOOK_URL | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
name: Run Slack Notification |