From f2d46d88fa2f24cd53bfa7e7069878a074ddcd16 Mon Sep 17 00:00:00 2001 From: Hugh Saunders Date: Thu, 12 Mar 2020 17:08:07 +0000 Subject: [PATCH] Allow github issues to be created with a label Related: conjurinc/ops#568 --- github/lib | 14 +++++++++++++- tests-for-this-repo/github.bats | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/github/lib b/github/lib index fcdce90..f85ae12 100644 --- a/github/lib +++ b/github/lib @@ -85,6 +85,8 @@ function bl_hub_add_issue_comment(){ function bl_hub_comment_or_create_issue(){ local title="${1}" local message="${2}" + local label="${3:-}" + local label_param="" local issue_number local issue_url local action @@ -96,10 +98,20 @@ function bl_hub_comment_or_create_issue(){ if [[ -z "${issue_number}" ]]; then action="created" + if [[ -n "${label}" ]]; then + label_param="-l ${label}" + fi + # issue doesn't exist create it + + # The following prevents a shellcheck warning about label_param + # getting split into multiple words. That is exactly what should + # happen as "-l" and "labelname" should be separate tokens for + # the hub command. + # shellcheck disable=SC2086 issue_url="$(hub issue create -m "${title} -${message}")" +${message}" ${label_param})" # Example issue url: https://github.com/{owner}/{repo}/issues/{issue number}" # To find the issue number, split on / and take the last field diff --git a/tests-for-this-repo/github.bats b/tests-for-this-repo/github.bats index 66a6c51..f415230 100644 --- a/tests-for-this-repo/github.bats +++ b/tests-for-this-repo/github.bats @@ -164,7 +164,7 @@ EOF @test "bl_hub_comment_or_create_issue creates issue when it doesnt exist" { bl_hub_check(){ :; } - bl_hub_ssue_number_for_title(){ :; } + bl_hub_issue_number_for_title(){ :; } bl_github_owner_repo(){ echo "owner/repo"; } hub(){ if [[ "${2:-}" == "create" ]]; then @@ -195,3 +195,18 @@ EOF assert_output --partial "comment" assert_output --partial "https://github.com/owner/repo/issues/1" } + +@test "bl_hub_comment_or_create_issue passes label to hub" { + bl_hub_check(){ :; } + bl_hub_issue_number_for_title(){ :; } + bl_github_owner_repo(){ echo "owner/repo"; } + hub(){ + [[ "${5}" == "-l" ]] || bl_die "expected -l for specifying a label" + [[ "${6}" == "label" ]] || bl_die "expected 'label' as label name" + echo "https://github.com/owner/repo/issues/1" + } + run bl_hub_comment_or_create_issue title message label + assert_success + assert_output --partial "https://github.com/owner/repo/issues/1" + assert_output --partial "create" +}