|
| 1 | +--- |
| 2 | +name: 'Control Check' |
| 3 | +description: 'Helps manage the workflow checks' |
| 4 | +author: 'Mr. Walls' |
| 5 | +branding: |
| 6 | + icon: 'check-circle' |
| 7 | + color: 'black' |
| 8 | +inputs: |
| 9 | + sha: |
| 10 | + description: | |
| 11 | + The commit to attach the check to. When running this action on github.com, |
| 12 | + the default value is sufficient. |
| 13 | + required: true |
| 14 | + default: ${{ github.server_url == 'https://github.com' && github.sha || '0000000000000000000000000000000000000000' }} |
| 15 | + workflow-run-id: |
| 16 | + description: | |
| 17 | + The workflow run to to attach the check to. When running this action on github.com, |
| 18 | + the default value is the calling workflow. |
| 19 | + required: true |
| 20 | + default: ${{ github.server_url == 'https://github.com' && github.run_id || '' }} |
| 21 | + check-id: |
| 22 | + description: | |
| 23 | + The check's ID to update (Optional). Must be the check-id from a previously created check. |
| 24 | + required: false |
| 25 | + default: '' |
| 26 | + token: |
| 27 | + description: | |
| 28 | + The token used to authenticate when fetching Python distributions from |
| 29 | + https://github.com/actions/python-versions. When running this action on github.com, |
| 30 | + the default value is sufficient. When running on GHES, you can pass a personal access |
| 31 | + token for github.com if you are experiencing rate limiting. |
| 32 | + default: ${{ github.server_url == 'https://github.com' && github.token || '' }} |
| 33 | + required: true |
| 34 | + details-url: |
| 35 | + description: | |
| 36 | + The check's details url. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 37 | + for documentation. |
| 38 | + default: 'DEFAULT' |
| 39 | + required: true |
| 40 | + name: |
| 41 | + description: | |
| 42 | + The check's display name. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 43 | + for documentation. |
| 44 | + default: ${{ github.server_url == 'https://github.com' && github.workflow || '' }} |
| 45 | + required: true |
| 46 | + title: |
| 47 | + description: | |
| 48 | + The check's title. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 49 | + for documentation. |
| 50 | + default: ${{ github.server_url == 'https://github.com' && github.workflow || '' }} |
| 51 | + required: true |
| 52 | + summary: |
| 53 | + description: | |
| 54 | + The check's title. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 55 | + for documentation. |
| 56 | + default: '' |
| 57 | + required: true |
| 58 | + text: |
| 59 | + description: | |
| 60 | + The check's body text. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 61 | + for documentation. |
| 62 | + default: '' |
| 63 | + required: true |
| 64 | + status: |
| 65 | + description: | |
| 66 | + The check's status. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 67 | + for documentation. |
| 68 | + default: 'completed' |
| 69 | + required: false |
| 70 | + type: choice |
| 71 | + options: |
| 72 | + - queued |
| 73 | + - in_progress |
| 74 | + - completed |
| 75 | + conclusion: |
| 76 | + description: | |
| 77 | + The check's conclusion. See 'https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run' |
| 78 | + for documentation. |
| 79 | + default: '' |
| 80 | + required: false |
| 81 | + type: choice |
| 82 | + options: |
| 83 | + - cancelled |
| 84 | + - failure |
| 85 | + - neutral |
| 86 | + - success |
| 87 | + - skipped |
| 88 | + - timed_out |
| 89 | +outputs: |
| 90 | + check-id: |
| 91 | + description: "The check ID operated on." |
| 92 | + value: ${{ steps.output_check_id.outputs.check_id || '' }} |
| 93 | + check-url: |
| 94 | + description: "The url for the check-run operated on." |
| 95 | + value: ${{ steps.output_check_id.outputs.check_url || '' }} |
| 96 | + check-operation: |
| 97 | + description: "The operation performed for the check" |
| 98 | + value: ${{ steps.output_check_outcome.outputs.check_operation }} |
| 99 | + check-date: |
| 100 | + description: "The event date for the check." |
| 101 | + value: ${{ steps.output_date.outputs.check_date }} |
| 102 | + api-outcome: |
| 103 | + description: "The outcome of the api operation" |
| 104 | + value: ${{ steps.output_check_outcome.outputs.check_outcome || 'cancelled' }} |
| 105 | + |
| 106 | +runs: |
| 107 | + using: composite |
| 108 | + steps: |
| 109 | + - id: output_sha |
| 110 | + if: ${{ !cancelled() }} |
| 111 | + shell: bash |
| 112 | + run: printf "sha=%s\n" $(git rev-parse --verify '${{ inputs.sha }}') >> "$GITHUB_OUTPUT" |
| 113 | + - id: output_uuid |
| 114 | + if: ${{ !cancelled() && (inputs.check-id == '') }} |
| 115 | + shell: bash |
| 116 | + run: | |
| 117 | + if [[ ${{ runner.os }} != 'Windows' ]] ; then |
| 118 | + printf "uuid=%s\n" $(uuidgen) >> "$GITHUB_OUTPUT" |
| 119 | + else |
| 120 | + printf "uuid=%04x%04x-%04x-%04x-%04x-%04x%04x%04x\n" $RANDOM $RANDOM $RANDOM $(($RANDOM & 0x0fff | 0x4000)) $(($RANDOM & 0x3fff | 0x8000)) $RANDOM $RANDOM $RANDOM >> "$GITHUB_OUTPUT" |
| 121 | + fi |
| 122 | + - id: output_date |
| 123 | + if: ${{ !cancelled() }} |
| 124 | + shell: bash |
| 125 | + run: | |
| 126 | + printf "check_date=%s\n" $(TZ=UTC date -Iseconds | cut -d+ -f1-1) >> "$GITHUB_OUTPUT" |
| 127 | + - id: output_check_details_url |
| 128 | + if: ${{ !cancelled() && (github.repository == 'reactive-firewall/multicast') }} |
| 129 | + shell: bash |
| 130 | + run: | |
| 131 | + if [[ "${{ inputs.details-url }}" != "" ]] ; then |
| 132 | + printf "details_url=%s\n" '${{ inputs.details-url }}' >> "$GITHUB_OUTPUT" |
| 133 | + printf "::debug:: %s\n" "Check detail url was provided: ${{ inputs.details-url }}" ; |
| 134 | + else |
| 135 | + if [[ "${{ inputs.workflow-run-id }}" != "" ]] ; then |
| 136 | + printf "details_url=%s\n" 'https://github.com/reactive-firewall/multicast/actions/runs/${{ github.run_id }}' >> "$GITHUB_OUTPUT" |
| 137 | + printf "::debug:: %s\n" "Check detail url was created from workflow ${{ inputs.workflow-run-id }}" ; |
| 138 | + else |
| 139 | + if [[ "${{ inputs.check-id }}" != "" ]] ; then |
| 140 | + printf "details_url=https://github.com/reactive-firewall/multicast/runs/%s\n" ${{ inputs.check-id }} >> "$GITHUB_OUTPUT" |
| 141 | + printf "::debug:: %s\n" "Check detail url was created from id given: ${{ inputs.check-id }}" ; |
| 142 | + else |
| 143 | + printf "details_url=null\n" >> "$GITHUB_OUTPUT" |
| 144 | + printf "::warning title='NO Detail Link':: %s\n" "Check details URL is NULL" ; |
| 145 | + fi |
| 146 | + fi |
| 147 | + fi |
| 148 | + - name: "Create New Check" |
| 149 | + id: create_new_check |
| 150 | + if: ${{ (github.repository == 'reactive-firewall/multicast') && (inputs.check-id == '') && (inputs.status != 'completed') }} |
| 151 | + shell: bash |
| 152 | + env: |
| 153 | + GH_TOKEN: ${{ inputs.token }} |
| 154 | + run: | |
| 155 | + printf "%s\n" "::group::create-new-check" |
| 156 | + # GitHub CLI api |
| 157 | + # https://cli.github.com/manual/gh_api |
| 158 | + CHECK_ID=$(gh api --method POST -H "Accept: application/vnd.github+json" \ |
| 159 | + /repos/reactive-firewall/multicast/check-runs \ |
| 160 | + -f "name=${{ inputs.name }}" -f "head_sha=${{ steps.output_sha.outputs.sha }}" \ |
| 161 | + -f "status=${{ inputs.status }}" -f "external_id=${{ steps.output_uuid.outputs.uuid }}" \ |
| 162 | + -f "started_at=${{ steps.output_date.outputs.check_date }}Z" \ |
| 163 | + -f "details_url=${{ steps.output_check_details_url.outputs.details_url }}" \ |
| 164 | + -f 'output[title]=${{ inputs.title }}' \ |
| 165 | + -f 'output[summary]=' -f 'output[text]=' --jq '.id'); |
| 166 | + printf "check_id=%s\n" "${CHECK_ID}" >> "$GITHUB_OUTPUT" |
| 167 | + printf "%s\n" "::endgroup::" |
| 168 | + - name: "Forge New Check" |
| 169 | + id: forge_new_check |
| 170 | + if: ${{ (github.repository == 'reactive-firewall/multicast') && (inputs.check-id == '') && (inputs.status == 'completed') }} |
| 171 | + shell: bash |
| 172 | + env: |
| 173 | + GH_TOKEN: ${{ inputs.token }} |
| 174 | + run: | |
| 175 | + printf "%s\n" "::group::update-new-check" |
| 176 | + # GitHub CLI api |
| 177 | + # https://cli.github.com/manual/gh_api |
| 178 | + CHECK_ID=$(gh api --method POST -H "Accept: application/vnd.github+json" \ |
| 179 | + /repos/reactive-firewall/multicast/check-runs \ |
| 180 | + -f "name=${{ inputs.name }}" -f "head_sha=${{ steps.output_sha.outputs.sha }}" \ |
| 181 | + -f "status=in_progress" -f "external_id=${{ steps.output_uuid.outputs.uuid }}" \ |
| 182 | + -f "started_at=${{ steps.output_date.outputs.check_date }}Z" \ |
| 183 | + -f "details_url=${{ steps.output_check_details_url.outputs.details_url }}" \ |
| 184 | + -f 'output[title]=${{ inputs.title }}' \ |
| 185 | + -f 'output[summary]=Check is in progress.' -f 'output[text]=' --jq '.id'); |
| 186 | + printf "check_id=%s\n" "${CHECK_ID}" >> "$GITHUB_OUTPUT" |
| 187 | + printf "%s\n" "::endgroup::" |
| 188 | + - id: output_check_id |
| 189 | + if: ${{ !cancelled() && (github.repository == 'reactive-firewall/multicast') }} |
| 190 | + shell: bash |
| 191 | + run: | |
| 192 | + if [[ "${{ steps.create_new_check.outcome }}" == "success" ]] ; then |
| 193 | + printf "check_id=%s\n" ${{ steps.create_new_check.outputs.check_id }} >> "$GITHUB_OUTPUT" |
| 194 | + printf "check_url=https://github.com/reactive-firewall/multicast/runs/%s\n" ${{ steps.create_new_check.outputs.check_id }} >> "$GITHUB_OUTPUT" |
| 195 | + printf "::debug:: %s\n" "Check id was created as ${{ steps.create_new_check.outputs.check_id }}" ; |
| 196 | + else |
| 197 | + if [[ "${{ steps.forge_new_check.outcome }}" == "success" ]] ; then |
| 198 | + printf "check_id=%s\n" ${{ steps.forge_new_check.outputs.check_id }} >> "$GITHUB_OUTPUT" |
| 199 | + printf "check_url=https://github.com/reactive-firewall/multicast/runs/%s\n" ${{ steps.forge_new_check.outputs.check_id }} >> "$GITHUB_OUTPUT" |
| 200 | + printf "::debug:: %s\n" "Check id is forged as ${{ steps.forge_new_check.outputs.check_id }}" ; |
| 201 | + else |
| 202 | + if [[ "${{ inputs.check-id }}" != "" ]] ; then |
| 203 | + printf "check_id=%s\n" ${{ inputs.check-id }} >> "$GITHUB_OUTPUT" |
| 204 | + printf "check_url=https://github.com/reactive-firewall/multicast/runs/%s\n" ${{ inputs.check-id }} >> "$GITHUB_OUTPUT" |
| 205 | + printf "::debug:: %s\n" "Check id given is ${{ inputs.check-id }}" ; |
| 206 | + else |
| 207 | + printf "check_id=null\n" >> "$GITHUB_OUTPUT" |
| 208 | + printf "::warning title='NO ID':: %s\n" "Check id is NULL" ; |
| 209 | + fi |
| 210 | + fi |
| 211 | + fi |
| 212 | + - name: "Update Check by ID" |
| 213 | + id: update_check |
| 214 | + if: ${{ (github.repository == 'reactive-firewall/multicast') && (steps.output_check_id.outputs.check_id != '') && (inputs.status != 'completed') }} |
| 215 | + shell: bash |
| 216 | + env: |
| 217 | + GH_TOKEN: ${{ inputs.token }} |
| 218 | + run: | |
| 219 | + printf "%s\n" "::group::update-check" |
| 220 | + # GitHub CLI api |
| 221 | + # https://cli.github.com/manual/gh_api |
| 222 | + gh api --method PATCH -H "Accept: application/vnd.github+json" \ |
| 223 | + /repos/reactive-firewall/multicast/check-runs/${{ steps.output_check_id.outputs.check_id }} \ |
| 224 | + -f "name=${{ inputs.name }}" -f "head_sha=${{ steps.output_sha.outputs.sha }}" \ |
| 225 | + -f "status=${{ inputs.status }}" \ |
| 226 | + -f "details_url=${{ steps.output_check_details_url.outputs.details_url }}" \ |
| 227 | + -f 'output[title]=${{ inputs.title }}' \ |
| 228 | + -f 'output[summary]=${{ inputs.summary }}' -f 'output[text]=${{ inputs.text }}' |
| 229 | + printf "%s\n" "::endgroup::" |
| 230 | + - name: "Update Check" |
| 231 | + id: compleate_check |
| 232 | + if: ${{ (github.repository == 'reactive-firewall/multicast') && (steps.output_check_id.outputs.check_id != '') && (inputs.conclusion != '') }} |
| 233 | + shell: bash |
| 234 | + env: |
| 235 | + GH_TOKEN: ${{ inputs.token }} |
| 236 | + run: | |
| 237 | + printf "%s\n" "::group::compleate-check" |
| 238 | + # GitHub CLI api |
| 239 | + # https://cli.github.com/manual/gh_api |
| 240 | + gh api --method PATCH -H "Accept: application/vnd.github+json" \ |
| 241 | + /repos/reactive-firewall/multicast/check-runs/${{ steps.output_check_id.outputs.check_id }} \ |
| 242 | + -f "name=${{ inputs.name }}" -f "head_sha=${{ steps.output_sha.outputs.sha }}" \ |
| 243 | + -f "status=completed" -f "conclusion=${{ inputs.conclusion }}" \ |
| 244 | + -f "completed_at=${{ steps.output_date.outputs.check_date }}Z" \ |
| 245 | + -f "details_url=${{ steps.output_check_details_url.outputs.details_url }}" \ |
| 246 | + -f 'output[title]=${{ inputs.title }}' \ |
| 247 | + -f 'output[summary]=${{ inputs.summary }}' -f 'output[text]=${{ inputs.text }}' |
| 248 | + printf "%s\n" "::endgroup::" |
| 249 | + - name: "Report outcome of checks API" |
| 250 | + id: output_check_outcome |
| 251 | + if: ${{ always() && (github.repository == 'reactive-firewall/multicast') }} |
| 252 | + shell: bash |
| 253 | + run: | |
| 254 | + if [[ "${{ steps.forge_new_check.outcome }}" == "success" ]] ; then |
| 255 | + CHECK_CREATED="true" |
| 256 | + CHECK_FORGED="true" |
| 257 | + CHECK_OPERATION="forged" |
| 258 | + else |
| 259 | + if [[ "${{ steps.create_new_check.outcome }}" == "success" ]] ; then |
| 260 | + CHECK_CREATED="true" |
| 261 | + CHECK_OPERATION="created" |
| 262 | + else |
| 263 | + CHECK_CREATED="false" |
| 264 | + fi |
| 265 | + fi |
| 266 | + if [[ "${{ steps.compleate_check.outcome }}" == "success" ]] ; then |
| 267 | + CHECK_UPDATED="true" |
| 268 | + CHECK_CONCLUDED="true" |
| 269 | + CHECK_OPERATION="completed" |
| 270 | + else |
| 271 | + if [[ "${{ steps.update_check.outcome }}" == "success" ]] ; then |
| 272 | + CHECK_UPDATED="true" |
| 273 | + CHECK_OPERATION="updated" |
| 274 | + else |
| 275 | + CHECK_UPDATED="false" |
| 276 | + fi |
| 277 | + CHECK_CONCLUDED="false" |
| 278 | + fi |
| 279 | + if [[ -z ${CHECK_OPERATION} ]] ; then |
| 280 | + printf "check_outcome=success\n" >> "$GITHUB_OUTPUT" |
| 281 | + printf "check_operation=%s\n" "${CHECK_OPERATION}" >> "$GITHUB_OUTPUT" |
| 282 | + exit 0 |
| 283 | + else |
| 284 | + EXIT_CODE=1 |
| 285 | + if [[ "${{ steps.forge_new_check.outcome }}" == "failure" ]] ; then |
| 286 | + printf "check_outcome=failure\n" >> "$GITHUB_OUTPUT" |
| 287 | + CHECK_OPERATION="cancelled" |
| 288 | + else |
| 289 | + if [[ "${{ steps.create_new_check.outcome }}" == "failure" ]] ; then |
| 290 | + printf "check_outcome=failure\n" >> "$GITHUB_OUTPUT" |
| 291 | + CHECK_OPERATION="uncreated" |
| 292 | + else |
| 293 | + if [[ "${{ steps.compleate_check.outcome }}" == "failure" ]] ; then |
| 294 | + printf "check_outcome=failure\n" >> "$GITHUB_OUTPUT" |
| 295 | + CHECK_OPERATION="uncompleted" |
| 296 | + else |
| 297 | + if [[ "${{ steps.update_check.outcome }}" == "failure" ]] ; then |
| 298 | + printf "check_outcome=failure\n" >> "$GITHUB_OUTPUT" |
| 299 | + CHECK_OPERATION="unaffected" |
| 300 | + else |
| 301 | + # skipped or cancelled both result in overall skipped for us |
| 302 | + printf "check_outcome=skipped\n" >> "$GITHUB_OUTPUT" |
| 303 | + CHECK_OPERATION="unknown" |
| 304 | + # also don't fail on skip |
| 305 | + EXIT_CODE=0 |
| 306 | + fi |
| 307 | + fi |
| 308 | + fi |
| 309 | + fi |
| 310 | + printf "check_operation=%s\n" "${CHECK_OPERATION}" >> "$GITHUB_OUTPUT" |
| 311 | + exit ${EXIT_CODE} |
| 312 | + fi |
0 commit comments