-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonalToken.py
More file actions
60 lines (51 loc) · 1.56 KB
/
personalToken.py
File metadata and controls
60 lines (51 loc) · 1.56 KB
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
import jwt
import time
import sys
import requests
import base64
import json
import os
with open("testproject.zip", "rb") as zipfile:
encoded_string = base64.b64encode(zipfile.read())
pem = "privateKey.pem"
# Open PEM
with open(pem, 'rb') as pem_file:
signing_key = jwt.jwk_from_pem(pem_file.read())
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': os.getenv('GH_APP_ID')
}
# Create JWT
jwt_instance = jwt.JWT()
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
# set scopes
req = requests.post("https://api.github.com/app/installations/" + os.getenv('GH_APP_INSTALLATION_ID') + "/access_tokens", headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + encoded_jwt,
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "multipart/form-data"
},
json={
"repository": os.getenv('REPO'),
"permissions": {
"issues": 'write',
"contents": 'write'
}
})
print(req.json())
# create file
filename = "test_project.zip"
req = requests.put("https://api.github.com/repos/" + os.getenv('ORGANIZATION') + "/" + os.getenv("REPO") + "/contents/" + filename, headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + encoded_jwt,
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "multipart/form-data"
}, json={
"message": "test_python",
"content": encoded_string.decode('utf-8')
})
print(req.json())