Skip to content

Commit d730aeb

Browse files
authored
Merge pull request #71 from paritytech/mak/111-update-substrate-template-2
Mak/111 update substrate template 2
2 parents 892d12e + b5d038f commit d730aeb

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

update_substrate_template.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
# This is an automation for updating Substrate Node Template
4+
# https://github.com/substrate-developer-hub/substrate-node-template
5+
# Triggered on push of new branch on polkadot release (i.e polkadot-v0.9.26) on substrate repo
6+
#
7+
# TODO: If any step fails => send a notification to some channel
8+
#
9+
# Usage:
10+
# Update Cumulus -> Substrate Parachain Template
11+
# ./pipeline-scripts/update_substrate_template.sh
12+
# --repo-name substrate-parachain-template
13+
# --template-path "parachain-template"
14+
# --github-api-token "$GITHUB_TOKEN"
15+
# --polkadot-branch "$CI_COMMIT_REF_NAME"
16+
#
17+
# Update Substrate -> Substrate Node Template
18+
# ./pipeline-scripts/update_substrate_template.sh
19+
# --repo-name substrate-node-template
20+
# --template-path "bin/node-template"
21+
# --github-api-token "$GITHUB_TOKEN"
22+
# --polkadot-branch "$CI_COMMIT_REF_NAME"
23+
24+
set -eu -o pipefail
25+
26+
. "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
27+
28+
# substrate-parachain-template or substrate-node-template
29+
get_arg required --repo-name "$@"
30+
target_repo_name="$out"
31+
32+
# "bin/node-template" or "parachain-template"
33+
get_arg required --template-path "$@"
34+
template_path="$out"
35+
36+
# GITHUB_TOKEN or other
37+
get_arg required --github-api-token "$@"
38+
github_api_token="$out"
39+
40+
# $CI_COMMIT_REF_NAME
41+
get_arg required --polkadot-branch "$@"
42+
polkadot_branch="$out"
43+
44+
target_org="substrate-developer-hub"
45+
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
46+
default_branch=main
47+
working_directory="$PWD"
48+
source_template_path="$working_directory/$template_path"
49+
dest_template_path="$working_directory/$target_repo_name"
50+
template_branch="autoupdate_${polkadot_branch}_${timestamp}"
51+
repo_url_with_token="https://$github_api_token@github.com/$target_org/$target_repo_name.git"
52+
53+
echo "working_directory - $working_directory"
54+
echo "source_template_path - $source_template_path"
55+
echo "dest_template_path - $dest_template_path"
56+
echo "template_branch - $template_branch"
57+
58+
# Step 1) clone substrate-node-template repo as destination
59+
echo "Cleaning existing $dest_template_path"
60+
rm -rf "$dest_template_path"
61+
git clone "$repo_url_with_token" "$dest_template_path"
62+
63+
# Step 2) copy files from ./bin/node-template to substrate-*-template
64+
echo "Copying fresh template $source_template_path to $dest_template_path"
65+
cp -a "$source_template_path/." "$dest_template_path"
66+
67+
# Step 3) replace references to pallets from local (relative) to remote (git + branch)
68+
echo "Change directory to $dest_template_path"
69+
cd "$dest_template_path"
70+
71+
# stings like 'path = "../../../../frame/system"'
72+
# but avoid "local" dependencies (from parent folder), like 'path = "../runtime"'
73+
query_pattern='path = "\.\.\/\.[^"]*"'
74+
# should be replaced with 'git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26"'
75+
replace_ref="git = \"https:\/\/github.com\/paritytech\/substrate.git\", branch = \"${polkadot_branch}\""
76+
77+
# find all .toml files and replace relative paths to github repo with polkadot branch
78+
echo "Replacing <$query_pattern> with <$replace_ref> in .toml files"
79+
find . -type f -name '*.toml'
80+
find . -type f -name '*.toml' -exec sed -i -E "s/$query_pattern/$replace_ref/g" {} \;
81+
82+
# update substrate deps which may point to master to $polkadot_branch (could be in cumulus)
83+
diener update --substrate --branch "$polkadot_branch"
84+
85+
# Step 4)
86+
echo "Run <cargo update> to update replaced references in root Cargo.lock"
87+
cargo update
88+
89+
# Step 5)
90+
echo "Run <cargo check> to verify build"
91+
cargo check
92+
93+
# Step 6) commit & push to https://github.com/substrate-developer-hub/substrate-*-template
94+
git config --global user.name substrate-developer-hub
95+
git config --global user.email "[email protected]"
96+
git remote -v
97+
git checkout -b "$template_branch"
98+
git add .
99+
commit_message="Auto-Update node-template from $polkadot_branch"
100+
git commit -m "$commit_message"
101+
git push origin HEAD
102+
103+
# Step 7) create a pull-request
104+
curl \
105+
-X POST \
106+
-H "Accept: application/vnd.github+json" \
107+
-H "Authorization: token $github_api_token" \
108+
https://api.github.com/repos/$target_org/$target_repo_name/pulls \
109+
-d '{"title":"'"$commit_message"'","body":"'"$commit_message"'","head":"'"$template_branch"'","base":"'"$default_branch"'"}'

0 commit comments

Comments
 (0)