3 # ============LICENSE_START=======================================================
4 # Copyright (C) 2020 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
25 # navigate to root of the git clone
28 echo "Info : Identifying PR"
30 GITHUB_PR_NO=$(git show --pretty=format:%s --quiet | awk -F'[#)]' '{print $2}')
32 # skip commenting if the commit is not created using PR
33 if [[ -z "$GITHUB_PR_NO" ]]; then
34 echo "Info : The commit is not connected to a PR. Will not post comment to GitHub!"
39 GITHUB_PR_HTTP_URL="$GITHUB_BASE_URL/$PROJECT/pull/$GITHUB_PR_NO"
40 GITHUB_PR_API_URL="$GITHUB_API_URL/$PROJECT/pulls/$GITHUB_PR_NO/reviews"
41 IMAGE_NAME_TAG="$NORDIX_REGISTRY/$HARBOR_EIFFEL_PROJECT/$PROJECT:$IMAGE_TAG"
43 # log PR no to console
44 echo "Info : PR and API URLs"
45 echo " $GITHUB_PR_HTTP_URL"
46 echo " $GITHUB_PR_API_URL"
49 BUILD_STATUS=$(curl -s "$BUILD_URL/api/json?pretty=true" | grep result | awk -F'[""]' '{print $4}')
50 echo "Info : Build status is $BUILD_STATUS"
52 # file to store PR comment
53 GITHUB_PR_COMMENT_FILE="/tmp/pr_comment.txt.$$"
54 /bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
56 # construct the comment body depending on build status
57 if [[ "$BUILD_STATUS" == "SUCCESS" ]]; then
58 cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
62 - Build Status: **$BUILD_STATUS**\n
63 - Build URL: [Nordix Jenkins]($BUILD_URL)\n
64 - Command to pull image:\n
65 \`docker pull $IMAGE_NAME_TAG\`\n
67 \`podman pull $IMAGE_NAME_TAG\`
72 cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
76 - Build Status: **$BUILD_STATUS**\n
77 - Build URL: [Nordix Jenkins]($BUILD_URL)\n
83 # construct the full comment
84 echo "Info : PR comment"
85 echo "----------------------------------------------------------------"
86 cat "$GITHUB_PR_COMMENT_FILE"
87 echo "----------------------------------------------------------------"
89 echo "Info : Command to send PR comment"
90 echo "----------------------------------------------------------------"
91 echo "curl -s -u \"username:password\" -X POST -H \"Accept: application/vnd.github.v3+json\" \"$GITHUB_PR_API_URL\" -d \"@${GITHUB_PR_COMMENT_FILE}\""
92 echo "----------------------------------------------------------------"
94 curl -s -u "${NORDIXINFRA_GITHUB_USERNAME}:${NORDIXINFRA_GITHUB_TOKEN}" -X POST \
95 -H \"Accept: application/vnd.github.v3+json\" "$GITHUB_PR_API_URL" -d "@${GITHUB_PR_COMMENT_FILE}"
98 /bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
100 # vim: set ts=2 sw=2 expandtab: