-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate_pr.sh
executable file
·161 lines (137 loc) · 5.66 KB
/
create_pr.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. 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.
set -e
set -o pipefail
set -x
REPO="$1"
FILEPATH="$2"
PR_BRANCH="${3:-image-tag-update}"
EXTRA_PR_BODY="${4:-}"
SED=sed
if [[ "$(uname -s)" == "Darwin" ]]; then
SED=gsed
fi
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
OTHER_CLONE_ROOT=${OTHER_CLONE_ROOT:-${SCRIPT_ROOT}/../../..}
MINIMAL_IMAGE_REBUILD_PJ_NAME="quarterly-minimal-image-rebuild"
GOLANG_RELEASE_PERIODIC="check-upstream-golang-release"
CHANGED_FILE="tag file(s)"
CHANGED_COMPONENT="base image"
if [[ $REPO =~ "prow-jobs" ]]; then
CHANGED_FILE="Prowjobs"
CHANGED_COMPONENT="builder-base image tag"
fi
if [[ $JOB_NAME =~ "prow-deck-tooling" ]]; then
CHANGED_FILE="Prow controlplane Helm chart"
CHANGED_COMPONENT="Prow component images"
fi
if [[ $JOB_NAME =~ $MINIMAL_IMAGE_REBUILD_PJ_NAME ]]; then
CHANGED_FILE="EKS_DISTRO_TAG_FILE"
CHANGED_COMPONENT="image tags"
fi
if [[ $JOB_NAME =~ $GOLANG_RELEASE_PERIODIC ]]; then
CHANGED_FILE="builder-base and eks-distro-base"
CHANGED_COMPONENT="golang"
fi
if [ $REPO_OWNER = "aws" ]; then
ORIGIN_ORG="eks-distro-pr-bot"
else
ORIGIN_ORG=$REPO_OWNER
fi
PR_TITLE="Update ${CHANGED_COMPONENT} in ${CHANGED_FILE}"
COMMIT_MESSAGE="[PR BOT] ${PR_TITLE}"
if [[ $REPO =~ "prow-jobs" ]]; then
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/builder_base_pr_body
else
if [[ $JOB_NAME =~ "prow-deck-tooling" ]]; then
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/prow_cp_pr_body
elif [[ $JOB_NAME =~ $MINIMAL_IMAGE_REBUILD_PJ_NAME ]]; then
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/rebuild-minimal-images-pr-body
else
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/eks_distro_base_other_repo_pr_body
if [ $REPO = "eks-distro-build-tooling" ]; then
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/eks_distro_base_pr_body
fi
cp $PR_BODY_FILE ${SCRIPT_ROOT}/../pr-scripts/${REPO}_pr_body
PR_BODY_FILE=${SCRIPT_ROOT}/../pr-scripts/${REPO}_pr_body
$SED -i "s,in .* with,in ${CHANGED_FILE} with," $PR_BODY_FILE
fi
fi
if [ -n "${EXTRA_PR_BODY}" ]; then
printf "${EXTRA_PR_BODY}" >>$PR_BODY_FILE
fi
# Adding this here to include the "do-not-merge/hold" label. Trying to use the gh client with the --label arg will not succeed
# as the bot doesn't have permission to add labels. Doing it this way our Prow chatops will pick it up and add it.
printf "\n/hold\n" >>$PR_BODY_FILE
PROW_BUCKET_NAME=$(echo $JOB_SPEC | jq -r ".decoration_config.gcs_configuration.bucket" | awk -F// '{print $NF}')
printf "\nClick [here](https://prow.eks.amazonaws.com/view/s3/$PROW_BUCKET_NAME/logs/$JOB_NAME/$BUILD_ID) to view job logs.
\nBy submitting this pull request,\
I confirm that you can use, modify, copy,\
and redistribute this contribution,\
under the terms of your choice." >>$PR_BODY_FILE
PR_BODY=$(cat $PR_BODY_FILE)
cd ${OTHER_CLONE_ROOT}/${ORIGIN_ORG}/${REPO}
if [[ "$(basename "$FILEPATH")" != "$FILEPATH" ]]; then
cd $(dirname $FILEPATH)
FILEPATH="$(basename $FILEPATH)"
fi
for FILE in $(find ./ -type f -name "$FILEPATH"); do
git add $FILE
done
cd ${OTHER_CLONE_ROOT}/${ORIGIN_ORG}/${REPO}
if [ $REPO = "eks-distro-build-tooling" ]; then
git add ./eks-distro-base-minimal-packages/. ./eks-distro-base-updates/.
fi
if [[ $REPO =~ "prow-jobs" ]]; then
git add ./BUILDER_BASE_TAG_FILE
fi
if [[ $JOB_NAME =~ $MINIMAL_IMAGE_REBUILD_PJ_NAME ]]; then
git add ../EKS_DISTRO_TAG_FILE.yaml
fi
if [[ $JOB_NAME =~ $GOLANG_RELEASE_PERIODIC ]]; then
# sync expected files changed during the previous make calls in aws/eks-distro-build-tooling to pr-bot fork
cp -f ${SCRIPT_ROOT}/../EKS_DISTRO_TAG_FILE.yaml ./EKS_DISTRO_TAG_FILE.yaml
cp -f ${SCRIPT_ROOT}/../eks-distro-base/golang_versions.yaml ./eks-distro-base/golang_versions.yaml
cp -f ${SCRIPT_ROOT}/../builder-base/versions.yaml ./builder-base/versions.yaml
cp -f ${SCRIPT_ROOT}/../eks-distro-base/make-tests/make-dry-run ./eks-distro-base/make-tests/make-dry-run
cp -rf ${SCRIPT_ROOT}/../builder-base/checksums ./builder-base
cp -rf ${SCRIPT_ROOT}/../eks-distro-base/make-tests/expected ./eks-distro-base/make-tests
git add ./EKS_DISTRO_TAG_FILE.yaml
git add ./eks-distro-base/golang_versions.yaml
git add ./builder-base/versions.yaml
git add ./builder-base/checksums/go-*
git add ./eks-distro-base/make-tests/expected/*golang*
git add ./eks-distro-base/make-tests/expected/export/all
git add ./eks-distro-base/make-tests/expected/standard/create-pr/all-create-prs
git add ./eks-distro-base/make-tests/expected/test/all
git add ./eks-distro-base/make-tests/make-dry-run
# TODO: remove after verifying what changes are unstaged
git diff --name-only
fi
FILES_ADDED=$(git diff --staged --name-only)
if [ "$FILES_ADDED" = "" ]; then
exit 0
fi
if [ "$JOB_TYPE" = "presubmit" ]; then
git diff --staged
exit 0
fi
git commit -m "$COMMIT_MESSAGE"
ssh-agent bash -c 'ssh-add /secrets/ssh-secrets/ssh-key; ssh -o StrictHostKeyChecking=no git@github.com; git push -u origin $PR_BRANCH -f'
gh auth login --with-token </secrets/github-secrets/token
PR_EXISTS=$(GH_PAGER='' gh pr list --json number -H "${PR_BRANCH}")
if [ "$PR_EXISTS" = "[]" ]; then
gh pr create --title "$PR_TITLE" --body "$PR_BODY"
fi