-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_job.py
135 lines (90 loc) · 3.84 KB
/
trigger_job.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
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
#!/usr/bin/env python3
# import requests
import jwt
import time
import sys
import json
import urllib.request
def executeJob(token, filename, jobid):
# params = sys.argv[1] # one liner null or empty string if nothing
headers = {
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}'
}
payload = {"ref": "main", "inputs": {"artifact":f'{filename}'}} #breaks if you give unexpected inputs? "name": "William", "home": "Knoxville, TN"
url = f'https://api.github.com/repos/geniesinc/runner-poc/actions/workflows/{jobid}/dispatches'
req = urllib.request.Request(url, headers=headers, data=json.dumps(payload).encode('utf-8'), method='POST')
try:
with urllib.request.urlopen(req) as response:
print(str(response.status))
except urllib.error.HTTPError as e:
print(str(e.code))
print(e.read())
def getInstallationToken(jwt_token, githubAppNumber):
headers = {
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {jwt_token}'
}
url = f'https://api.github.com/app/installations/{githubAppNumber}/access_tokens'
req = urllib.request.Request(url, headers=headers, method='POST')
with urllib.request.urlopen(req) as response:
respJson = json.loads(response.read().decode('utf-8'))
return respJson['token']
def getJWT():
pem = "./genies-art-runners.2023-07-17.private-key.pem"
app_id = "360400"
# 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': app_id
}
# Create JWT
jwt_instance = jwt.JWT()
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
return encoded_jwt
def lambda_handler(event, context):
# Parse the S3 event data
print("Received event: " + json.dumps(event, indent=2))
# Extract the bucket name and object key
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
object_key = event["Records"][0]["s3"]["object"]["key"]
# Extract the file name from the object key
filename = object_key.split("/")[-1]
# Print the file info
print(f"Object key: {object_key}")
jobid = "62953910"
githubAppNumber = "39576324"
jwt = getJWT()
token = getInstallationToken(jwt,githubAppNumber)
executeJob(token,filename,jobid)
return {
"statusCode": 200,
"body": "Lambda function executed successfully"
}
# if __name__ == '__main__':
# jobid = "62953910"
# githubAppNumber = "39576324"
# jwt = getJWT()
# token = getInstallationToken(jwt)
# executeJob(token)
#if we want to use the requests library, uncomment these.
#We currently are using all python native libraries in this file.
# def executeJob(token):
# params = sys.argv[1] #one liner null or empty string if nothing
# headers = {'X-GitHub-Api-Version': '2022-11-28','Accept': 'application/vnd.github+json','Authorization':f'Bearer {token}'}
# payload = {"ref":"main","inputs":{"name":"William","home":"Knoxville, TN"}}
# r = requests.post(f'https://api.github.com/repos/geniesinc/runner-poc/actions/workflows/{jobid}/dispatches', headers=headers, data=json.dumps(payload))
# print(str(r.status_code))
# def getInstallationToken(jwt):
# headers = {'X-GitHub-Api-Version': '2022-11-28','Accept': 'application/vnd.github+json','Authorization':f'Bearer {jwt}'}
# r = requests.post(f'https://api.github.com/app/installations/{githubAppNumber}/access_tokens', headers=headers)
# respJson = r.json()
# return respJson.token