-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpactflow-publish.sh
executable file
·118 lines (112 loc) · 2.66 KB
/
pactflow-publish.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
#!/bin/bash
#
# Parse arguments
#
# https://unix.stackexchange.com/questions/129391/passing-named-arguments-to-shell-scripts
#
while [ $# -gt 0 ]; do
case "$1" in
--username=*)
username="${1#*=}"
;;
--password=*)
password="${1#*=}"
;;
--token=*)
token="${1#*=}"
;;
--pactflow-broker-url=*)
pactflow_broker_url="${1#*=}"
;;
--build-url=*)
build_url="${1#*=}"
;;
--pact-json-folder=*)
pact_json_folder="${1#*=}"
;;
--participant-version-number=*)
participant_version_number="${1#*=}"
;;
*)
printf "Invalid argument: $1"
exit 1
esac
shift
done
if [[ -z "$pactflow_broker_url" ]]; then
echo "Must provide --pactflow-broker-url" 1>&2
exit 1
fi
if [[ -z "$build_url" ]]; then
echo "Must provide --build-url" 1>&2
exit 1
fi
if [[ -z "$pact_json_folder" ]]; then
echo "Must provide --pact-json-folder" 1>&2
exit 1
fi
if [[ -z "$participant_version_number" ]]; then
echo "Must provide --participant-version-number" 1>&2
exit 1
fi
#
# Report to Pactflow
#
pact_json_files=$(ls $pact_json_folder/*.json)
echo "Found files:"
echo $pact_json_files
echo
for pact_json_file in $pact_json_files
do
echo "Processing $pact_json_file file..."
json=`cat $pact_json_file`
consumer_name=`echo $json | jq -r '.consumer.name'`
provider_name=`echo $json | jq -r '.provider.name'`
content_base64=`echo $json | base64`
content_base64=`echo $content_base64 | sed 's/[[:space:]]//g'`
branch=$(git symbolic-ref --short HEAD)
read -r -d '' publish_content << EndOfMessage
{
"pacticipantName": "$consumer_name",
"pacticipantVersionNumber": "$participant_version_number",
"branch": "$branch",
"tags": [
"$branch"
],
"buildUrl": "$build_url",
"contracts": [
{
"consumerName": "$consumer_name",
"providerName": "$provider_name",
"specification": "pact",
"contentType": "application/json",
"content": "$content_base64"
}
]
}
EndOfMessage
echo Publishing:
echo
echo $publish_content
echo
publish_content_file=$(mktemp)
echo $publish_content > $publish_content_file
if [ -n "$token" ]; then
curl -v \
-H "Authorization: Bearer $token" \
$pactflow_broker_url \
-H "Content-Type: application/json" \
--data-binary @$publish_content_file
elif [ -n "$username" ]; then
curl -v \
-u "$username:$password" \
$pactflow_broker_url \
-H "Content-Type: application/json" \
--data-binary @$publish_content_file
else
curl -v \
$pactflow_broker_url \
-H "Content-Type: application/json" \
--data-binary @$publish_content_file
fi
done