-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.sh
executable file
·108 lines (95 loc) · 4.63 KB
/
run.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
#!/bin/sh
set -ex
rver="$(curl --silent https://www.perforce.com/perforce/doc.current/user/p4vnotes.txt |
sed -n 'N;N;s/.* Version \(20[0-9][0-9]\.[1-9][0-9]*\).*/\1/p;q')"
url=https://cdist2.perforce.com/perforce/r${rver#20}/bin.macosx1015x86_64/helix-core-server.tgz
test -n "$LAST_MODIFIED" ||
LAST_MODIFIED="Wed, 14 Jul 2021 13:53:46 GMT"
curl -I --silent --header "If-Modified-Since: $LAST_MODIFIED" $url | tr -d '\r' >out
first_line=$(head -n 1 <out)
case "$first_line,$rver" in
"HTTP/1.1 404 Not Found",2021.4)
# Seems that Perforce does not offer 21.4 for public download
rver=2021.2
url=https://cdist2.perforce.com/perforce/r${rver#20}/bin.macosx1015x86_64/helix-core-server.tgz
curl -I --silent --header "If-Modified-Since: $LAST_MODIFIED" $url | tr -d '\r' >out
first_line=$(head -n 1 <out)
;;
esac
case "$first_line" in
"HTTP/1.1 304 Not Modified")
;; # still up to date; nothing needs to be changed
"HTTP/1.1 200 OK")
# not up to date
new_last_modified="$(sed -n 's/^Last-Modified: //p' <out)"
file=helix-core-server.tgz
if test ! -f $file ||
case "$(uname -s)" in
Darwin)
test $(date -j -f "%a, %d %B %Y %H:%M:%S GMT" "$new_last_modified" +%s) -gt $(stat -f %m $file)
;;
*)
test $(date -d "$new_last_modified" +%s) -gt $(stat -c %Y $file)
;;
esac
then
curl --silent -o helix-core-server.tgz $url
fi
new_version="$(tar Oxvf $file Versions.txt | sed -n 's/^Rev\. P4D\/[^\/]*\/\(20[^\/]*\)\/\([^ ]*\).*/\1,\2/p')"
new_sha256="$(openssl dgst -sha256 $file)"
new_sha256="${new_sha256##* }"
file=Casks/perforce.rb
if test ! -d homebrew-cask/.git
then
git init homebrew-cask
echo "/$file" >homebrew-cask/.git/info/sparse-checkout
git -C homebrew-cask config core.sparseCheckout true
fi
git -C homebrew-cask fetch https://github.com/Homebrew/homebrew-cask/ master
git -C homebrew-cask reset --hard FETCH_HEAD
old_version=$(sed -n 's/^ *version "\(.*\)"$/\1/p' <homebrew-cask/$file)
commit_message="Update perforce from $old_version to $new_version"
sed -e 's/^\( *version "\)[^"]*/\1'"$new_version/" \
-e 's/^\( *sha256 "\)[^"]*/\1'"$new_sha256/" \
<homebrew-cask/$file >homebrew-cask/$file.new
mv homebrew-cask/$file.new homebrew-cask/$file
git -C homebrew-cask commit -m "$commit_message" $file
test -n "$GITHUB_PUSH_TOKEN" || {
echo "Need a push token!" >&2
exit 1
}
git -C homebrew-cask push https://dscho:[email protected]/gitgitgadget/homebrew-cask +HEAD:refs/heads/perforce
# Open the PR
body="$(printf '%s\\n' \
"<!-- If there’s a checkbox you can’t complete for any reason, that's okay, just explain in detail why you weren’t able to do so. -->" \
'' \
'After making all changes to the cask:' \
'' \
'- [ ] `brew cask audit --download {{cask_file}}` is error-free.' \
'- [ ] `brew cask style --fix {{cask_file}}` reports no offenses.' \
'- [x] The commit message includes the cask’s name and version.' \
'- [x] The submission is for [a stable version](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#finding-a-home-for-your-cask) or [documented exception](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#but-there-is-no-stable-version).' \
'' \
'This PR was generated by [a script](https://github.com/gitgitgadget/keep-homebrew-perforce-up-to-date/blob/master/run.sh) running in [an Azure Pipeline](https://dev.azure.com/gitgitgadget/git/_build?definitionId=11&_a=summary).')"
curl -XPOST -i -d '{"head":"gitgitgadget:perforce","base":"master","title":"'"$commit_message"'","body":"'"$body"'"}' \
https://dscho:[email protected]/repos/Homebrew/homebrew-cask/pulls | tee out
if ! grep "^\(HTTP/1.1 201 Created\|HTTP/2 201\)" out
then
echo "Failed to open a Pull Request" >&2
exit 1
fi
# Update the last.modified variable in this build definition
if test -n "$AZURE_PIPELINE_PAT"
then
url="$SYSTEM_TEAMFOUNDATIONSERVERURI$SYSTEM_TEAMPROJECTID/_apis/build/definitions/$SYSTEM_DEFINITIONID?api-version=5.0"
original_json="$(curl --silent -u "PAT:$AZURE_PIPELINE_PAT" -H "Accept: application/json; api-version=5.0; excludeUrls=true" "$url")"
json="$(echo "$original_json" | sed 's/\("last.modified":{"value":"\)[^"]*/\1'"$new_last_modified"/)"
curl --silent -X PUT -u "PAT:$AZURE_PIPELINE_PAT" -H "Content-Type: application/json" -d "$json" "$url"
fi
;;
*)
echo "Unexpected curl result:" >&2
cat out >&2
exit 1
;;
esac