Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 3d5c7bd

Browse files
authored
Merge pull request #4 from SHyx0rmZ/put-should-wait-for-status-to-appear
Put now waits 5 seconds for status to appear Closes #3
2 parents a03ae58 + 9a98311 commit 3d5c7bd

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

bin/out

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ jq -c -n \
7171
| curlgh -d@- "$source_endpoint/repos/$source_repository/statuses/$commit" \
7272
> /tmp/gh-result
7373

74+
#
75+
# check retry counter
76+
#
77+
78+
REMAINING_TRIES=5
79+
80+
while [[ $REMAINING_TRIES > 0 ]]; do
81+
82+
#
83+
# lookup
84+
#
85+
86+
curlgh "$source_endpoint/repos/$source_repository/commits/$source_branch/status" \
87+
| jq -c \
88+
--arg ref "$(jq -r '.id | tostring' < /tmp/gh-result)" \
89+
'{
90+
"sha": .sha,
91+
"status": ( .statuses | map(select( $ref == ( .id | tostring ) )) | .[0] )
92+
}' \
93+
> /tmp/status
94+
95+
#
96+
# validate
97+
#
98+
99+
[[ -s /tmp/status ]] \
100+
&& jq -e '.status' < /tmp/status > /dev/null \
101+
&& break
102+
103+
#
104+
# decrease retry counter and loop
105+
#
106+
107+
REMAINING_TRIES=$(($REMAINING_TRIES - 1))
108+
109+
sleep 1
110+
111+
done
74112

75113
#
76114
# concourse

test/out/wait-for-status.sh

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
#!/bin/sh
3+
4+
response_after_creation() {
5+
cat <<EOF | nc -l -s 127.0.0.1 -p 9192 > $TMPDIR/http.req-$$
6+
HTTP/1.0 200 OK
7+
8+
{
9+
"created_at": "2012-07-20T01:19:13Z",
10+
"updated_at": "2012-07-20T02:19:13Z",
11+
"state": "success",
12+
"target_url": "https://ci.example.com/1000/output",
13+
"description": "Build has completed successfully",
14+
"id": 1,
15+
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/1",
16+
"context": "continuous-integration/jenkins",
17+
"creator": {
18+
"login": "octocat",
19+
"id": 1,
20+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
21+
"gravatar_id": "",
22+
"url": "https://api.github.com/users/octocat",
23+
"html_url": "https://github.com/octocat",
24+
"followers_url": "https://api.github.com/users/octocat/followers",
25+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
26+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
27+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
28+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
29+
"organizations_url": "https://api.github.com/users/octocat/orgs",
30+
"repos_url": "https://api.github.com/users/octocat/repos",
31+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
32+
"received_events_url": "https://api.github.com/users/octocat/received_events",
33+
"type": "User",
34+
"site_admin": false
35+
}
36+
}
37+
EOF
38+
}
39+
40+
response_with_status() {
41+
cat <<EOF | nc -l -s 127.0.0.1 -p 9192 > /dev/null
42+
HTTP/1.0 200 OK
43+
44+
{
45+
"state": "success",
46+
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
47+
"total_count": 2,
48+
"statuses": [
49+
{
50+
"created_at": "2012-07-20T01:19:13Z",
51+
"updated_at": "2012-07-20T01:19:13Z",
52+
"state": "success",
53+
"target_url": "https://ci.example.com/1000/output",
54+
"description": "Build has completed successfully",
55+
"id": 1,
56+
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/1",
57+
"context": "continuous-integration/jenkins"
58+
},
59+
{
60+
"created_at": "2012-08-20T01:19:13Z",
61+
"updated_at": "2012-08-20T01:19:13Z",
62+
"state": "success",
63+
"target_url": "https://ci.example.com/2000/output",
64+
"description": "Testing has completed successfully",
65+
"id": 2,
66+
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/2",
67+
"context": "security/brakeman"
68+
}
69+
],
70+
"repository": {},
71+
"commit_url": "https://api.github.com/repos/octocat/Hello-World/6dcb09b5b57875f334f61aebed695e2e4193db5e",
72+
"url": "https://api.github.com/repos/octocat/Hello-World/6dcb09b5b57875f334f61aebed695e2e4193db5e/status"
73+
}
74+
EOF
75+
}
76+
77+
response_without_status() {
78+
cat <<EOF | nc -l -s 127.0.0.1 -p 9192 > /dev/null
79+
HTTP/1.0 200 OK
80+
81+
{
82+
"state": "success",
83+
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
84+
"total_count": 1,
85+
"statuses": [
86+
{
87+
"created_at": "2012-08-20T01:19:13Z",
88+
"updated_at": "2012-08-20T01:19:13Z",
89+
"state": "success",
90+
"target_url": "https://ci.example.com/2000/output",
91+
"description": "Testing has completed successfully",
92+
"id": 2,
93+
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/2",
94+
"context": "security/brakeman"
95+
}
96+
],
97+
"repository": {},
98+
"commit_url": "https://api.github.com/repos/octocat/Hello-World/6dcb09b5b57875f334f61aebed695e2e4193db5e",
99+
"url": "https://api.github.com/repos/octocat/Hello-World/6dcb09b5b57875f334f61aebed695e2e4193db5e/status"
100+
}
101+
EOF
102+
}
103+
104+
sequential_responses() {
105+
response_after_creation
106+
response_without_status
107+
response_with_status
108+
}
109+
110+
set -eu
111+
112+
DIR=$( dirname "$0" )/../..
113+
114+
echo 'a1b2c3d4e5' > $TMPDIR/commit
115+
116+
sequential_responses &
117+
118+
BUILD_ID=123 $DIR/bin/out > $TMPDIR/resource-$$ <<EOF
119+
{
120+
"params": {
121+
"description": "test-description",
122+
"commit": "$TMPDIR/commit",
123+
"state": "success",
124+
"target_url": "https://ci.example.com/\$BUILD_ID/output"
125+
},
126+
"source": {
127+
"access_token": "test-token",
128+
"context": "test-context",
129+
"endpoint": "http://127.0.0.1:9192",
130+
"repository": "dpb587/test-repo"
131+
}
132+
}
133+
EOF
134+
135+
if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.req-$$ ; then
136+
echo "FAILURE: Invalid HTTP method or URI"
137+
cat $TMPDIR/http.req-$$
138+
exit 1
139+
fi
140+
141+
if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
142+
echo "FAILURE: Unexpected request body"
143+
cat $TMPDIR/http.req-$$
144+
exit 1
145+
fi
146+
147+
if ! grep -q '"version":{"ref":"1"}' $TMPDIR/resource-$$ ; then
148+
echo "FAILURE: Unexpected version output"
149+
cat $TMPDIR/resource-$$
150+
exit 1
151+
fi
152+
153+
if ! grep -q '{"name":"created_at","value":"2012-07-20T01:19:13Z"}' $TMPDIR/resource-$$ ; then
154+
echo "FAILURE: Unexpected created_at output"
155+
cat $TMPDIR/resource-$$
156+
exit 1
157+
fi
158+
159+
if ! grep -q '{"name":"created_by","value":"octocat"}' $TMPDIR/resource-$$ ; then
160+
echo "FAILURE: Unexpected creator output"
161+
cat $TMPDIR/resource-$$
162+
exit 1
163+
fi

0 commit comments

Comments
 (0)