-
Notifications
You must be signed in to change notification settings - Fork 8
/
in
executable file
·165 lines (127 loc) · 4.71 KB
/
in
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
162
163
164
165
#!/bin/bash
# vim: set ft=sh
set -e
exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging
ASSETS=$(cd "$(dirname "$0")" && pwd)
source $ASSETS/helpers/git.sh
source $ASSETS/helpers/utils.sh
source $ASSETS/helpers/bitbucket.sh
# for all temporary files in 'check'
tmpfile() {
tmp_file_unique "in-$1"
}
destination=$1
if [ -z "$destination" ]; then
echo "usage: $0 <path/to/destination>" >&2
exit 1
fi
# for jq
PATH=/usr/local/bin:$PATH
payload=$(tmpfile request)
cat > "$payload" <&0
load_pubkey "$payload"
configure_credentials "$payload"
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
uri=$(jq -r '.source.uri // ""' < "$payload")
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
commit_verification_key_ids=$(jq -r '(.source.commit_verification_key_ids // [])[]' < "$payload")
commit_verification_keys=$(jq -r '(.source.commit_verification_keys // [])[]' < "$payload")
gpg_keyserver=$(jq -r '.source.gpg_keyserver // "hkp://keys.gnupg.net/"' < "$payload")
depth=$(jq -r '(.params.depth // 0)' < "$payload")
submodules=$(jq -r '(.params.submodules // "all")' < "$payload")
disable_git_lfs=$(jq -r '(.params.disable_git_lfs // false)' < "$payload")
prq_id=$(jq -r '.version.id // ""' < "$payload")
prq_date=$(jq -r '.version.date // ""' < "$payload")
configure_git_ssl_verification "$skip_ssl_verification"
configure_git_global "${git_config_payload}"
if [ -z "$uri" ]; then
log "invalid payload (missing uri):" "$(cat "$payload")"
exit 1
fi
if [ -z "$prq_id" ]; then
log "invalid payload (missing pull request id):" "$(cat "$payload")"
exit 1
fi
depthflag=""
if test "$depth" -gt 0 2> /dev/null; then
depthflag="--depth $depth"
fi
branch="pull-requests/${prq_id}/merge"
log "Cloning $uri in $destination"
git clone $depthflag "$uri" "$destination"
cd "$destination"
git fetch $depthflag origin "+refs/${branch}:refs/remotes/origin/${branch}"
git checkout -B $branch origin/$branch
ref=$(git rev-parse HEAD)
invalid_key() {
echo "Invalid GPG key in: ${commit_verification_keys}"
exit 2
}
commit_not_signed() {
commit_id=$(git rev-parse ${ref})
echo "The commit ${commit_id} is not signed"
exit 1
}
if [ ! -z "${commit_verification_keys}" ] || [ ! -z "${commit_verification_key_ids}" ] ; then
if [ ! -z "${commit_verification_keys}" ]; then
echo "${commit_verification_keys}" | gpg --batch --import || invalid_key "${commit_verification_keys}"
fi
if [ ! -z "${commit_verification_key_ids}" ]; then
echo "${commit_verification_key_ids}" | \
xargs --no-run-if-empty -n1 gpg --batch --keyserver $gpg_keyserver --recv-keys
fi
git verify-commit $(git rev-list -n 1 $ref) || commit_not_signed
fi
if [ "$disable_git_lfs" != "true" ]; then
git lfs fetch
git lfs checkout
fi
git clean --force --force -d
if [ "$submodules" == "all" ]; then
git submodule update --init $depthflag --recursive
elif [ "$submodules" != "none" ]; then
submodules=$(echo $submodules | jq -r '(.[])')
for submodule in $submodules; do
git submodule update --init $depthflag --recursive $submodule
done
fi
if [ "$disable_git_lfs" != "true" ]; then
git submodule foreach "git lfs fetch && git lfs checkout"
fi
# calculate source and target commit
source_commit=$(git rev-list --parents -1 $ref | awk '{print $3}')
target_commit=$(git rev-list --parents -1 $ref | awk '{print $2}')
if [ -z "$source_commit" ]; then
log "Unable to determine source commit from merge commit $ref. Please verify depth configuration."
exit 1
fi
if [ -z "$target_commit" ]; then
log "Unable to determine target commit from merge commit $ref. Please verify depth configuration."
exit 1
fi
# parse uri and retrieve host
uri_parser "$uri"
repo_host="${uri_schema}://${uri_address}"
repo_host=${repo_host}$(getBasePathOfBitbucket)
# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
repo_project=$(basename $(dirname "$uri"))
# verify target branch of prq
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_id" "" "$skip_ssl_verification")
if [ "$prq" != "NO_SUCH_PULL_REQUEST" ] && \
[ "$prq" != "ALREADY_MERGED" ] && \
[ "$prq" != "DECLINED" ]; then
branch=$(echo "$prq" | jq -r '.fromRef.displayId')
fi
# expose configuration of pull request that can be used in container
git config --add pullrequest.id $prq_id
git config --add pullrequest.source $source_commit
git config --add pullrequest.target $target_commit
git config --add pullrequest.merge $ref
git config --add pullrequest.date "$prq_date"
git config --add pullrequest.branch "$branch"
jq -n "{
version: $(jq '.version' < "$payload"),
metadata: $(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification")
}" >&3