Skip to content

Commit

Permalink
LINBEE-10041 - Handle cases where downloading artifacts fails and imp…
Browse files Browse the repository at this point in the history
…rove code safety (#114)

* LINBEE-10041 - fix case when download artifact fails

* fix format

* fix format

* fix var name

* debug

* use env.CACHE_DOWNLOAD_FAILED

* convert setFailed to warning

* remove debugs

* core debug

* fix condition

* revert debug core

---------

Co-authored-by: Yeela Lifshitz <[email protected]>
  • Loading branch information
yeelali14 and Yeela Lifshitz committed Aug 19, 2024
1 parent ae17b8b commit 9bb982e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 14 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,32 @@ runs:
env:
IS_NON_COMMIT_ARG: ${{ fromJSON(fromJSON(inputs.client_payload)).isNonCommitEvent }}
ENABLE_CACHE_ARG: ${{ env.ENABLE_CACHE }}
RUN_ID_ARG: ${{ fromJSON(fromJSON(inputs.client_payload)).artifactRunId }}
with:
script: |
require('${{ github.action_path }}/scripts/get-condition-vars.js')(core);
- name: Download cache artifact
id: download-cache
uses: actions/download-artifact@v4
if: ${{ env.SKIP_GIT_CLONE == 'true' }}
continue-on-error: true
with:
github-token: ${{ fromJSON(fromJSON(inputs.client_payload)).githubToken || github.token }}
repository: ${{ inputs.full_repository }}
run-id: ${{ fromJSON(fromJSON(inputs.client_payload)).artifactRunId }}
name: output
path: code/output

- name: Check if download cache artifact failed
uses: actions/github-script@v7
env:
ARTIFACT_OUTCOME_ARG: ${{ steps.download-cache.outcome }}
with:
script: require('${{ github.action_path }}/scripts/check-cache-download-status')(core);

- name: Checkout Pull Request branches history
if: ${{ env.SKIP_GIT_CLONE == 'false' }}
if: ${{ env.SKIP_GIT_CLONE == 'false' || env.CACHE_DOWNLOAD_FAILED == 'true' }}
shell: bash
run: |
ALL=2147483647
Expand All @@ -116,13 +126,13 @@ runs:
git checkout $'${{ steps.safe-strings.outputs.head_ref }}'
- name: Create cm folder
if: ${{ env.SKIP_GIT_CLONE == 'false' }}
if: ${{ env.SKIP_GIT_CLONE == 'false' || env.CACHE_DOWNLOAD_FAILED == 'true' }}
shell: bash
run: cd gitstream && mkdir cm

- name: Checkout cm repo
uses: actions/checkout@v4
if: ${{ fromJSON(fromJSON(inputs.client_payload)).hasCmRepo == true && env.SKIP_GIT_CLONE == 'false' }}
if: ${{ fromJSON(fromJSON(inputs.client_payload)).hasCmRepo == true && (env.SKIP_GIT_CLONE == 'false' || env.CACHE_DOWNLOAD_FAILED == 'true')}}
with:
repository: '${{ fromJSON(fromJSON(inputs.client_payload)).owner }}/${{ fromJSON(fromJSON(inputs.client_payload)).cmRepo }}'
ref: ${{ fromJSON(fromJSON(inputs.client_payload)).cmRepoRef }}
Expand All @@ -146,7 +156,7 @@ runs:
RULES_RESOLVER_URL: ${{ inputs.resolver_url }}
RULES_RESOLVER_TOKEN: ${{ inputs.resolver_token }}
DEBUG_MODE: ${{ inputs.debug_mode }}
IS_NON_COMMIT_EVENT: ${{ env.IS_NON_COMMIT_EVENT }}
SHOULD_SKIP_CLONE: ${{ env.SKIP_GIT_CLONE == 'true' && env.CACHE_DOWNLOAD_FAILED == 'false'}}
ENABLE_CACHE: ${{ env.ENABLE_CACHE }}
ENABLE_DEBUG_ARTIFACTS: ${{ env.ENABLE_DEBUG_ARTIFACTS }}

Expand Down
17 changes: 17 additions & 0 deletions scripts/check-cache-download-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable import/no-commonjs */

module.exports = core => {
try {
const { ARTIFACT_OUTCOME_ARG } = process.env
core.info(`Setting cache download status to: ${ARTIFACT_OUTCOME_ARG}`)

if (ARTIFACT_OUTCOME_ARG === 'failure') {
core.exportVariable('CACHE_DOWNLOAD_FAILED', 'true')
} else {
core.exportVariable('CACHE_DOWNLOAD_FAILED', 'false')
}
} catch (error) {
core.warn(`Failed to set cache download status: ${error.message}`)
core.exportVariable('CACHE_DOWNLOAD_FAILED', 'true')
}
}
9 changes: 6 additions & 3 deletions scripts/get-condition-vars.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* eslint-disable import/no-commonjs */

module.exports = core => {
const { IS_NON_COMMIT_ARG, ENABLE_CACHE_ARG } = process.env
const { IS_NON_COMMIT_ARG, ENABLE_CACHE_ARG, RUN_ID_ARG } = process.env
try {
const isRunIdExists = !!RUN_ID_ARG
const skipGitClone =
IS_NON_COMMIT_ARG === 'true' && ENABLE_CACHE_ARG === 'true'
IS_NON_COMMIT_ARG === 'true' &&
ENABLE_CACHE_ARG === 'true' &&
isRunIdExists

core.exportVariable('IS_NON_COMMIT_EVENT', IS_NON_COMMIT_ARG)
core.exportVariable('SKIP_GIT_CLONE', skipGitClone.toString())
} catch (error) {
core.setFailed(error.message)
core.warn(error.message)

core.exportVariable('IS_NON_COMMIT_EVENT', 'false')
core.exportVariable('SKIP_GIT_CLONE', 'false')
Expand Down

0 comments on commit 9bb982e

Please sign in to comment.