-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (72 loc) · 2.39 KB
/
main.py
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
import os
import jwt;
import datetime
import sys
import time
import requests
class UploadTestFlightReleaseNotes:
def generateToken(self, issuer_id, key_id, private_key):
current_time = datetime.datetime.now(datetime.timezone.utc)
unix_timestamp = current_time.timestamp()
exp = unix_timestamp_plus_5_min = unix_timestamp + (10 * 60) # 10 min * 60 seconds (tokens over 20 minutes are not allowed)
iss = unix_timestamp_plus_5_min = unix_timestamp + (-1 * 60) # -1 min * 60 seconds
data = {'aud': 'appstoreconnect-v1',
'iss': issuer_id,
'exp': exp,
'iat': iss}
headers = {'kid': key_id}
encoded_token = jwt.encode(data, private_key, algorithm='ES256', headers=headers)
return encoded_token
def uploadNotes(self, app_id, token, whats_new, build_number):
# Create Header
HEAD = {
'Authorization': 'Bearer ' + token
}
# URLS
BASE_URL = 'https://api.appstoreconnect.apple.com/v1/'
# Find builds
versionId = ""
while versionId == "":
print("---Finding Build---")
URL = BASE_URL + 'builds?filter[app]=' + app_id + '&filter[version]=' + build_number
r = requests.get(URL, params={}, headers=HEAD)
try:
versionId = r.json()['data'][0]['id']
except:
time.sleep(60) #wait for 60 seconds
# Find localizations
localizationId = ""
while localizationId == "":
print("---Finding Localizations---")
URL = BASE_URL + 'builds/' + versionId + '/betaBuildLocalizations'
r = requests.get(URL, params={}, headers=HEAD)
try:
localizationId = r.json()['data'][0]['id']
except:
time.sleep(60) #wait for 60 seconds
print("---Update What's New---")
URL = BASE_URL + 'betaBuildLocalizations/' + localizationId
data = {
"data": {
"id": localizationId,
"type": "betaBuildLocalizations",
"attributes": {
"whatsNew": whats_new[:4000] #4000 char limit
}
}
}
result = requests.patch(URL, json=data, headers=HEAD)
return result.reason
def main():
issuer_id = os.getenv('ISSUER_ID')
key_id = os.getenv('KEY_ID')
private_key = os.getenv('PRIVATE_KEY')
app_id = os.getenv('APP_ID')
whats_new = os.getenv('WHATS_NEW')
build_number = os.getenv('BUILD_NUMBER')
service = UploadTestFlightReleaseNotes()
token = service.generateToken(issuer_id, key_id, private_key)
reason = service.uploadNotes(app_id, token, whats_new, build_number)
print(reason)
if __name__ == "__main__":
main()