From 81c6d8a98a8cd035727c63774019ac2e7fcb2a56 Mon Sep 17 00:00:00 2001 From: Fatih Degirmenci Date: Mon, 9 Nov 2020 09:23:55 +0000 Subject: [PATCH] eiffel: Enable posting comments to PR on GitHub Change-Id: Id42b79adab7def1eb3c40fd79d420ffa0fedfcf0 --- jjb/eiffel/macros/eiffel-macros.yaml | 6 ++ .../scripts/eiffel-global-pr-comment.sh | 100 ++++++++++++++++++ jjb/global/jjb-macros.yaml | 10 ++ 3 files changed, 116 insertions(+) create mode 100644 jjb/eiffel/scripts/eiffel-global-pr-comment.sh diff --git a/jjb/eiffel/macros/eiffel-macros.yaml b/jjb/eiffel/macros/eiffel-macros.yaml index 6c339bfc..7247ba12 100644 --- a/jjb/eiffel/macros/eiffel-macros.yaml +++ b/jjb/eiffel/macros/eiffel-macros.yaml @@ -29,4 +29,10 @@ - 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 index 00000000..2ca3942b --- /dev/null +++ b/jjb/eiffel/scripts/eiffel-global-pr-comment.sh @@ -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: diff --git a/jjb/global/jjb-macros.yaml b/jjb/global/jjb-macros.yaml index 9db98c31..8d6428ed 100644 --- a/jjb/global/jjb-macros.yaml +++ b/jjb/global/jjb-macros.yaml @@ -466,6 +466,16 @@ 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 -- 2.25.1