-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update message to include more git info
Signed-off-by: Andrey Devyatkin <[email protected]>
- Loading branch information
1 parent
10ab34c
commit 3e35ec9
Showing
6 changed files
with
196 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import subprocess | ||
|
||
def resolve_git_ref_to_sha1(ref_name): | ||
print(f'Resolving {ref_name} to Git SHA1...') | ||
sha1 = subprocess.getoutput(f'git rev-parse {ref_name}') | ||
print(f'{ref_name} = {sha1}') | ||
return sha1 | ||
|
||
|
||
def get_commit_message_for_ref(ref_name): | ||
print(f'Getting commit message for {ref_name}...') | ||
msg = subprocess.getoutput(f'git log -n1 --pretty=tformat:%s%b {ref_name}') | ||
print(f'Message = {msg}') | ||
return msg | ||
|
||
|
||
def get_author_email_for_ref(ref_name): | ||
print(f'Getting author email for {ref_name}...') | ||
email = subprocess.getoutput(f'git --no-pager show -s --format=%ae {ref_name}') | ||
print(f'Author email = {email}') | ||
return email | ||
|
||
|
||
def get_committer_email_for_ref(ref_name): | ||
print(f'Getting committer email for {ref_name}...') | ||
email = subprocess.getoutput(f'git --no-pager show -s --format=%ce {ref_name}') | ||
print(f'Committer email = {email}') | ||
return email | ||
|
||
|
||
def generate_diff(base_branch, current_commit_id, repo_url=''): | ||
remote_name = subprocess.getoutput('git remote') | ||
if remote_name == '': | ||
raise Exception(f'Can not get remote name. Out put of git remote command is: {remote_name}') | ||
base_sha1 = resolve_git_ref_to_sha1(f'{remote_name}/{base_branch}') | ||
diff = f'{base_sha1}..{current_commit_id}' | ||
|
||
# Add a list of commits that you are about to promote | ||
cmd = f'git log --pretty=format:"%h %<(27)%ai %<(20)%an %s" --graph {diff}' | ||
diff_info = f'Change log for changes to approve:\n```\n{cmd}\n' | ||
diff_info += subprocess.getoutput(cmd) | ||
diff_info += '\n```\n\n' | ||
|
||
if 'github' in repo_url or 'gitlab' in repo_url: | ||
diff_info += f'\nFull diff for changes to approve: {repo_url}/compare/{diff}\n\n' | ||
|
||
return diff_info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
|
||
from slack_bolt import App | ||
from slack_sdk.errors import SlackApiError | ||
|
||
def init_app(slack_bot_token, approve_action_id, cancel_action_id): | ||
app = App(token=slack_bot_token) | ||
|
||
@app.action(approve_action_id) | ||
def approve_request(ack, respond, body): | ||
# Acknowledge action request | ||
ack() | ||
print(body) | ||
username = body['user']['username'] | ||
original_text = body['message']['blocks'][0]['text']['text'] | ||
respond(original_text + f'\n\nApproved by {username}') | ||
os._exit(0) | ||
|
||
@app.action(cancel_action_id) | ||
def approve_request(ack, respond, body): | ||
# Acknowledge action request | ||
ack() | ||
print(body) | ||
username = body['user']['username'] | ||
original_text = body['message']['blocks'][0]['text']['text'] | ||
respond(original_text + f'\n\nCanceled by {username}') | ||
os._exit(1) | ||
|
||
@app.middleware | ||
def middleware_func(logger, body, next): | ||
logger.info(f"request body: {body}") | ||
next() | ||
|
||
return app | ||
|
||
def user_id_by_email(app, email): | ||
try: | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytz | ||
from datetime import datetime | ||
|
||
def is_business_hours(timezone): | ||
tz = pytz.timezone(timezone) | ||
now = datetime.now(tz) | ||
return True if now.hour > 8 and now.hour < 19 else False | ||
|
||
|
||
def is_friday_evening(timezone): | ||
tz = pytz.timezone(timezone) | ||
now = datetime.now(tz) | ||
return True if now.isoweekday() == 5 and now.hour > 14 else False | ||
|
||
|
||
def current_time(timezone): | ||
tz = pytz.timezone(timezone) | ||
now = datetime.now(tz) | ||
return "{}:{}:{}".format(now.hour, now.minute, now.second) | ||
|
||
|
||
def generate_time_based_message(prod_branch, branches, timezone): | ||
if prod_branch not in ' '.join(branches): | ||
return '' | ||
message = f'\nIt is {current_time()} in {timezone}.' | ||
if is_friday_evening(): | ||
return (message | ||
+ ' Deploying to production during Friday afternoon hours?' | ||
+ ' *This a sure way to screw up your evening and potentialy weekend!*\n' | ||
+ ' Make sure you are around to deal with consecuences') | ||
if is_business_hours(): | ||
return message + ' *Business hours - think twice before deploying to production!*\n' | ||
return message + ' A good time to attempt deploy\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
slack_bolt == 1.9.0 | ||
slack_bolt == 1.9.0 | ||
pytz==2021.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
export BUILD_JOB_NAME=local-test | ||
export BUILD_JOB_URL=http://whatever.com | ||
export CURRENT_GIT_COMMIT=$(git rev-parse HEAD) | ||
export REPOSITORY_NAME=$(basename $(git rev-parse --show-toplevel)) | ||
export REPOSITORY_URL=https://github.com/fivexl/magic-button | ||
export BRANCHES_TO_PROMOTE=test | ||
export TIMEOUT_MINUTES=10 | ||
export TIMEZONE=$(cat /etc/timezone) | ||
export PRODUCTION_BRANCH=release | ||
export SLACK_CHANNEL_NAME=magic-button-test | ||
|
||
python3 main.py |