Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

Post contract size diff to parent PR #16

Merged
merged 97 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
7fc45cb
Installation has been moved to docker container
cmichi Nov 15, 2021
bc722e9
Output markdown table
cmichi Nov 15, 2021
8e51331
Experiment with comment posting
cmichi Nov 15, 2021
7ec4402
Experiment with comment posting
cmichi Nov 15, 2021
2942307
Fix `curl`
cmichi Nov 15, 2021
a8de6fb
Fix token
cmichi Nov 15, 2021
77a8945
Fix token
cmichi Nov 15, 2021
49709ae
Fix path
cmichi Nov 15, 2021
1be5be4
Fix path
cmichi Nov 15, 2021
b00898b
Fix path
cmichi Nov 15, 2021
9fa28e4
Fix body
cmichi Nov 15, 2021
5870eb8
Remove for testing unnecessary jobs
cmichi Nov 15, 2021
f20a0f2
Fix JSON
cmichi Nov 15, 2021
4108d49
Post comparison results
cmichi Nov 15, 2021
4ab0f0b
Fix `tr`
cmichi Nov 15, 2021
901175f
Install jq
cmichi Nov 15, 2021
0eca1ba
Install jq
cmichi Nov 15, 2021
8f7a247
Debug jq execution
cmichi Nov 15, 2021
be3aa64
Fix quoting issues
cmichi Nov 15, 2021
f13829c
Fix quoting issues
cmichi Nov 15, 2021
89adf32
Fix posting
cmichi Nov 15, 2021
ac65d89
Add debug output
cmichi Nov 15, 2021
93875a5
Fix indentation
cmichi Nov 15, 2021
9dec06e
Build correct uri
cmichi Nov 15, 2021
3322dd0
Fix filename
cmichi Nov 15, 2021
3b73fe5
Fix var
cmichi Nov 15, 2021
e272581
Debug url
cmichi Nov 15, 2021
01735e6
Fix url
cmichi Nov 15, 2021
c506b5d
Fix url
cmichi Nov 15, 2021
e9e1396
Fix url
cmichi Nov 15, 2021
6d1d457
Use raw
cmichi Nov 15, 2021
2480e3d
Update verb
cmichi Nov 15, 2021
b9f0fb6
Improve comment
cmichi Nov 15, 2021
2ce86b0
Improve comment
cmichi Nov 15, 2021
f0781bb
Improve comment
cmichi Nov 15, 2021
3a34b4b
Prettify comment
cmichi Nov 15, 2021
34606df
Improve readability
cmichi Nov 15, 2021
6500284
Improve readability
cmichi Nov 15, 2021
ea7f3f0
Improve readability
cmichi Nov 15, 2021
10e3b60
Fix syntax
cmichi Nov 15, 2021
aa98892
Fix syntax
cmichi Nov 15, 2021
80de5ec
Fix syntax
cmichi Nov 15, 2021
bc1e9aa
Fix syntax
cmichi Nov 15, 2021
49b6c03
Fix md
cmichi Nov 15, 2021
011494d
Fix md
cmichi Nov 15, 2021
cd35543
Output total size as well
cmichi Nov 15, 2021
995609e
Update text
cmichi Nov 15, 2021
c4aff3a
Update command
cmichi Nov 15, 2021
db95af9
Update command
cmichi Nov 15, 2021
e5282ac
Fix syntax
cmichi Nov 15, 2021
dc9d178
Fix syntax
cmichi Nov 15, 2021
77f39df
Fix syntax
cmichi Nov 15, 2021
f763dfc
Fix syntax
cmichi Nov 15, 2021
179fa9f
Fix syntax
cmichi Nov 15, 2021
e4cac50
Fix syntax
cmichi Nov 15, 2021
3a9352f
Fix syntax
cmichi Nov 15, 2021
fa0612f
Prettify output
cmichi Nov 15, 2021
6a330bd
Sort everything
cmichi Nov 15, 2021
7868764
Fix typos and grammar
cmichi Nov 15, 2021
1a8e135
Prettify code and remove fixed `TRGR_REF`
cmichi Nov 15, 2021
d610022
Revert "Remove for testing unnecessary jobs"
cmichi Nov 15, 2021
f30c673
Improve script
cmichi Nov 15, 2021
0a9093e
Only compare if necessary
cmichi Nov 15, 2021
9fc6ada
Fix grammar
cmichi Nov 15, 2021
f474d66
Improve script readability
cmichi Nov 15, 2021
a1391ed
Revert me: Temporarily disable tests
cmichi Nov 15, 2021
4295120
Fix syntax
cmichi Nov 15, 2021
bd1a27c
Fix valid variant
cmichi Nov 15, 2021
c6fe6a5
Fix valid variant
cmichi Nov 15, 2021
6f842e7
Revert "Revert me: Temporarily disable tests"
cmichi Nov 15, 2021
ab6a123
Ensure unique entries
cmichi Nov 15, 2021
b1ffe78
Format numbers
cmichi Nov 15, 2021
b25ae4f
Remove xargs
cmichi Nov 15, 2021
49aaf79
Use awk
cmichi Nov 15, 2021
2a06a7e
Fix parsing
cmichi Nov 15, 2021
ffc0954
Fix syntax
cmichi Nov 15, 2021
b2665ff
Fix syntax
cmichi Nov 15, 2021
98148ef
Format numbers
cmichi Nov 15, 2021
f3ab434
Strip out no-changes
cmichi Nov 15, 2021
75970d0
Improve displaying
cmichi Nov 15, 2021
febb11a
Improve displaying
cmichi Nov 15, 2021
35308c9
Rename job
cmichi Nov 15, 2021
aefc56a
Move logic to script
cmichi Nov 15, 2021
48f1fdb
Fix order
cmichi Nov 15, 2021
becf1eb
Handle multi-lines
cmichi Nov 15, 2021
d52a8f6
Handle indentation correctly
cmichi Nov 15, 2021
d23fc9a
Improve text
cmichi Nov 15, 2021
780c17c
Improve text
cmichi Nov 15, 2021
3f68d7a
Fix text
cmichi Nov 16, 2021
b3047ba
Improve script
cmichi Nov 16, 2021
63d0118
Fix url
cmichi Nov 16, 2021
04540ac
Update .scripts/evaluate-examples-size-changes.sh
Nov 16, 2021
ed6f0a5
Update .scripts/evaluate-examples-size-changes.sh
Nov 16, 2021
39eb682
Update .scripts/evaluate-examples-size-changes.sh
Nov 16, 2021
14b7475
Update .scripts/evaluate-examples-size-changes.sh
Nov 16, 2021
484816a
Improve naming
cmichi Nov 16, 2021
1edd192
Add comment
cmichi Nov 16, 2021
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
54 changes: 27 additions & 27 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ workflow:
REDISCLI_AUTH:
vault: cicd/gitlab/parity/REDIS_AUTH@kv
file: false
GITHUB_TOKEN:
vault: cicd/gitlab/parity/ink/GITHUB_TOKEN@kv
file: false


