eiffel: Enable posting comments to PR on GitHub
[infra/cicd.git] / jjb / eiffel / scripts / eiffel-global-pr-comment.sh
1 #!/bin/bash
2
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
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 set -o errexit
22 set -o pipefail
23 set -o nounset
24
25 # navigate to root of the git clone
26 #cd "$WORKSPACE"
27
28 echo "Info  : Identifying PR"
29 # get the PR no
30 GITHUB_PR_NO=$(git show --pretty=format:%s --quiet | awk -F'[#)]' '{print $2}')
31
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!"
35   exit 0
36 fi
37
38 # construct the URLs
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"
42
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"
47
48 # get build status
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"
51
52 # file to store PR comment
53 GITHUB_PR_COMMENT_FILE="/tmp/pr_comment.txt.$$"
54 /bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
55
56 # construct the comment body depending on build status
57 if [[ "$BUILD_STATUS" == "SUCCESS" ]]; then
58   cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
59 {
60   "event": "COMMENT",
61   "body": "
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
66     or\n
67     \`podman pull $IMAGE_NAME_TAG\`
68 "
69 }
70 EOF
71 else
72   cat << EOF >> "$GITHUB_PR_COMMENT_FILE"
73 {
74   "event": "COMMENT",
75   "body": "
76 - Build Status: **$BUILD_STATUS**\n
77 - Build URL: [Nordix Jenkins]($BUILD_URL)\n
78 "
79 }
80 EOF
81 fi
82
83 # construct the full comment
84 echo "Info  : PR comment"
85 echo "----------------------------------------------------------------"
86 cat "$GITHUB_PR_COMMENT_FILE"
87 echo "----------------------------------------------------------------"
88 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 "----------------------------------------------------------------"
93
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}"
96
97 # remove the file
98 /bin/rm -rf "$GITHUB_PR_COMMENT_FILE"
99
100 # vim: set ts=2 sw=2 expandtab: