Skip to content

Commit

Permalink
Control comment visibility with to/cc headers
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Nov 16, 2023
1 parent 6d8642c commit dc0f653
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
27 changes: 23 additions & 4 deletions jiramail/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ def command_issue_assign(issue: jira.resources.Issue,


def command_issue_comment(issue: jira.resources.Issue,
text: str) -> jira.resources.Issue | jiramail.Error:
text: str,
visibility: Optional[Dict[str, str]]=None) -> jira.resources.Issue | jiramail.Error:
try:
if not dry_run:
jiramail.jserv.jira.add_comment(issue, text)
jiramail.jserv.jira.add_comment(issue=issue, body=text,
visibility=visibility)
else:
logger.critical("add comment to %s: %s", issue.key, text.encode())
logger.critical("add comment to %s (visibility=%s): %s",
issue.key, visibility, text.encode())

except jira.exceptions.JIRAError as e:
return jiramail.Error(f"unable to add comment to issue {issue.key}: {e}")
Expand Down Expand Up @@ -283,6 +286,19 @@ def find_issue_key(mail: email.message.Message) -> Any:
return ""


def find_visibility(mail: email.message.Message) -> Optional[Dict[str, str]]:
to_list = mail.get_all("To", [])
cc_list = mail.get_all("Cc", [])

for val in email.utils.getaddresses(to_list + cc_list):
m = re.match(r'^(?P<type>[^@]+)@visible.comment.jira', val[1])
if not m:
continue
return {m.group('type'): val[0]}

return None


def command_issue(mail: email.message.Message,
content: List[str],
args: List[str]) -> None | jiramail.Error:
Expand Down Expand Up @@ -530,6 +546,8 @@ def comment_mail(mail: email.message.Message) -> bool:
logger.critical("issue number not found. Maybe it's because you don't reply to the generated email.")
return False

visibility = find_visibility(mail)

try:
issue = jiramail.jserv.jira.issue(key)
except jira.exceptions.JIRAError as e:
Expand All @@ -540,7 +558,8 @@ def comment_mail(mail: email.message.Message) -> bool:
if part.get_content_type() != "text/plain":
continue

res = command_issue_comment(issue, part.get_payload())
res = command_issue_comment(issue, part.get_payload(),
visibility=visibility)

if isinstance(res, jiramail.Error):
logger.critical("%s", res.message)
Expand Down
9 changes: 7 additions & 2 deletions jiramail/mbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ def comment_email(issue: jira.resources.Issue, comment: jira.resources.Comment,
msg_id = f"<{issue.id}-{comment.id}@comment.issue.jira>"
parent_id = f"<v1-{issue.id}@issue.jira>"
subject.action = "C:"
reply_to = [("Add a comment", "comment@jira")]

mail.add_header("Subject", str(subject))
mail.add_header("Date", get_date(date))
mail.add_header("From", str(author))
mail.add_header("Message-Id", msg_id)
mail.add_header("Reply-To", "change@jira")
mail.add_header("In-Reply-To", parent_id)
mail.add_header("References", f"{parent_id} {msg_id}")
mail.add_header("X-Jiramail-Issue-Id", f"{issue.id}")
Expand All @@ -347,8 +347,13 @@ def comment_email(issue: jira.resources.Issue, comment: jira.resources.Comment,
body = []

if hasattr(comment, "visibility") and hasattr(comment.visibility, "value"):
body.append(f"[Visible only to {comment.visibility.value}]")
body.append(f"[Visible only to {comment.visibility.value} ({comment.visibility.type})]")
body.append("---")
mail.add_header("Cc", email.utils.formataddr(
(f"{comment.visibility.value}",
f"{comment.visibility.type}@visible.comment.jira")))

mail.add_header("Reply-To", ", ".join([ email.utils.formataddr(x) for x in reply_to ]))

body += decode_markdown(message)

Expand Down

0 comments on commit dc0f653

Please sign in to comment.