.default-refs: &default-refs
Expand Down Expand Up @@ -77,12 +80,12 @@ workflow:
- kubernetes-parity-build


.if-trigger-ref-is-master: &if-trigger-ref-is-master
.if-trigger-ref-valid: &if-trigger-ref-valid
before_script:
- if [ "$TRGR_REF" == "master" ]; then
echo "It makes no sense to compare $REDIS_SIZES_KEY to $REDIS_SIZES_KEY_MASTER."
echo "Exiting gracefully."
exit 0
- if [ "$TRGR_REF" == "master" ] || [ -z "$TRGR_REF" ]; then
echo "It makes no sense to compare $REDIS_SIZES_KEY to $REDIS_SIZES_KEY_MASTER.";
echo "Exiting gracefully.";
exit 0;
fi

.clone-repo: &clone-repo
Expand All @@ -101,7 +104,7 @@ workflow:
- pkill -f "substrate-contracts-node-rand-extension --tmp --dev"


# needed vars have to be "exported" on a earlier stage
# Needed vars have to be "exported" in an earlier stage
parent-vars:
stage: declare-vars
<<: *kubernetes-env
Expand Down Expand Up @@ -131,12 +134,12 @@ parent-vars:
- echo "UPSTREAM_REPO_NAME=${UPSTREAM_REPO_NAME}" | tee -a parent-vars.env

# REDIS_SIZES_KEY (e.g. ink-waterfall::ink::foo-add-feature::sizes)
# defines a Redis key name where contract sizes will be stored frpm a upstrem above
# defines a Redis key name where contract sizes will be stored from an upstream above.
- echo "REDIS_SIZES_KEY=${CI_PROJECT_NAME}::${UPSTREAM_REPO_NAME}::${UPSTREAM_BRANCH_REDIS_KEY}::sizes" | tee -a parent-vars.env

# REDIS_SIZES_KEY_MASTER (e.g. ink-waterfall::ink::master::sizes)
# defines a Redis key name for a upstream's master reference branch. contracts sizes stored there will be used for
# a comparison with contract sizes stored in REDIS_SIZES_KEY
# defines a Redis key name for an upstream's master reference branch.
# contract sizes stored there will be used for a comparison with contract sizes stored in REDIS_SIZES_KEY.
- echo "REDIS_SIZES_KEY_MASTER=${CI_PROJECT_NAME}::${UPSTREAM_REPO_NAME}::master::sizes" | tee -a parent-vars.env
artifacts:
reports:
Expand Down Expand Up @@ -174,24 +177,6 @@ polkadot-js-ui-ink-examples: &polkadot-js-ui-ink-examples
dependencies:
- parent-vars

polkadot-js-ui-ink-examples-compare:
stage: compare-build
<<: *kubernetes-env
<<: *vault-secrets
# Comparison is made only if a parent (trigger) was created on PRs. Otherwise we would
# would be comparing `master` with `master`.
#
# TODO: While we lack comparison data for master, lets "compare" everything
# <<: *if-trigger-ref-is-master
script:
- echo "$REDIS_SIZES_KEY will be compared to $REDIS_SIZES_KEY_MASTER"
# Deserialize comparison data
- redis-cli -u $GITLAB_REDIS_URI --raw lrange $REDIS_SIZES_KEY 0 -1 | tee $REDIS_SIZES_KEY.csv
- redis-cli -u $GITLAB_REDIS_URI --raw lrange $REDIS_SIZES_KEY_MASTER 0 -1 | tee $REDIS_SIZES_KEY_MASTER.csv
- cargo install --git https://github.com/HCastano/csv-comparator
# TODO [#4]: Post the results of this script on GitHub
- csv-comparator $REDIS_SIZES_KEY_MASTER.csv $REDIS_SIZES_KEY.csv | tee contract-size-diff.csv


polkadot-js-ui-rand-extension:
stage: test
Expand All @@ -204,6 +189,21 @@ polkadot-js-ui-rand-extension:
- *shutdown-substrate-contracts-node-rand-extension


evaluate-ink-examples-size-changes:
stage: compare-build
<<: *kubernetes-env
<<: *vault-secrets
# Comparison is made only if a parent (trigger) was created by a PR.
# Otherwise we would be comparing `master` with `master`.
<<: *if-trigger-ref-valid
script:
# Deserialize comparison data
- redis-cli -u $GITLAB_REDIS_URI --raw lrange $REDIS_SIZES_KEY 0 -1 | sort | tee $REDIS_SIZES_KEY.csv
- redis-cli -u $GITLAB_REDIS_URI --raw lrange $REDIS_SIZES_KEY_MASTER 0 -1 | sort | tee $REDIS_SIZES_KEY_MASTER.csv
- PR_COMMENTS_URL=https://api.github.com/repos/paritytech/ink/issues/${TRGR_REF}/comments
- ./.scripts/evaluate-examples-size-changes.sh $REDIS_SIZES_KEY_MASTER.csv $REDIS_SIZES_KEY.csv $PR_COMMENTS_URL


build_badge:
stage: compare-build
rules:
Expand Down
77 changes: 77 additions & 0 deletions .scripts/evaluate-examples-size-changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Evaluates size changes of the ink! examples and posts the results
# as a comment on GitHub.
#
# Usage:
# ./evaluate-examples-size-changes.sh \
# <path_to_baseline.csv> <path_to_size_change.csv> \
# <github_url_to_comments_of_pr>

set -eux

BASELINE_FILE=$1
COMPARISON_FILE=$2
PR_COMMENTS_URL=$3

echo "$BASELINE_FILE will be compared to $COMPARISON_FILE"

csv-comparator $BASELINE_FILE $COMPARISON_FILE | \
sort | \
awk -F"," '{printf "`%s`,%.2f K,%.2f K\n", $1, $2, $3}' | \
# prepend a plus in front of all positive numbers
sed --regexp-extended 's/^([0-9])/,+\1/g' | \
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this doing? My regex brain is too smol

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prepends a plus in front of positive numbers. Yeah we could definitely move all of this into csv-comparator. I added a comment as well.

sed --regexp-extended 's/,([0-9])/,+\1/g' | \
tee pure-contract-size-diff.csv

# Append the original optimized size (i.e. not the delta) to the end of each line
cat $COMPARISON_FILE | \
sort | uniq | \
egrep --only-matching ', [0-9]+\.[0-9]+$' | \
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if maybe we should install ripgrep in our CI Docker image, might make some of this stuff more readable

awk -F", " '{printf ",%.2f K\n", $2}' | \
tee total-optimized-size.csv

paste -d "" pure-contract-size-diff.csv total-optimized-size.csv | tee combined.csv

echo " ,Δ Original Size,Δ Optimized Size,Total Optimized Size" | tee contract-size-diff.csv
cat combined.csv | sed 's/+0.00 K//g' | tee --append contract-size-diff.csv
csv2md --pretty < contract-size-diff.csv | tee contract-size-diff.md

# Replace `\n` so that it works propely when submitted to the GitHub API.
# Also align the table text right.
cat contract-size-diff.md | \
sed 's/---|/---:|/g' | \
sed --regexp-extended 's/(-+)\:/:\1/' | \
sed 's/$/\\n/g' | \
tr -d '\n' | \
tee contract-size-diff-newlines.md
COMMENT=$(cat contract-size-diff-newlines.md)

# If there is already a comment by the user `paritytech-ci` in the ink! PR which triggered
# this run, then we can just edit this comment (using `PATCH` instead of `POST`).
POSSIBLY_COMMENT_URL=$(curl --silent $PR_COMMENTS_URL | \
jq -r ".[] | select(.user.login == \"paritytech-ci\") | .url" | \
head -n1
)
echo $POSSIBLY_COMMENT_URL

VERB="POST"
if [ ! -z "$POSSIBLY_COMMENT_URL" ]; then
VERB="PATCH";
PR_COMMENTS_URL="$POSSIBLY_COMMENT_URL"
fi

echo $VERB
echo $PR_COMMENTS_URL

UPDATED=$(TZ='Europe/Berlin' date)
CC_VERSION=$(cargo-contract --version | egrep --only-matching "cargo-contract [^-]*")
curl -X ${VERB} ${PR_COMMENTS_URL} \
-H "Cookie: logged_in=no" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json; charset=utf-8" \
-d $"{ \
\"body\": \"## 🦑 📈 ink! Example Contracts ‒ Size Change Report 📉 🦑\\n \
These are the results of building the \`examples/*\` contracts from this branch with \`$CC_VERSION\`. \\n\\n\
${COMMENT}\n\n[Link to the run](https://gitlab.parity.io/parity/ink-waterfall/-/pipelines/${CI_PIPELINE_ID}) | Last update: ${UPDATED}\" \
}"