Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add delete of existing branch #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions gh-code-scanning
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,28 @@ class GitHeadRef:
def branch(self, new_branch_name):
"""Create a new branch from the HEAD of this branch."""
log.info('%s: Creating branch "%s"', self.repository, new_branch_name)
self.repository.api(
f'repos/{self.repository}/git/refs',
method='POST',
params={'ref': f'refs/heads/{new_branch_name}', 'sha': self.head()},
)
try:
self.repository.api(
f'repos/{self.repository}/git/refs',
method='POST',
params={'ref': f'refs/heads/{new_branch_name}', 'sha': self.head()},
)
except GithubError as error:
if error.http_status == 422:
# Try again, but delete the branch first
self.repository.api(
f'repos/{self.repository}/git/refs/heads/{new_branch_name}',
method='DELETE',
params={'ref': f'refs/heads/{new_branch_name}', 'sha': self.head()},
)
self.repository.api(
f'repos/{self.repository}/git/refs',
method='POST',
params={'ref': f'refs/heads/{new_branch_name}', 'sha': self.head()},
)

else:
raise
return GitHeadRef(new_branch_name, self.repository)

def create_file(self, name, content):
Expand Down Expand Up @@ -195,7 +212,7 @@ class GithubRepository:

if raw_output:
return buffer.read()
else:
elif method != 'DELETE':
return json.load(buffer)

def analyses(self, ref=None):
Expand Down