55from hashlib import sha1 , sha256
66from typing import Optional
77
8+ from django .db .models import Q
89from django .utils import timezone
910from django .utils .crypto import constant_time_compare
1011from rest_framework import status
@@ -239,42 +240,33 @@ def push(self, request, *args, **kwargs):
239240 )
240241 return Response (data = WebhookHandlerErrorMessages .SKIP_WEBHOOK_IGNORED )
241242
242- branch_name = self .request .data .get ("ref" )[11 :]
243+ pushed_to_branch_name = self .request .data .get ("ref" )[11 :]
243244 commits = self .request .data .get ("commits" , [])
244245
245246 if not commits :
246247 log .debug (
247- f"No commits in webhook payload for branch { branch_name } " ,
248+ f"No commits in webhook payload for branch { pushed_to_branch_name } " ,
248249 extra = dict (repoid = repo .repoid , github_webhook_event = self .event ),
249250 )
250251 return Response ()
251252
252- commits_queryset = Commit . objects . filter (
253- repository = repo ,
254- commitid__in = [ commit . get ( "id" ) for commit in commits ] ,
255- merged = False ,
256- )
257- commits_queryset . update ( branch = branch_name )
258- if branch_name == repo . branch :
259- commits_queryset .update (merged = True )
253+ if pushed_to_branch_name == repo . branch :
254+ commits_queryset = Commit . objects . filter (
255+ ~ Q ( branch = pushed_to_branch_name ) ,
256+ repository = repo ,
257+ commitid__in = [ commit . get ( "id" ) for commit in commits ],
258+ merged = False ,
259+ )
260+ commits_queryset .update (branch = pushed_to_branch_name , merged = True )
260261 log .info (
261- "Pushed commits to default branch ; setting merged to True" ,
262+ f"Branch name updated for commits to { pushed_to_branch_name } ; setting merged to True" ,
262263 extra = dict (
263264 repoid = repo .repoid ,
264265 github_webhook_event = self .event ,
265266 commits = [commit .get ("id" ) for commit in commits ],
266267 ),
267268 )
268269
269- log .info (
270- f"Branch name updated for commits to { branch_name } " ,
271- extra = dict (
272- repoid = repo .repoid ,
273- github_webhook_event = self .event ,
274- commits = [commit .get ("id" ) for commit in commits ],
275- ),
276- )
277-
278270 most_recent_commit = commits [- 1 ]
279271
280272 if regexp_ci_skip (most_recent_commit .get ("message" )):
@@ -300,7 +292,7 @@ def push(self, request, *args, **kwargs):
300292 TaskService ().status_set_pending (
301293 repoid = repo .repoid ,
302294 commitid = most_recent_commit .get ("id" ),
303- branch = branch_name ,
295+ branch = pushed_to_branch_name ,
304296 on_a_pull_request = False ,
305297 )
306298
0 commit comments