eiffel: Enable posting comments to PR on GitHub 23/6523/8
authorFatih Degirmenci <fatih.degirmenci@est.tech>
Mon, 9 Nov 2020 09:23:55 +0000 (09:23 +0000)
committerFatih Degirmenci <fdegir@gmail.com>
Thu, 12 Nov 2020 13:14:07 +0000 (13:14 +0000)
Change-Id: Id42b79adab7def1eb3c40fd79d420ffa0fedfcf0

jjb/eiffel/macros/eiffel-macros.yaml
jjb/eiffel/scripts/eiffel-global-pr-comment.sh [new file with mode: 0644]
jjb/global/jjb-macros.yaml

index 6c339bfc1575c773f78829251b74cf7081b4eaae..7247ba12235da22177da61fb8f29f36ab138783d 100644 (file)
       - shell:
           !include-raw: ../scripts/eiffel-remrem-publish-container-build.sh
 
+- builder:
+    name: 'eiffel-global-pr-comment-macro'
+    builders:
+      - shell:
+          !include-raw: ../scripts/eiffel-global-pr-comment.sh
+
 # vim: set ts=2 sw=2 expandtab:
diff --git a/jjb/eiffel/scripts/eiffel-global-pr-comment.sh b/jjb/eiffel/scripts/eiffel-global-pr-comment.sh
new file mode 100644 (file)
index 0000000..2ca3942
--- /dev/null
@@ -0,0 +1,100 @@
+#!/bin/bash
+
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+set -o errexit
+set -o pipefail
+set -o nounset
+
+# navigate to root of the git clone
+#cd "$WORKSPACE"
+
+echo "Info  : Identifying PR"
+# get the PR no
+GITHUB_PR_NO=$(git show --pretty=format:%s --quiet | awk -F'[#)]' '{print $2}')
+
+# skip commenting if the commit is not created using PR
+if [[ -z "$GITHUB_PR_NO" ]]; then
+  echo "Info  : The commit is not connected to a PR. Will not post comment to GitHub!"
+  exit 0
+fi
+
+# construct the URLs
+GITHUB_PR_HTTP_URL="$GITHUB_BASE_URL/$PROJECT/pull/$GITHUB_PR_NO"
+GITHUB_PR_API_URL="$GITHUB_API_URL/$PROJECT/pulls/$GITHUB_PR_NO/reviews"
+IMAGE_NAME_TAG="$NORDIX_REGISTRY/$HARBOR_EIFFEL_PROJECT/$PROJECT:$IMAGE_TAG"
+
+# log PR no to console
+echo "Info  : PR and API URLs"
+echo "        $GITHUB_PR_HTTP_URL"
+echo "        $GITHUB_PR_API_URL"
+
+# get build status
+BUILD_STATUS=$(curl -s "$BUILD_URL/api/json?pretty=true" | grep result | awk -F'[""]' '{print $4}')
+echo "Info  : Build status is $BUILD_STATUS"
+
+# file to store PR comment
+GITHUB_PR_COMMENT_FILE="/tmp/pr_comment.txt.$$"
+/bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
+
+# construct the comment body depending on build status
+if [[ "$BUILD_STATUS" == "SUCCESS" ]]; then
+  cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
+{
+  "event": "COMMENT",
+  "body": "
+- Build Status: **$BUILD_STATUS**\n
+- Build URL: [Nordix Jenkins]($BUILD_URL)\n
+- Command to pull image:\n
+    \`docker pull $IMAGE_NAME_TAG\`\n
+    or\n
+    \`podman pull $IMAGE_NAME_TAG\`
+"
+}
+EOF
+else
+  cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
+{
+  "event": "COMMENT",
+  "body": "
+- Build Status: **$BUILD_STATUS**\n
+- Build URL: [Nordix Jenkins]($BUILD_URL)\n
+"
+}
+EOF
+fi
+
+# construct the full comment
+echo "Info  : PR comment"
+echo "----------------------------------------------------------------"
+cat "$GITHUB_PR_COMMENT_FILE"
+echo "----------------------------------------------------------------"
+echo
+echo "Info  : Command to send PR comment"
+echo "----------------------------------------------------------------"
+echo "curl -s -u \"username:password\" -X POST -H \"Accept: application/vnd.github.v3+json\" \"$GITHUB_PR_API_URL\" -d \"@${GITHUB_PR_COMMENT_FILE}\""
+echo "----------------------------------------------------------------"
+
+curl -s -u "${NORDIXINFRA_GITHUB_USERNAME}:${NORDIXINFRA_GITHUB_TOKEN}" -X POST \
+    -H \"Accept: application/vnd.github.v3+json\" "$GITHUB_PR_API_URL" -d "@${GITHUB_PR_COMMENT_FILE}"
+
+# remove the file
+/bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
+
+# vim: set ts=2 sw=2 expandtab:
index 9db98c31a5d933bb5907a14f795ce779d41fec7f..8d6428ed01b56e34149cefdc3855df52e60f2eae 100644 (file)
               username: HARBOR_USERNAME
               password: HARBOR_PASSWORD
 
+# wrappers to use for jobs
+- wrapper:
+    name: nordixinfra-eiffel-github-token
+    wrappers:
+      - credentials-binding:
+          - username-password-separated:
+              credential-id: nordix-eiffel-github-token
+              username: NORDIXINFRA_GITHUB_USERNAME
+              password: NORDIXINFRA_GITHUB_TOKEN
+
 # timeout wrapper to use for jobs
 - wrapper:
     name: build-timeout