2222
2323import requests
2424from gcloud import storage
25+ from github3 import login
2526
2627logging .getLogger ().setLevel (logging .INFO )
2728
9899 'gz' ,
99100]
100101
101- session = requests .Session ()
102+ gh = login (token = ACCESS_TOKEN )
103+ repository = gh .repository (REPO_OWNER , REPO_NAME )
104+
102105
103106def create_status_report_html (artifacts ):
104107 """
@@ -122,22 +125,17 @@ def create_github_status(report_url):
122125 Create a github status with a link to the report URL,
123126 only do this once buildkite has been successful, so only report success here.
124127 """
125- url = 'https://api.github.com/repos/{owner}/{repo}/statuses/{commit}' .format (
126- owner = REPO_OWNER ,
127- repo = REPO_NAME ,
128- commit = COMMIT
128+ status = repository .create_status (
129+ COMMIT ,
130+ "success" ,
131+ target_url = report_url ,
132+ description = "Kolibri Buildkite assets" ,
133+ context = "buildkite/kolibri/assets"
129134 )
130- payload = {
131- "state" : "success" ,
132- "target_url" : report_url ,
133- "description" : "Kolibri Buildkite assets" ,
134- "context" : "buildkite/kolibri/assets"
135- }
136- r = session .post (url , json = payload , headers = headers )
137- if r .status_code == 201 :
138- logging .info ('Successfully created Github status(%s).' % url )
135+ if status :
136+ logging .info ('Successfully created Github status for commit %s.' % COMMIT )
139137 else :
140- logging .info ('Error encounter(%s) . Now exiting!' % r . status_code )
138+ logging .info ('Error encounter. Now exiting!' )
141139 sys .exit (1 )
142140
143141
@@ -195,6 +193,7 @@ def upload_artifacts():
195193
196194 if TAG :
197195 # Building from a tag, this is probably a release!
196+ # Have to do this with requests because github3 does not support this interface yet
198197 get_release_asset_url = requests .get ("https://api.github.com/repos/{owner}/{repo}/releases/tags/{tag}" .format (
199198 owner = REPO_OWNER ,
200199 repo = REPO_NAME ,
@@ -203,21 +202,24 @@ def upload_artifacts():
203202 if get_release_asset_url .status_code == 200 :
204203 # Definitely a release!
205204 release_id = get_release_asset_url .json ()['id' ]
206- url = "https://api.github.com/repos/{owner}/{repo}/releases/{id}/assets" .format (
207- owner = REPO_OWNER ,
208- repo = REPO_NAME ,
209- id = release_id ,
210- )
205+ release_name = get_release_asset_url .json ()['name' ]
206+ release = repository .release (id = release_id )
207+ logging .info ("Uploading built assets to Github Release: %s" % release_name )
211208 for file_extension in file_order :
212209 artifact = artifacts [file_extension ]
213- params = {
214- 'name' : artifact ['name' ],
215- 'label' : artifact ['description' ]
216- }
217- files = {
218- 'file' : (artifact ['name' ], open (artifact ['file_location' ], 'rb' ), artifact ['content_type' ])
219- }
220- session .post (url , params = params , files = files , headers = headers )
210+ logging .info ("Uploading release asset: %s" % (artifact .get ("name" )))
211+ # For some reason github3 does not let us set a label at initial upload
212+ asset = release .upload_asset (
213+ content_type = artifact ['content_type' ],
214+ name = artifact ['name' ],
215+ asset = open (artifact ['file_location' ], 'rb' )
216+ )
217+ if asset :
218+ # So do it after the initial upload instead
219+ asset .edit (artifact ['name' ], label = artifact ['description' ])
220+ logging .info ("Successfully uploaded release asset: %s" % (artifact .get ('name' )))
221+ else :
222+ logging .error ("Error uploading release asset: %s" % (artifact .get ('name' )))
221223
222224
223225def main ():
0 commit comments