Skip to content

Commit

Permalink
chore: download did script (#642)
Browse files Browse the repository at this point in the history
# Motivation

In #643, we need to download another did file from a repository other
than the IC repository. Therefore, we have to use a one more curl
command in our `import-candid` script. This PR creates a function for
handling such did files in import-candid, and also uses the GitHub API
to collect the commit hash and add it as a comment in the generated
file.

---------

Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker authored Jun 3, 2024
1 parent 50904a9 commit e1b961c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/ic-management/candid/ic-management.did
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Generated from dfinity/interface-spec commit 17ae77a0a25781545af9533f4e2ff569f52d1eb4 for file 'spec/_attachments/ic.did'
type canister_id = principal;
type wasm_module = blob;

Expand Down
30 changes: 29 additions & 1 deletion scripts/import-candid
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ import_did() {
} >"${out_path}"
}

download_did() {
local raw_url="$1"
local out_filename="$2"
local pkg="$3"

local out_path="packages/${pkg}/candid/${out_filename}"

# Extract repository, branch, and file path from the raw URL
local repo=$(echo "$raw_url" | awk -F '/' '{print $4"/"$5}')
local branch=$(echo "$raw_url" | awk -F '/' '{print $6}')
local file_path=$(echo "$raw_url" | awk -F "$branch/" '{print $2}')

# Get the latest commit hash for the specified file
local api_url="https://api.github.com/repos/${repo}/commits?path=${file_path}&sha=${branch}"
local commit_hash=$(curl -s "$api_url" | jq -r '.[0].sha')

if [ -z "$commit_hash" ]; then
echo "Failed to retrieve commit hash for ${file_path} in ${repo}."
return 1
fi

echo "Downloading ${raw_url} -> REPO_ROOT/${out_path}"
{
echo "// Generated from ${repo} commit ${commit_hash} for file '${file_path}'"
curl -s "$raw_url"
} >"${out_path}"
}

: Move to root of the repo
cd "$(dirname "$(realpath "$0")")/.."

Expand Down Expand Up @@ -88,5 +116,5 @@ import_did "rs/ethereum/cketh/minter/cketh_minter.did" "minter.did" "cketh"
import_did "rs/ethereum/ledger-suite-orchestrator/ledger_suite_orchestrator.did" "orchestrator.did" "cketh"

mkdir -p packages/ic-management/candid
curl https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did -o packages/ic-management/candid/ic-management.did
download_did https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did "ic-management.did" "ic-management"
: Fin

0 comments on commit e1b961c

Please sign in to comment.