From b20acf9245c906382baf216b53fe38e21156350a Mon Sep 17 00:00:00 2001 From: Ganeshwara Hananda Date: Mon, 23 Nov 2020 18:43:06 +0000 Subject: [PATCH] Adds a parameter in deploy_github that allows for creating both a draft as well as a published release (#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 --- github/rules.bzl | 42 +++++++++++++++++++++++--------------- github/templates/deploy.py | 27 ++++++++++++------------ 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/github/rules.bzl b/github/rules.bzl index fbd15d01..2dc979c6 100644 --- a/github/rules.bzl +++ b/github/rules.bzl @@ -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 = [ @@ -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, @@ -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, @@ -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", diff --git a/github/templates/deploy.py b/github/templates/deploy.py index f3b1c3c9..0506c1e1 100644 --- a/github/templates/deploy.py +++ b/github/templates/deploy.py @@ -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() @@ -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: @@ -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)