-
Notifications
You must be signed in to change notification settings - Fork 26
152 lines (133 loc) · 6.53 KB
/
cronJob.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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"
payload=$(jq -n \
--arg status "$STATUS" \
--arg branch "${{ github.ref }}" \
--arg build_url "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
'
{
"Status": $status,
"Branch": $branch,
"Build_url": $build_url
}')
curl -X POST -H 'Content-type: application/json' --data "$payload" "$NOTIFY_BUILD_RESULT" || echo "Slack notification failed with status code: $?"
env:
NOTIFY_BUILD_RESULT: ${{ secrets.NOTIFY_BUILD_RESULT }}
name: Run Slack Notification