Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Configuration is done via env variables
* `SLACK_BOT_TOKEN` - Slack bot token. Mandatory parameter. scopes: channels:history, chat:write, reactions:read, users:read.email, users:read
* `SLACK_APP_TOKEN` - Slack app token. Mandatory parameter. scopes: connections:write
* `SLACK_CHANNEL_NAME` - Slack channel name. Also channel_id can be used
* `TRIGGERED_BY_EMAIL` - Email of the person who triggered the build, for example by pressing the merge button. Optional parameter. When set, a `Triggered by` line is added to the approval message, mentioning that person if the email matches a Slack profile. Useful because the commit itself does not always point at a person: squash merges on GitHub, for instance, are committed as `noreply@github.com`. A GitHub-generated noreply address (bare `noreply@github.com` or the privacy-enabled `<id>+<username>@users.noreply.github.com` form) never resolves to a person, so the `Triggered by` line is omitted entirely rather than showing that address

# Slack App manifect example
```yaml
Expand Down
12 changes: 12 additions & 0 deletions helpers_git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import subprocess
import helpers_slack

# GitHub-generated addresses that never resolve to a real Slack profile:
# noreply@github.com (bare merge/squash commits) and the privacy-enabled
# form <id>+<username>@users.noreply.github.com (bots, and any user with
# "Keep my email addresses private" turned on - this is common, not rare).
NOREPLY_DOMAIN_SUFFIX = '@users.noreply.github.com'
NOREPLY_BARE_ADDRESS = 'noreply@github.com'


def is_noreply_email(email):
email = (email or '').strip().lower()
return email == NOREPLY_BARE_ADDRESS or email.endswith(NOREPLY_DOMAIN_SUFFIX)


def resolve_git_ref_to_sha1(ref_name):
print(f'Resolving {ref_name} to Git SHA1...')
Expand Down
10 changes: 6 additions & 4 deletions helpers_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ def user_id_by_email(app, email):
result = app.client.users_lookupByEmail(email=email)
return result['user']['id']
except SlackApiError as err:
if err.response['error'] == 'users_not_found':
return None

return None
error_code = err.response['error']
if error_code == 'users_not_found':
print(f'No Slack user found for email {email}')
else:
print(f'Slack lookup failed for email {email}: {error_code}')
return None


def is_message_longer_than_limit(message):
Expand Down
15 changes: 14 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
timezone = os.environ['TIMEZONE']
production_branches = os.environ['PRODUCTION_BRANCHES'].split()
slack_bot_token = os.environ["SLACK_BOT_TOKEN"]
# Not every commit is made by a person - squash merges, for instance, are committed by
# the SCM itself - so CI can tell us who triggered the build. Optional.
triggered_by_email = os.environ.get('TRIGGERED_BY_EMAIL', '')

print(f'branches_to_promote: {branches_to_promote}')
print(f'production_branches: {production_branches}')
Expand All @@ -40,6 +43,13 @@
author_email = helpers_git.get_author_email_for_ref(current_commit_id)
author_slack_id = helpers_slack.user_id_by_email(app, author_email)
author_id = f'<@{author_slack_id}>' if author_slack_id is not None else author_email
triggered_by_id = None
triggered_by_valid = (triggered_by_email and triggered_by_email.strip()
and not helpers_git.is_noreply_email(triggered_by_email))
if triggered_by_valid:
triggered_by_slack_id = helpers_slack.user_id_by_email(app, triggered_by_email)
triggered_by_id = (f'<@{triggered_by_slack_id}>'
if triggered_by_slack_id is not None else triggered_by_email)
commit_msg = helpers_git.get_commit_message_for_ref(current_commit_id)

text_for_request = 'If approved will promote commit(s) below to branch '
Expand All @@ -49,7 +59,10 @@
details += f'Commit message: `{commit_msg}`\n'
details += f'Commit id: `{current_commit_id}`\n'
details += f'Committer: {commiter_id}\n'
details += f'Author: {author_id}\n\n'
details += f'Author: {author_id}\n'
if triggered_by_id is not None:
details += f'Triggered by: {triggered_by_id}\n'
details += '\n'
details += helpers_time.generate_time_based_message(production_branches, branches_to_promote, timezone)

# Generate separate diff blocks for every branch
Expand Down
Loading