Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _run_task(self):
repo = self.get_repo()
for release in repo.iter_releases():
if release.name == self.options['version']:
message = 'Release {} already exists at {}'.format(release.name, release.html_url)
self.logger.error(message)
return GithubException(message)
commit = self.options.get('commit', self.project_config.repo_commit)
if not commit:
message = 'Could not detect the current commit from the local repo'
self.logger.error(message)
return GithubException(message)
version = self.options['version']
self.tag_name = self.project_config.get_tag_for_version(version)
ref = repo.ref('tags/{}'.format(self.tag_name))
if not ref:
# Create the annotated tag
tag = repo.create_tag(
tag = self.tag_name,
try:
release = repo.release_from_tag(tag_name)
except github3.exceptions.NotFoundError:
message = f"Release for {tag_name} not found"
self.logger.error(message)
raise GithubException(message)
# Check for tag in target repo
target_ref = None
try:
target_ref = self.target_repo.ref(f"tags/{tag_name}")
except github3.exceptions.NotFoundError:
pass
else:
message = "Ref for tag {tag_name} already exists in target repo"
raise GithubException(message)
# Create the tag
target_tag = self.target_repo.create_tag(
tag=tag_name,
message=tag.message,
sha=commit,
obj_type="commit",
tagger={
"name": self.github_config.username,
"email": self.github_config.email,
"date": "{}Z".format(datetime.utcnow().isoformat()),
},
lightweight=False,
)
# Create the release
def _create_tree(self, new_tree_list):
new_tree = None
if self.dry_run:
self.logger.info("[dry_run] Skipping creation of new tree")
else:
self.logger.info("Creating new tree")
new_tree = self.repo.create_tree(new_tree_list, None)
if not new_tree:
raise GithubException("Failed to create tree")
return new_tree
if self.dry_run:
self.logger.info(
"[dry_run] Skipping creation of "
+ "blob for new file: {}".format(local_file)
)
blob_sha = None
else:
self.logger.info("Creating blob for new file: {}".format(local_file))
try:
content = content.decode("utf-8")
blob_sha = self.repo.create_blob(content, "utf-8")
except UnicodeDecodeError:
content = base64.b64encode(content)
blob_sha = self.repo.create_blob(content.decode("utf-8"), "base64")
if not blob_sha:
raise GithubException("Failed to create blob")
self.logger.debug("Blob created: {}".format(blob_sha))
return blob_sha
def _validate_dirs(self, local_dir, repo_dir):
local_dir = os.path.abspath(local_dir)
if not os.path.isdir(local_dir):
raise GithubException("Not a dir: {}".format(local_dir))
# do not use os.path because repo_dir is not local
if repo_dir is None:
repo_dir = ""
if repo_dir.startswith("."):
repo_dir = repo_dir[1:]
if repo_dir.startswith("/"):
repo_dir = repo_dir[1:]
if repo_dir.endswith("/"):
repo_dir = repo_dir[:-1]
return local_dir, repo_dir
def _update_head(self, new_commit):
if self.dry_run:
self.logger.info("[dry_run] Skipping call to update HEAD")
else:
self.logger.info("Updating HEAD")
success = self.head.update(new_commit.sha)
if not success:
raise GithubException("Failed to update HEAD")
def _create_release(self, path, commit):
# Get current release info
tag_name = self.project_config.get_tag_for_version(self.options["version"])
repo = self.get_repo()
# Get the ref
try:
ref = repo.ref(f"tags/{tag_name}")
except github3.exceptions.NotFoundError:
message = f"Ref not found for tag {tag_name}"
self.logger.error(message)
raise GithubException(message)
# Get the tag
try:
tag = repo.tag(ref.object.sha)
except github3.exceptions.NotFoundError:
message = f"Tag {tag_name} not found"
self.logger.error(message)
raise GithubException(message)
# Get the release
try:
release = repo.release_from_tag(tag_name)
except github3.exceptions.NotFoundError:
message = f"Release for {tag_name} not found"
self.logger.error(message)
raise GithubException(message)
# Check for tag in target repo
version = self.options["version"]
self.tag_name = self.project_config.get_tag_for_version(version)
for release in repo.iter_releases():
if release.tag_name == self.tag_name:
message = "Release {} already exists at {}".format(
release.name, release.html_url
)
self.logger.error(message)
raise GithubException(message)
commit = self.options.get("commit", self.project_config.repo_commit)
if not commit:
message = "Could not detect the current commit from the local repo"
self.logger.error(message)
raise GithubException(message)
ref = repo.ref("tags/{}".format(self.tag_name))
if not ref:
# Create the annotated tag
tag = repo.create_tag(
tag=self.tag_name,
message="Release of version {}".format(version),
sha=commit,
obj_type="commit",
tagger={
"name": self.github_config.username,
"email": self.github_config.email,
"date": "{}Z".format(datetime.utcnow().isoformat()),
},
lightweight=False,