@@ -132,6 +132,43 @@ def push_update_success_as_discord_embed(
132132 except Exception :
133133 logger .exception ("push_update_success_as_discord_embed had a bad time" )
134134
135+ def push_skipped_update_as_discord_embed (
136+ repo_config : RepoToWatch , current_branch : str , development : bool
137+ ):
138+ repo_name = repo_config .name
139+ # default yellow
140+ color = 0xFFFF00
141+ if development :
142+ prefix = "[development mode]"
143+ repo_name = prefix + " " + repo_name
144+ # do a gray color if we are sending "not real" embeds
145+ color = 0x99AAB5
146+
147+ embed_json = {
148+ "embeds" : [
149+ {
150+ "title" : f"{ repo_name } update skipped due to branch mismatch" ,
151+ "url" : "https://github.com/SCE-Development/"
152+ + repo_config .name , # link to CICD project repo
153+ "description" : f"{ repo_config .branch } was pushed to, but the current branch is { current_branch } ." ,
154+ "color" : color ,
155+ }
156+ ]
157+ }
158+ try :
159+ discord_webhook = requests .post (
160+ str (os .getenv ("CICD_DISCORD_WEBHOOK_URL" )),
161+ json = embed_json ,
162+ )
163+ if discord_webhook .status_code in (200 , 204 ):
164+ return logger .info (f"Discord webhook response: { discord_webhook .text } " )
165+
166+ logger .error (
167+ f"Discord webhook returned status code: { discord_webhook .status_code } with text { discord_webhook .text } "
168+ )
169+ except Exception :
170+ logger .exception ("push_skipped_update_as_discord_embed had a bad time" )
171+
135172
136173def update_repo (repo_config : RepoToWatch ) -> RepoUpdateResult :
137174 MetricsHandler .last_push_timestamp .labels (repo = repo_config .name ).set (time .time ())
@@ -203,8 +240,29 @@ async def github_webhook(request: Request):
203240 return {"status" : f"not acting on repo and branch name of { key } " }
204241
205242 logger .info (f"Push to { branch } detected for { repo_name } " )
206- # update the repo
207- thread = threading .Thread (target = update_repo , args = (config [key ],))
243+
244+ repo_config = config [key ]
245+
246+ # we skip the update if the branch we are on
247+ # does not match the branch we are listening to
248+ if args .development :
249+ logger .warning ("assume we are on main branch, we are in development mode" )
250+ current_branch = "main"
251+ else :
252+ current_branch_result = subprocess .run (
253+ ["git" , "branch" , "--show-current" ],
254+ cwd = repo_config .path ,
255+ capture_output = True ,
256+ text = True ,
257+ )
258+ current_branch = current_branch_result .stdout .strip ()
259+
260+ if current_branch != branch :
261+ logger .warning (f"current branch { current_branch } does not match updated branch { branch } on repo { repo_name } " )
262+ push_skipped_update_as_discord_embed (repo_config , current_branch , args .development )
263+ return {"status" : f"current branch { current_branch } does not match updated branch { branch } on repo { repo_name } " }
264+
265+ thread = threading .Thread (target = update_repo , args = (repo_config ,))
208266 thread .start ()
209267
210268 return {"status" : "webhook received" }
0 commit comments