From b2655b5220ba37eab167ef03ff176f15122f7e5f Mon Sep 17 00:00:00 2001
From: s3krit <pugh@s3kr.it>
Date: Fri, 8 May 2020 17:31:23 +0200
Subject: [PATCH] [CI] add check_tags CI job (#1072)

* add check_tags CI job

* fix typo in gitlab-ci.yml

* add more useful CI output

* Make presence of github token optional
---
 polkadot/.gitlab-ci.yml               |  9 +++++++++
 polkadot/scripts/gitlab/check_tags.sh | 23 +++++++++++++++++++++++
 polkadot/scripts/gitlab/lib.sh        | 12 ++++++++++--
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100755 polkadot/scripts/gitlab/check_tags.sh

diff --git a/polkadot/.gitlab-ci.yml b/polkadot/.gitlab-ci.yml
index 2c7e1127e2e..c1f760130be 100644
--- a/polkadot/.gitlab-ci.yml
+++ b/polkadot/.gitlab-ci.yml
@@ -73,7 +73,16 @@ variables:
     - /^v[0-9]+\.[0-9]+.*$/        # i.e. v1.0, v2.1rc1
     - /^[0-9]+$/
 
+#### stage:                       .pre
 
+check-labels:
+  stage:                          .pre
+  image:                          parity/tools:latest
+  <<:                             *kubernetes-env
+  only:
+    - /^[0-9]+$/
+  script:
+    - ./scripts/gitlab/check_tags.sh
 
 #### stage:                        test
 
diff --git a/polkadot/scripts/gitlab/check_tags.sh b/polkadot/scripts/gitlab/check_tags.sh
new file mode 100755
index 00000000000..f8f3df941fb
--- /dev/null
+++ b/polkadot/scripts/gitlab/check_tags.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+#shellcheck source=lib.sh
+source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/lib.sh"
+
+# Must have one of the following labels
+labels=(
+  'B1-releasenotes'
+  'B1-runtimeworthy'
+  'B1-silent'
+)
+
+echo "[+] Checking labels for $CI_COMMIT_BRANCH"
+
+for label in "${labels[@]}"; do
+  if has_label 'paritytech/polkadot' "$CI_COMMIT_BRANCH" "$label"; then
+    echo "[+] Label $label detected, test passed"
+    exit 0
+  fi
+done
+
+echo "[!] PR does not have one of the required labels! Please add one of: ${labels[*]}"
+exit 1
diff --git a/polkadot/scripts/gitlab/lib.sh b/polkadot/scripts/gitlab/lib.sh
index 7fae82f1895..3d7897a21b9 100755
--- a/polkadot/scripts/gitlab/lib.sh
+++ b/polkadot/scripts/gitlab/lib.sh
@@ -21,7 +21,11 @@ sanitised_git_logs(){
 check_tag () {
   repo=$1
   tagver=$2
-  tag_out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/git/refs/tags/$tagver")
+  if [ -n "$GITHUB_RELEASE_TOKEN" ]; then
+    tag_out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/git/refs/tags/$tagver")
+  else
+    tag_out=$(curl -s "$api_base/$repo/git/refs/tags/$tagver")
+  fi
   tag_sha=$(echo "$tag_out" | jq -r .object.sha)
   object_url=$(echo "$tag_out" | jq -r .object.url)
   if [ "$tag_sha" = "null" ]; then
@@ -46,7 +50,11 @@ has_label(){
   repo="$1"
   pr_id="$2"
   label="$3"
-  out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/pulls/$pr_id")
+  if [ -n "$GITHUB_RELEASE_TOKEN" ]; then
+    out=$(curl -H "Authorization: token $GITHUB_RELEASE_TOKEN" -s "$api_base/$repo/pulls/$pr_id")
+  else
+    out=$(curl -s "$api_base/$repo/pulls/$pr_id")
+  fi
   [ -n "$(echo "$out" | tr -d '\r\n' | jq ".labels | .[] | select(.name==\"$label\")")" ]
 }
 
-- 
GitLab