Skip to content

Instantly share code, notes, and snippets.

@mapes911
Last active December 17, 2015 05:59
Show Gist options
  • Save mapes911/5562484 to your computer and use it in GitHub Desktop.
Save mapes911/5562484 to your computer and use it in GitHub Desktop.
Django view - git trello hook
"""
This is a django view that can be called via a github post-commit hook.
I was really surprised when such a thing didn't exist so I wrote it myself. Hopefully this helps someone out there :)
Thanks to the Trolly app for providing a python wrapper to the Trello API.
Requirements:
https://github.com/plish/Trolly
** Note - this was pulled out of a current project and not tested after pulling out some code.. so buyer beware
** Notes for usage below...
The #CardID makes an assumption that you are making a commit on the Current Development Board. If so, then you can use the short CardID shown at the bottom right of every expanded card.
However, if the card is not part of the Current Development Board, we can't use the short form ID because then there is no way to uniquely identify the card within Trello. So, if you need to do a commit to a card outside of the Current Development Board for any reason, you need to use the ID that comes from it's short URL. This is found by clicking the "More..." link beside the CardID which shows you the short form URL.
For example, if the card URL is
https://trello.com/c/idLqVSYD
That makes the CardID idLqVSYD
So both of these commit messages would post the commit to the card.
"Example commit to #19"
or
"Example commit to #idLqVSYD"
"""
import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from trolly.client import Client
from trolly.card import Card
from trolly.board import Board
api_key = '<YOUR API KEY>'
user_auth_token = '<YOUR USER AUTH TOKEN>'
development_board = '<ID OF MAIN BOARD>'
@csrf_exempt
def git_post_commit_hook(request):
''' post commit hook processing to update trello '''
client = Client(api_key, user_auth_token)
if 'payload' in request.POST:
payload_json = json.loads(request.POST['payload'])
for commit in payload_json['commits']:
committer_name = commit['committer']['name']
commit_message = commit['message']
commit_url = commit['url']
new_trello_comment = "GitHub Commit (via %s)\n'%s'\n%s" % (committer_name, commit_message, commit_url)
git_comment_split = commit_message.split(' ')
for term in git_comment_split:
if term != '' and term[0] == '#':
card_id = term[1:]
board = Board(client, development_board)
card = board.getCard(card_id)
# assume the card belongs to our dev board, but if not try to find it with the id
if not card:
card = Card(client, card_id)
card.addComments(new_trello_comment)
return HttpResponse('ok')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment