Last active
December 10, 2015 21:39
-
-
Save joelparker/4496818 to your computer and use it in GitHub Desktop.
A little command line utility that uses boto to send email to my micro managing boss for every git command I push to a repo. It's not that he wants all commits but rather a list of every commit I do (that's the same thing to me to....). I created an Amazon SNS Topic with an email subscription. When I publish to the topic an email is sent.
This file contains hidden or 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
import boto.sns | |
import git | |
import argparse | |
#Default git repo location so you don't have to pass it on the cli using --repo | |
git_repo_location = "/some/path/on/disk/to/git/repo" | |
#Log into the AWS SNS console to get this value | |
#https://console.aws.amazon.com/sns/home and click on the topic you want | |
topic_arn = u'arn:aws:sns:region:.*:topic_name' | |
def parse_cli_options(): | |
parser = argparse.ArgumentParser(description='Send git email notificiations using Amazon SNS') | |
parser.add_argument('-c', '--commits', nargs="+", help='Commit(s) to send in Email', metavar='COMMIT') | |
parser.add_argument('--repo','-r', type=str, help='Path to git repository', default=git_repo_location) | |
return parser | |
def create_message_and_subject(repo_path, commits): | |
git_repo = git.repo.Repo(repo_path) | |
message = "" | |
has_valid_commit = False | |
for commit in commits: | |
try: | |
git_commit = git_repo.commit(commit) | |
message += git_commit.hexsha + '\n' | |
message += git_commit.message | |
message += '\n' + ('=' * 79) + '\n' | |
has_valid_commit = True | |
except: | |
print commit + " is not valid. Skipping" | |
#Subject line | |
subject = 'Commit' | |
if len(commits) > 1: | |
subject += 's ' | |
for c in commits: | |
subject += (c + ' ') | |
else: | |
subject += ' ' + commits[0] | |
data = {} | |
data['message'] = message | |
data['subject'] = subject | |
data['valid'] = has_valid_commit | |
return data | |
def publish_to_amazon_sns_topic(topic_arn,data): | |
if data['valid']: | |
sns = boto.sns.SNSConnection() | |
message = data['message'] | |
subject = data['subject'] | |
sns.publish(topic_arn,message,subject) | |
def main(): | |
parser = parse_cli_options() | |
args = parser.parse_args() | |
if args.commits < 1: | |
parser.print_help() | |
print "ERROR: Must have at least one commit specificed!" | |
exit(1) | |
sns_info = create_message_and_subject(args.repo, args.commits) | |
publish_to_amazon_sns_topic(topic_arn,sns_info) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment