From 8395a1a75b7af6b162f8a4620995d7350dea5fca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Paulo=20Silva=20de=20Souza?=
 <77391175+joao-paulo-parity@users.noreply.github.com>
Date: Fri, 21 Oct 2022 05:01:09 -0300
Subject: [PATCH] Add link to the weight comparison tool for weights PRs
 (#1788)

* add link to the weight comparison tool for weights PRs

* Update scripts/create-benchmark-pr.sh

Co-authored-by: Chevdor <chevdor@users.noreply.github.com>

* add backport checklist item

* move to scripts/ci

Co-authored-by: Chevdor <chevdor@users.noreply.github.com>
---
 cumulus/scripts/ci/create-benchmark-pr.sh     | 53 +++++++++++++++++++
 .../scripts/ci/gitlab/pipeline/benchmarks.yml | 10 +---
 2 files changed, 55 insertions(+), 8 deletions(-)
 create mode 100755 cumulus/scripts/ci/create-benchmark-pr.sh

diff --git a/cumulus/scripts/ci/create-benchmark-pr.sh b/cumulus/scripts/ci/create-benchmark-pr.sh
new file mode 100755
index 00000000000..46927f24b80
--- /dev/null
+++ b/cumulus/scripts/ci/create-benchmark-pr.sh
@@ -0,0 +1,53 @@
+#!/usr/bin/env bash
+
+set -Eeu -o pipefail
+shopt -s inherit_errexit
+
+PR_TITLE="$1"
+HEAD_REF="$2"
+
+ORG="paritytech"
+REPO="$CI_PROJECT_NAME"
+BASE_REF="$CI_COMMIT_BRANCH"
+# Change threshold in %. Bigger values excludes the small changes.
+THRESHOLD=${THRESHOLD:-30}
+
+WEIGHTS_COMPARISON_URL_PARTS=(
+  "https://weights.tasty.limo/compare?"
+  "repo=$REPO&"
+  "threshold=$THRESHOLD&"
+  "path_pattern=**%2Fweights%2F*.rs&"
+  "method=guess-worst&"
+  "ignore_errors=true&"
+  "unit=time&"
+  "old=$BASE_REF&"
+  "new=$HEAD_REF"
+)
+printf -v WEIGHTS_COMPARISON_URL %s "${WEIGHTS_COMPARISON_URL_PARTS[@]}"
+
+PAYLOAD="$(jq -n \
+  --arg title "$PR_TITLE" \
+  --arg body "
+This PR is generated automatically by CI.
+
+Compare the weights with \`$BASE_REF\`: $WEIGHTS_COMPARISON_URL
+
+- [ ] Backport to master and node release branch once merged
+" \
+  --arg base "$BASE_REF" \
+  --arg head "$HEAD_REF" \
+  '{
+      title: $title,
+      body: $body,
+      head: $head,
+      base: $base
+   }'
+)"
+
+echo "PAYLOAD: $PAYLOAD"
+
+curl \
+  -H "Authorization: token $GITHUB_TOKEN" \
+  -X POST \
+  -d "$PAYLOAD" \
+  "https://api.github.com/repos/$ORG/$REPO/pulls"
diff --git a/cumulus/scripts/ci/gitlab/pipeline/benchmarks.yml b/cumulus/scripts/ci/gitlab/pipeline/benchmarks.yml
index 03aedd03960..09a0d7d2152 100644
--- a/cumulus/scripts/ci/gitlab/pipeline/benchmarks.yml
+++ b/cumulus/scripts/ci/gitlab/pipeline/benchmarks.yml
@@ -28,10 +28,7 @@ benchmarks-assets:
     - export CURRENT_TIME=$(date '+%s')
     - export BRANCHNAME="weights-statemint-${CI_COMMIT_BRANCH}-${CURRENT_TIME}"
     - !reference [.git-commit-push, script]
-    # create PR to release-parachains-v* branch
-    - curl -u ${GITHUB_USER}:${GITHUB_TOKEN}
-      -d '{"title":"[benchmarks] Update weights for statemine/t","body":"This PR is generated automatically by CI. (Once merged please backport to master and node release branch.)","head":"'${BRANCHNAME}'","base":"'${CI_COMMIT_BRANCH}'"}'
-      -X POST https://api.github.com/repos/paritytech/${CI_PROJECT_NAME}/pulls
+    - ./scripts/ci/create-benchmark-pr.sh "[benchmarks] Update weights for statemine/t" "$BRANCHNAME"
   after_script:
     - rm -rf .git/config
   tags:
@@ -50,10 +47,7 @@ benchmarks-collectives:
     - export CURRENT_TIME=$(date '+%s')
     - export BRANCHNAME="weights-collectives-${CI_COMMIT_BRANCH}-${CURRENT_TIME}"
     - !reference [.git-commit-push, script]
-    # create PR
-    - curl -u ${GITHUB_USER}:${GITHUB_TOKEN}
-      -d '{"title":"[benchmarks] Update weights for collectives","body":"This PR is generated automatically by CI.","head":"'$BRANCHNAME'","base":"'${CI_COMMIT_BRANCH}'"}'
-      -X POST https://api.github.com/repos/paritytech/${CI_PROJECT_NAME}/pulls
+    - ./scripts/ci/create-benchmark-pr.sh "[benchmarks] Update weights for collectives" "$BRANCHNAME"
   after_script:
     - rm -rf .git/config
   tags:
-- 
GitLab