#!/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: