Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINBEE-10041 - Handle cases where downloading artifacts fails and improve code safety #114

Merged
merged 12 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition env.SKIP_GIT_CLONE == 'false' || env.CACHE_DOWNLOAD_FAILED == 'true' repeat itself 3 times, I think env.CACHE_DOWNLOAD_FAILED == 'true' maybe be in the logic when you calculated SKIP_GIT_CLONE

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I have to calculate SKIP_GIT_CLONE before downloading artifact... I can do other step that calculate it

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