-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl-cookbook.sh
162 lines (130 loc) · 5.3 KB
/
curl-cookbook.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
usage_error() {
echo $1
echo
echo "<base_url> examples:"
echo ' CODESPACE: http://api.github.localhost/repos/{owner}/{repo}'
echo ' GHE TEST ENV: https://api.darylcantrell-03d56231eb10a1a11.ghe-test.org/repos/{owner}/{repo}'
}
exit
collab_pat='collaborator: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'
# Merge existing PR -- data args are all optional
curl -u $collab_pat \
--request PUT \
--header "Content-Type: application/json" \
--data '{
"commit_title": "commit_title",
"commit_message": "commit_message",
"sha": "cf2adc2b8ee9dbe4a3463754c6c2c1014e012060",
"merge_method": "merge"
}' \
http://api.github.localhost/repos/github/public-server/pulls/5/merge
# Get merge commit for existing PR
pr_getmerge() {
curl -s -X GET \
-H "Authorization: Bearer $2" \
http://api.github.localhost/repos/github/public-server/pulls/$1 | jq -r .merge_commit_sha
}
# Post status to commit
# error, failure, pending, success
collab_pat='darylcantrell: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'
curl -u $collab_pat \
--request POST \
--header "Content-Type: application/json" \
--header "Accept: application/vnd.github+json" \
--data '{
"state":"success",
"target_url":"https://example.com/build/status",
"description":"PAT status foo",
"context":"foo"
}' \
https://api.github.com/repos/darylc-test-1/test-1/statuses/f6cf56aaa4e10f9f9fa2db4e988149d972941025
# https://api.darylcantrell-03d56231eb10a1a11.ghe-test.org/repos/Org1/Repo1/statuses/fc3bf2ecc69c65024c610fe0633753ffd47fbcc9
# List branches
curl -u :ghs_ggxgK9JlqFS1AH6ROOsq603g4HiCwy45BoGh \
--request GET \
--header "Content-Type: application/json" \
--header "Accept: application/vnd.github+json" \
http://api.github.localhost/repos/github/public-server/branches
# Rename branch
curl -u :ghs_ggxgK9JlqFS1AH6ROOsq603g4HiCwy45BoGh \
--request POST \
--header "Content-Type: application/json" \
--header "Accept: application/vnd.github+json" \
--data '{"new_name":"topic8"}' \
http://api.github.localhost/repos/github/public-server/branches/topic7/rename
getbp() {
curl -i \
--request GET \
--header "Authorization: token $2" \
--header "Accept: application/vnd.github+json" \
http://api.github.localhost/repos/github/public-server/branches/$1/protection
}
## Github-App
## ==========
# Step 0: Go to GH App page and get:
- App ID from top of page
- Private key from bottom of page
# Step 1: Extract pkcs pubkey from pem private key
openssl rsa -in testapp1.2022-08-05.private-key.pem -outform PEM -pubout -out testapp1.2022-08-05.public-key.pkcs1
# Step 2: create JWT at https://jwt.io/#debugger-io (switch dropdown to RS256)
echo \"iat\": $(expr `date +%s` - 60), ; echo \"exp\": $(expr `date +%s` + 600), # now -1min / +10min
# Use app id as "iss" value. JWT contents should look like this:
HEADER:
{
"alg": "RS256",
"typ": "JWT"
}
PAYLOAD:
{
"iat": 1659819187,
"exp": 1659819847,
"iss": "1"
}
# Step 3 (possibly skippable): Get installation id from list of places where app has been installed
# NOTE: This shows you all installations for an app. If you have only one installation of interest,
# a short cut is going to http://localhost/ORG/REPO/settings/installations, click Configure on the
# app and look at the URL for the installation id. Then skip to step 4.
app_jwt='eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NTk5MTAxNzIsImV4cCI6MTY1OTkxMDgzMiwiaXNzIjoiMSJ9.kaeQsXrpbfNlLnPXhE9w-meAnBFj-MW0CcG7r8tZ5u1sfN8Zg7jiPdy26aJwzX17PJpoI_LuH444Od_t1z5oftfDzjMO-t0InupdsiDebznUm2zzDfyxe_KIyqCNlpowpllWKQ5jQTvTumhUaeMbCVN1JXdFimYZAfOFas5jEi8ZQ5QYH3U-rU0BNyqSLJef-ODnf64ZP9yv2tdJOlrIdrVtlzlX7u0BhHeEjtXB8wZeneV6YYlqQ3OqVRsiq3e4rNGCS0PuGwkugPYRY8k8wLz1T6Enp2KAsksnU7f-fPLQJ1bVu4GphdjKlirFTuYSMCCRVsQJysqs_F9x7V8b-Q'
curl -i -X GET \
-H "Authorization: Bearer $app_jwt" \
-H "Accept: application/vnd.github+json" \
http://api.github.localhost/app/installations
#https://api.darylcantrell-03d56231eb10a1a11.ghe-test.org/app/installations
# Step 4: Use JWT token and installation id to create 1hr access token for that installation.
curl -i -X POST \
-H "Authorization: Bearer $app_jwt" \
-H "Accept: application/vnd.github+json" \
http://api.github.localhost/app/installations/**installation_id from above**/access_tokens
#https://api.darylcantrell-0577bb434cff36aeb.ghe-test.ninja/app/installations/1/access_tokens
# Step 5: Use 1hr access token to make API calls as app
poststat() {
curl -i \
--request POST \
--header "Authorization: token $2" \
--header "Content-Type: application/json" \
--header "Accept: application/vnd.github+json" \
--data '{
"state":"success",
"target_url":"https://example.com/build/status",
"description":"Status '$3'",
"context":"foo"
}' \
https://api.darylcantrell-0348810741b29a732.ghe-test.com/repos/org1/repo1/statuses/$1
}
# http://api.github.localhost/repos/Org1/Repo1/statuses/$1
# https://api.darylcantrell-03d56231eb10a1a11.ghe-test.org/repos/Org1/Repo1/statuses/$1
# Make a private test repo
mkprv() {
curl -i \
--request POST \
--header "Authorization: token $token3" \
--header "Content-Type: application/json" \
--header "Accept: application/vnd.github+json" \
--data '{
"description": "desc",
"homepage": "https://google.com",
"private": true,
"auto_init": true,
"name": "prv_test_'$1'" }' \
http://api.github.localhost/orgs/github/repos
}