Skip to content

Commit

Permalink
Removed personalization of issue label name
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-medici committed Dec 17, 2024
1 parent 9557be7 commit bc7a7b2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 53 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,11 @@ settings:
# (Optional) (Default: false) Add a new comment in GitHub with a link to Jira created issue
add_gh_comment: false

# (Optional) (Default: false) Add a label with name <gh_synced_label_name> to newly created
# issues when a corresponding ticket is created in Jira. This is meant to replace the comment
# depending on project preferences, but comments will still be added if <add_gh_comment> is true.
# (Optional) (Default: false) Add a 'synced-to-jira' label to newly created issues when a corresponding
# ticket is created in Jira. This is meant to replace the comment depending on project preferences,
# but comments will still be added if <add_gh_comment> is true.
# The 'synced-to-jira' label must already be present in the GitHub repo and will not be created by the bot.
add_gh_synced_label: false

# (Optional) (Default: None) The name of the label to add to GitHub issues. Ignored if
# <add_gh_synced_label> is false. A label with this name must already be present in the
# repository. If <add_gh_comment> is true, misconfigurations such as the label not existing, or
# <gh_synced_label_name> being unset when <add_gh_synced_label> is true will be reported as a
# warning in the comment under the GitHub issue
gh_synced_label_name: None

# (Optional) (Default: true) Synchronize issue description from GitHub to Jira
sync_description: true
Expand Down
41 changes: 17 additions & 24 deletions github_jira_sync_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
> This message was autogenerated
"""

gh_synced_label_name = "synced-to-jira"

nonexistent_gh_label_warning = f"'{gh_synced_label_name}' label doesn't exist in the GitHub repo."


def define_logger():
"""Define logger to output to the file and to STDOUT."""
Expand Down Expand Up @@ -265,42 +269,31 @@ async def bot(request: Request, payload: dict = Body(...)):
new_issue = jira.create_issue(fields=issue_dict)
existing_issues.append(new_issue)

misconfigured_gh_label_reason = ""
synced_label_absent_in_repo = False

if settings["gh_synced_label_name"]:
gh_synced_label_name = settings["gh_synced_label_name"]

if gh_synced_label_name:
try:
repo.get_label(gh_synced_label_name)
issue.add_to_labels(gh_synced_label_name)
except UnknownObjectException:
misconfigured_gh_label_reason = (
" Warning: the specified GitHub label doesn't exist in the repo."
)
else:
misconfigured_gh_label_reason = (
" Warning: option gh_synced_label_name has not been set while"
"gh_synced_label_name has."
)
if settings["add_gh_synced_label"]:
try:
repo.get_label(gh_synced_label_name)
issue.add_to_labels(gh_synced_label_name)
except UnknownObjectException:
logger.warning(nonexistent_gh_label_warning)
synced_label_absent_in_repo = True

if settings["add_gh_comment"]:
gh_comment_body = gh_comment_body_template.format(jira_issue_link=new_issue.permalink())

if misconfigured_gh_label_reason:
gh_comment_body += "\n\n" + misconfigured_gh_label_reason
if synced_label_absent_in_repo:
gh_comment_body += "\n\nWarning: " + nonexistent_gh_label_warning

issue.create_comment(gh_comment_body)

# need this since we allow to sync issue on many actions. And if someone commented
# we first create a Jira issue, then create a comment
msg = "Issue was created in Jira. "

if misconfigured_gh_label_reason:
msg += (
"Comment added to GitHub issue because gh_synced_label_name "
"is not set, or does not exist in repo."
)
# Add warning to response in case of label setting misconfiguration
if synced_label_absent_in_repo:
msg += "Warning: " + nonexistent_gh_label_warning
else:
jira_issue = existing_issues[0]
if payload["action"] == "closed":
Expand Down
1 change: 0 additions & 1 deletion github_jira_sync_app/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ settings:
labels:
add_gh_comment: false
add_gh_synced_label: false
gh_synced_label_name:
sync_description: true
sync_comments: true
epic_key:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/dumm_env
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ GITHUB_CLIENT_SECRET=rezindkm3dod04h12yt8oktm8if51wtsfgljk04k
JIRA_INSTANCE="https://my-jira.atlassian.net"
[email protected]
JIRA_TOKEN=mv38swy07r6ius3v90cffyi4
DEFAULT_BOT_CONFIG='{"settings": {"components": null, "labels": ["jira"], "add_gh_comment": false, "add_gh_synced_label": false, "gh_synced_label_name": null, "sync_description": true, "sync_comments": true, "epic_key": null, "jira_project_key": null, "label_mapping": null, "status_mapping": null}}'
DEFAULT_BOT_CONFIG='{"settings": {"components": null, "labels": ["jira"], "add_gh_comment": false, "add_gh_synced_label": false, "sync_description": true, "sync_comments": true, "epic_key": null, "jira_project_key": null, "label_mapping": null, "status_mapping": null}}'
BOT_NAME="syncronize-issues-to-jira[bot]"
15 changes: 7 additions & 8 deletions tests/unit/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def test_issue_created_and_sync_label_added(signature_mock):
5. Authenticate in Jira
6. Validate via JQL that this issue does not exist in Jira
7. Create new issue in Jira
8. Validate that <gh_synced_label_name> label exists in repo
9. Add <gh_synced_label_name> label to the issue
8. Validate that synced-to-jira label exists in repo
9. Add synced-to-jira label to the issue
"""
responses._add_from_file(
UNITTESTS_DIR / "url_responses" / "auth_github_responses_sync_label.yaml"
Expand All @@ -207,8 +207,8 @@ def test_issue_created_and_sync_label_added(signature_mock):

@responses.activate(assert_all_requests_are_fired=True)
def test_issue_created_and_sync_label_not_present(signature_mock):
"""Test when a bug is created on GitHub with the right label but <add_gh_synced_label> does not
exist in the repository
"""Test when a bug is created on GitHub with the right label and <add_gh_synced_label> is set
but the 'synced-to-jira' label does not exist in the repository
Tests the following scenario:
1. Authenticate in GitHub
Expand All @@ -218,7 +218,7 @@ def test_issue_created_and_sync_label_not_present(signature_mock):
5. Authenticate in Jira
6. Validate via JQL that this issue does not exist in Jira
7. Create new issue in Jira
8. Try and validate that <gh_synced_label_name> label exists in repo
8. Try and validate that synced-to-jira label exists in repo
9. Label doesn't exist, nothing is done on the GitHub issue
"""
responses._add_from_file(
Expand All @@ -234,7 +234,6 @@ def test_issue_created_and_sync_label_not_present(signature_mock):

assert response.status_code == 200
assert response.json() == {
"msg": "Issue was created in Jira. Comment added to GitHub issue "
"because gh_synced_label_name is not set, or does not exist"
" in repo."
"msg": "Issue was created in Jira. "
"Warning: 'synced-to-jira' label doesn't exist in the GitHub repo."
}
12 changes: 6 additions & 6 deletions tests/unit/url_responses/auth_github_responses_sync_label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ responses:
# get content of .jira_sync_config.yaml to load the settings for the repo
- response:
auto_calculate_content_length: false
body: '{"name":".jira_sync_config.yaml","path":".github/.jira_sync_config.yaml","sha":"a1a8aab6de92fc3eed4211350200ada4a2405b9c","size":297,"url":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","html_url":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml","git_url":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","download_url":"https://raw.githubusercontent.com/beliaev-maksim/test-ci/master/.github/.jira_sync_config.yaml","type":"file","content":"c2V0dGluZ3M6CiAgY29tcG9uZW50czoKICAgIC0gSW9UCiAgICAtIERBQ0ggVFQKICBsYWJlbHM6CiAgICAtIGJ1ZwogICAgLSBjdXN0b20KICBhZGRfZ2hfY29tbWVudDogZmFsc2UKICBhZGRfZ2hfc3luY2VkX2xhYmVsOiB0cnVlCiAgZ2hfc3luY2VkX2xhYmVsX25hbWU6IFN5bmNlZAogIHN5bmNfZGVzY3JpcHRpb246IHRydWUKICBzeW5jX2NvbW1lbnRzOiB0cnVlCiAgZXBpY19rZXk6ICJNVEMtMjk2IgogIGppcmFfcHJvamVjdF9rZXk6ICJNVEMiCiAgc3RhdHVzX21hcHBpbmc6CiAgICBvcGVuZWQ6IFVudHJpYWdlZAogICAgY2xvc2VkOiBkb25lIAogIGxhYmVsX21hcHBpbmc6CiAgICBlbmhhbmNlbWVudDogU3RvcnkK","encoding":"base64","_links":{"self":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","git":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","html":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml"}}'
body: '{"name":".jira_sync_config.yaml","path":".github/.jira_sync_config.yaml","sha":"a1a8aab6de92fc3eed4211350200ada4a2405b9c","size":297,"url":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","html_url":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml","git_url":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","download_url":"https://raw.githubusercontent.com/beliaev-maksim/test-ci/master/.github/.jira_sync_config.yaml","type":"file","content":"c2V0dGluZ3M6CiAgY29tcG9uZW50czoKICAgIC0gSW9UCiAgICAtIERBQ0ggVFQKICBsYWJlbHM6CiAgICAtIGJ1ZwogICAgLSBjdXN0b20KICBhZGRfZ2hfY29tbWVudDogZmFsc2UKICBhZGRfZ2hfc3luY2VkX2xhYmVsOiB0cnVlCiAgc3luY19kZXNjcmlwdGlvbjogdHJ1ZQogIHN5bmNfY29tbWVudHM6IHRydWUKICBlcGljX2tleTogIk1UQy0yOTYiCiAgamlyYV9wcm9qZWN0X2tleTogIk1UQyIKICBzdGF0dXNfbWFwcGluZzoKICAgIG9wZW5lZDogVW50cmlhZ2VkCiAgICBjbG9zZWQ6IGRvbmUgCiAgbGFiZWxfbWFwcGluZzoKICAgIGVuaGFuY2VtZW50OiBTdG9yeQo=","encoding":"base64","_links":{"self":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","git":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","html":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml"}}'
content_type: text/plain
method: GET
status: 200
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml

# get the <gh_synced_label_name> label from the repo
# get the synced-to-jira label from the repo
- response:
auto_calculate_content_length: false
body: '{"id":208045946,"node_id":"MDU6TGFiZWwyMDgwNDU5NDY=","url":"https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/Synced","name":"Synced","description":"TicketinJirahasbeencreatedforthisissue","color":"f29513","default":false}'
body: '{"id":208045946,"node_id":"MDU6TGFiZWwyMDgwNDU5NDY=","url":"https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/synced-to-jira","name":"synced-to-jira","description":"TicketinJirahasbeencreatedforthisissue","color":"f29513","default":false}'
content_type: text/plain
method: GET
status: 200
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/Synced
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/synced-to-jira

# add the <gh_synced_label_name> label to the new issue
# add the synced-to-jira label to the new issue
- response:
auto_calculate_content_length: false
body: '[{"id":208045946,"node_id":"MDU6TGFiZWwyMDgwNDU5NDY=","url":"https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/Synced","name":"Synced","description":"TicketinJirahasbeencreatedforthisissue","color":"f29513","default":false}]'
body: '[{"id":208045946,"node_id":"MDU6TGFiZWwyMDgwNDU5NDY=","url":"https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/synced-to-jira","name":"synced-to-jira","description":"TicketinJirahasbeencreatedforthisissue","color":"f29513","default":false}]'
content_type: text/plain
method: POST
status: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ responses:
# get content of .jira_sync_config.yaml to load the settings for the repo
- response:
auto_calculate_content_length: false
body: '{"name":".jira_sync_config.yaml","path":".github/.jira_sync_config.yaml","sha":"a1a8aab6de92fc3eed4211350200ada4a2405b9c","size":297,"url":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","html_url":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml","git_url":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","download_url":"https://raw.githubusercontent.com/beliaev-maksim/test-ci/master/.github/.jira_sync_config.yaml","type":"file","content":"c2V0dGluZ3M6CiAgY29tcG9uZW50czoKICAgIC0gSW9UCiAgICAtIERBQ0ggVFQKICBsYWJlbHM6CiAgICAtIGJ1ZwogICAgLSBjdXN0b20KICBhZGRfZ2hfY29tbWVudDogZmFsc2UKICBhZGRfZ2hfc3luY2VkX2xhYmVsOiB0cnVlCiAgZ2hfc3luY2VkX2xhYmVsX25hbWU6IFN5bmNlZAogIHN5bmNfZGVzY3JpcHRpb246IHRydWUKICBzeW5jX2NvbW1lbnRzOiB0cnVlCiAgZXBpY19rZXk6ICJNVEMtMjk2IgogIGppcmFfcHJvamVjdF9rZXk6ICJNVEMiCiAgc3RhdHVzX21hcHBpbmc6CiAgICBvcGVuZWQ6IFVudHJpYWdlZAogICAgY2xvc2VkOiBkb25lIAogIGxhYmVsX21hcHBpbmc6CiAgICBlbmhhbmNlbWVudDogU3RvcnkK","encoding":"base64","_links":{"self":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","git":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","html":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml"}}'
body: '{"name":".jira_sync_config.yaml","path":".github/.jira_sync_config.yaml","sha":"a1a8aab6de92fc3eed4211350200ada4a2405b9c","size":297,"url":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","html_url":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml","git_url":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","download_url":"https://raw.githubusercontent.com/beliaev-maksim/test-ci/master/.github/.jira_sync_config.yaml","type":"file","content":"c2V0dGluZ3M6CiAgY29tcG9uZW50czoKICAgIC0gSW9UCiAgICAtIERBQ0ggVFQKICBsYWJlbHM6CiAgICAtIGJ1ZwogICAgLSBjdXN0b20KICBhZGRfZ2hfY29tbWVudDogZmFsc2UKICBhZGRfZ2hfc3luY2VkX2xhYmVsOiB0cnVlCiAgc3luY19kZXNjcmlwdGlvbjogdHJ1ZQogIHN5bmNfY29tbWVudHM6IHRydWUKICBlcGljX2tleTogIk1UQy0yOTYiCiAgamlyYV9wcm9qZWN0X2tleTogIk1UQyIKICBzdGF0dXNfbWFwcGluZzoKICAgIG9wZW5lZDogVW50cmlhZ2VkCiAgICBjbG9zZWQ6IGRvbmUgCiAgbGFiZWxfbWFwcGluZzoKICAgIGVuaGFuY2VtZW50OiBTdG9yeQo=","encoding":"base64","_links":{"self":"https://api.github.com/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml?ref=master","git":"https://api.github.com/repos/beliaev-maksim/test-ci/git/blobs/a1a8aab6de92fc3eed4211350200ada4a2405b9c","html":"https://github.com/beliaev-maksim/test-ci/blob/master/.github/.jira_sync_config.yaml"}}'
content_type: text/plain
method: GET
status: 200
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/contents/.github/.jira_sync_config.yaml

# get the <gh_synced_label_name> label from the repo
# get the (nonexistent) synced-to-jira label from the repo
- response:
auto_calculate_content_length: false
body: '{"message": "Not Found", "documentation_url": "https://docs.github.com/rest/issues/labels#get-a-label", "status": "404"}'
content_type: text/plain
method: GET
status: 404
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/Synced
url: https://api.github.com:443/repos/beliaev-maksim/test-ci/labels/synced-to-jira

0 comments on commit bc7a7b2

Please sign in to comment.