Repository navigation
feat(MMQA-1951): add new action to download segment analytics contracts #296
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| name: Download analytics contract | ||
| description: > | ||
| Resolve and download a deployed analytics contract release using a short-lived OIDC token. | ||
| The calling workflow must grant id-token: write. | ||
|
|
||
| inputs: | ||
| token-exchange-url: | ||
| description: URL used to exchange the GitHub OIDC token. | ||
| required: true | ||
| target-repository: | ||
| description: Private repository containing the analytics contract releases. | ||
| required: true | ||
| asset-name: | ||
| description: Platform contract asset to download. | ||
| required: true | ||
| release-tag: | ||
| description: Exact analytics contract release tag. Empty selects the newest published release. | ||
| required: false | ||
| default: '' | ||
| output-directory: | ||
| description: Directory where manifest.json and the platform contract are written. | ||
| required: true | ||
|
|
||
| outputs: | ||
| release-tag: | ||
| description: Selected analytics contract release tag. | ||
| value: ${{ steps.resolve-release.outputs.release-tag }} | ||
| release-url: | ||
| description: Selected analytics contract release URL. | ||
| value: ${{ steps.resolve-release.outputs.release-url }} | ||
| asset-digest: | ||
| description: SHA-256 digest advertised for the downloaded asset. | ||
| value: ${{ steps.resolve-release.outputs.asset-digest }} | ||
| manifest-path: | ||
| description: Absolute path to the downloaded manifest. | ||
| value: ${{ steps.download-release.outputs.manifest-path }} | ||
| asset-path: | ||
| description: Absolute path to the downloaded platform asset. | ||
| value: ${{ steps.download-release.outputs.asset-path }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Get read token | ||
| id: contract-token | ||
| uses: MetaMask/github-tools/.github/actions/get-token@v1 | ||
| with: | ||
| token-exchange-url: ${{ inputs.token-exchange-url }} | ||
| target-repository: ${{ inputs.target-repository }} | ||
| permissions: | | ||
| contents: read | ||
|
|
||
| - name: Resolve release | ||
| id: resolve-release | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ steps.contract-token.outputs.token }} | ||
| TARGET_REPOSITORY: ${{ inputs.target-repository }} | ||
| REQUESTED_TAG: ${{ inputs.release-tag }} | ||
| ASSET_NAME: ${{ inputs.asset-name }} | ||
| run: | | ||
| set -euo pipefail | ||
|
|
||
| if [ -n "${REQUESTED_TAG}" ]; then | ||
| RELEASE="$( | ||
| gh api \ | ||
| "repos/${TARGET_REPOSITORY}/releases/tags/${REQUESTED_TAG}" | ||
| )" | ||
| else | ||
| RELEASE_PAGES="$( | ||
| gh api \ | ||
| --paginate \ | ||
| --slurp \ | ||
| "repos/${TARGET_REPOSITORY}/releases?per_page=100" | ||
| )" | ||
| RELEASE="$( | ||
| jq ' | ||
| [ | ||
| .[][] | ||
| | select(.draft == false) | ||
| | select(.prerelease == false) | ||
| | select(.tag_name | startswith("analytics-contracts-")) | ||
| ] | ||
| | sort_by(.published_at) | ||
| | last | ||
| ' <<< "${RELEASE_PAGES}" | ||
| )" | ||
| fi | ||
|
|
||
| TAG="$(jq -r '.tag_name // empty' <<< "${RELEASE}")" | ||
| RELEASE_URL="$(jq -r '.html_url // empty' <<< "${RELEASE}")" | ||
| DRAFT="$(jq -r '(.draft == true)' <<< "${RELEASE}")" | ||
| PRERELEASE="$(jq -r '(.prerelease == true)' <<< "${RELEASE}")" | ||
| ASSET_DIGEST="$( | ||
| jq -r --arg asset "${ASSET_NAME}" ' | ||
| .assets[] | ||
| | select(.name == $asset) | ||
| | .digest // empty | ||
| ' <<< "${RELEASE}" | ||
| )" | ||
|
|
||
| if [ -z "${TAG}" ] || [[ "${TAG}" != analytics-contracts-* ]]; then | ||
| echo "No matching analytics contract release was found." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ "${DRAFT}" = "true" ] || [ "${PRERELEASE}" = "true" ]; then | ||
| echo "Release ${TAG} is not published." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if ! [[ "${ASSET_DIGEST}" =~ ^sha256:[0-9a-f]{64}$ ]]; then | ||
| echo "Release ${TAG} has no valid ${ASSET_NAME} SHA-256 digest." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| { | ||
| echo "release-tag=${TAG}" | ||
| echo "release-url=${RELEASE_URL}" | ||
| echo "asset-digest=${ASSET_DIGEST}" | ||
| } >> "${GITHUB_OUTPUT}" | ||
|
|
||
| - name: Download release | ||
| id: download-release | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ steps.contract-token.outputs.token }} | ||
| TARGET_REPOSITORY: ${{ inputs.target-repository }} | ||
| RELEASE_TAG: ${{ steps.resolve-release.outputs.release-tag }} | ||
| ASSET_NAME: ${{ inputs.asset-name }} | ||
| OUTPUT_DIRECTORY: ${{ inputs.output-directory }} | ||
| EXPECTED_DIGEST: ${{ steps.resolve-release.outputs.asset-digest }} | ||
| run: | | ||
| set -euo pipefail | ||
| if [[ "${OUTPUT_DIRECTORY}" != /* ]]; then | ||
| OUTPUT_DIRECTORY="${GITHUB_WORKSPACE}/${OUTPUT_DIRECTORY}" | ||
| fi | ||
| mkdir -p "${OUTPUT_DIRECTORY}" | ||
| OUTPUT_DIRECTORY="$(cd "${OUTPUT_DIRECTORY}" && pwd)" | ||
|
|
||
| MANIFEST_PATH="${OUTPUT_DIRECTORY}/manifest.json" | ||
| ASSET_PATH="${OUTPUT_DIRECTORY}/${ASSET_NAME}" | ||
| rm -f -- "${MANIFEST_PATH}" "${ASSET_PATH}" | ||
|
|
||
| gh release download "${RELEASE_TAG}" \ | ||
| --repo "${TARGET_REPOSITORY}" \ | ||
| --pattern manifest.json \ | ||
| --pattern "${ASSET_NAME}" \ | ||
| --dir "${OUTPUT_DIRECTORY}" \ | ||
| --clobber | ||
|
|
||
| if [ ! -f "${MANIFEST_PATH}" ] || [ ! -f "${ASSET_PATH}" ]; then | ||
| echo "Release did not contain manifest.json and ${ASSET_NAME}." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| ACTUAL_DIGEST="$( | ||
| sha256sum "${ASSET_PATH}" | awk '{ print "sha256:" $1 }' | ||
| )" | ||
|
|
||
| if [ "${ACTUAL_DIGEST}" != "${EXPECTED_DIGEST}" ]; then | ||
| echo "Downloaded ${ASSET_NAME} digest does not match the release digest." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| { | ||
| echo "manifest-path=${MANIFEST_PATH}" | ||
| echo "asset-path=${ASSET_PATH}" | ||
| } >> "${GITHUB_OUTPUT}" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.