Skip to content

Commit

Permalink
Adds a parameter in deploy_github that allows for creating both a dra…
Browse files Browse the repository at this point in the history
…ft as well as a published release (vaticle#269)

## What is the goal of this PR?

We have added a `draft` parameter to `deploy_github`. If it is set to `True`, the rule will create a release that is marked as draft. When set to `False`, the release will be immediately published.

This is an improvement of the existing behaviour, in which you can only create a draft release.

## What are the changes implemented in this PR?

- Implement the `draft` field
- Refactor the rule and `deploy.py` template
  • Loading branch information
lolski committed Nov 23, 2020
1 parent 3e8b2a4 commit b20acf9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
42 changes: 25 additions & 17 deletions github/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ def _deploy_github_impl(ctx):
template = ctx.file._deploy_script,
output = _deploy_script,
substitutions = {
"{archive}": ctx.file.archive.short_path if (ctx.file.archive!=None) else "",
"{has_release_description}": str(int(bool(ctx.file.release_description))),
"{ghr_osx_binary}": ctx.files._ghr[0].path,
"{ghr_linux_binary}": ctx.files._ghr[1].path,
"{release_title}": ctx.attr.title or "",
"{title_append_version}": str(int(bool(ctx.attr.title_append_version))),
"{organisation}" : ctx.attr.organisation,
"{repository}" : ctx.attr.repository,
"{title}": ctx.attr.title or "",
"{title_append_version}": str(bool(ctx.attr.title_append_version)),
"{release_description}": str(bool(ctx.file.release_description)),
"{archive}": ctx.file.archive.short_path if (ctx.file.archive!=None) else "",
"{draft}": str(bool(ctx.attr.draft)),
"{ghr_binary_mac}": ctx.files._ghr[0].path,
"{ghr_binary_linux}": ctx.files._ghr[1].path,
}
)
files = [
Expand Down Expand Up @@ -72,10 +73,13 @@ def _deploy_github_impl(ctx):

deploy_github = rule(
attrs = {
"archive": attr.label(
mandatory = False,
allow_single_file = [".zip"],
doc = "`assemble_versioned` label to be deployed.",
"organisation" : attr.string(
mandatory = True,
doc = "Github organisation to deploy to",
),
"repository" : attr.string(
mandatory = True,
doc = "Github repository to deploy to within organisation",
),
"title": attr.string(
mandatory = False,
Expand All @@ -89,13 +93,10 @@ deploy_github = rule(
allow_single_file = True,
doc = "Description of GitHub release"
),
"organisation" : attr.string(
mandatory = True,
doc = "Github organisation to deploy to",
),
"repository" : attr.string(
mandatory = True,
doc = "Github repository to deploy to within organisation",
"archive": attr.label(
mandatory = False,
allow_single_file = [".zip"],
doc = "`assemble_versioned` label to be deployed.",
),
"version_file": attr.label(
allow_single_file = True,
Expand All @@ -105,6 +106,13 @@ deploy_github = rule(
Not specifying version at all defaults to '0.0.0'
"""
),
"draft": attr.bool(
default = True,
doc = """
Creates an unpublished / draft release when set to True.
Defaults to True.
"""
),
"_deploy_script": attr.label(
allow_single_file = True,
default = "//github/templates:deploy.py",
Expand Down
27 changes: 14 additions & 13 deletions github/templates/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@


GHR_BINARIES = {
"Darwin": os.path.abspath("{ghr_osx_binary}"),
"Linux": os.path.abspath("{ghr_linux_binary}"),
"Darwin": os.path.abspath("{ghr_binary_mac}"),
"Linux": os.path.abspath("{ghr_binary_linux}"),
}

system = platform.system()
Expand Down Expand Up @@ -69,14 +69,14 @@ def extract(self, member, path=None, pwd=None):

archive = "{archive}" or args.archive

title = "{release_title}"
title_append_version = bool(int("{title_append_version}"))

has_release_description = bool(int("{has_release_description}"))
github_token = os.getenv('DEPLOY_GITHUB_TOKEN')
target_commit_id = args.commit_id
github_organisation = "{organisation}"
github_repository = "{repository}"
title = "{title}"
title_append_version = {title_append_version}
release_description = {release_description}
draft = {draft}
github_token = os.getenv('DEPLOY_GITHUB_TOKEN')
target_commit_id = args.commit_id
ghr = GHR_BINARIES[system]

with open('VERSION') as version_file:
Expand All @@ -98,16 +98,17 @@ def extract(self, member, path=None, pwd=None):
# satisfied and we're able to proceed

try:
exit_code = sp.call([
cmd = [
ghr,
'-u', github_organisation,
'-r', github_repository,
'-n', title,
'-b', open('release_description.txt').read() if has_release_description else '',
'-b', open('release_description.txt').read() if release_description else '',
'-c', target_commit_id,
'-delete', '-draft', github_tag, # TODO: tag must reference the current commit
directory_to_upload
], env={'GITHUB_TOKEN': github_token})
]
cmd += [ '-delete', '-draft', github_tag ] if draft else [ '-delete', github_tag ]
cmd += [ directory_to_upload ]
exit_code = sp.call(cmd, env={'GITHUB_TOKEN': github_token})
finally:
shutil.rmtree(directory_to_upload)
sys.exit(exit_code)

0 comments on commit b20acf9

Please sign in to comment.