Skip to content

Instantly share code, notes, and snippets.

@j3k0
Created November 6, 2018 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j3k0/4658374fa1500624bedcf69ffc124242 to your computer and use it in GitHub Desktop.
Save j3k0/4658374fa1500624bedcf69ffc124242 to your computer and use it in GitHub Desktop.
mutt script that creates github issues
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## About
# Use the content of an email to create a github issue
#
## Usage
# add this to your .muttrc:
# macro index,pager \ei "<pipe-message>~/path/to/mutt2issue.py<enter>"
#
# Requires the `hub` CLI tool: https://hub.github.com/
# import libraries
import sys
import time
import email
import os
from email.header import decode_header
from subprocess import call, Popen, PIPE
import re
# read from stdin and parse subject
message = sys.stdin.read()
message = email.message_from_string(message)
body = None
for part in message.walk():
if part.get_content_type() == "text/plain":
if body is None:
body = ""
charset = part.get_content_charset()
if charset is None:
charset = 'ASCII'
body += unicode(
part.get_payload(decode=True),
charset, 'replace'
).encode('utf8','replace')
message = message['Subject']
# decode internationalized subject and transform ascii into utf8
message = decode_header(message)
message = ' '.join([ unicode(t[0], t[1] or 'ASCII' ) for t in message ])
message = message.encode('utf8')
message = '%s\n%s' % (message, body)
if 'my-project-id' in message or 'some-other-string' in message:
print 'Project X detected'
os.chdir('/path/to/my/project')
else:
print 'Cannot recognize the project in email...'
time.sleep(1)
print message
time.sleep(2)
sys.exit()
call(['hub', 'issue', 'create', '--edit', '-m', message])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment