public
Fork of dreiss/git-jira-attacher
Description: Python script to upload patches from Git to JIRA
Homepage:
Clone URL: git://github.com/eevans/git-jira-attacher.git
git-jira-attacher / jira-apply
100755 78 lines (61 sloc) 2.332 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
 
from datetime import datetime
from common import JiraClient
import getpass, subprocess
 
def formatted_attachment_list(attachments):
    counter = 1
    for a in attachments:
        print "-" * 80
        print "%-10s: %d" % ("ID No.", counter)
        print "%-10s: %s (%s)" % ('Filename', a.filename, a.mimetype)
        print "%-10s: %s" % ('Author', a.author)
        print "%-10s: %s" % ('Created', a.created)
        print "%-10s: %s bytes" % ('Size', a.filesize)
        counter += 1
    print "-" * 80
    
def parse_patch_ids(ids):
    # This is a little naive but OK for now
    return ids.replace(',', ' ').split()
 
def apply_patches(attachments, ids, level=0):
    for index in ids:
        f = attachments[int(index)-1].get_attached_file()
        cmd = ['patch', '-p%d' % level]
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        p.stdin.write(f.read())
        (stdout, _) = p.communicate()
        if p.returncode != 0:
            raise subprocess.CalledProcessError(p.returncode, cmd)
    return [line for line in stdout.split("\n")]
 
def main(issue, username, passwd, level=0):
    client = JiraClient(username, passwd)
    attachments = client.get_attachments(issue)
    
    formatted_attachment_list(attachments)
    print "Attachment ID(s) to apply? ",
    response = raw_input()
    output = apply_patches(attachments, parse_patch_ids(response), level)
    print '\n'.join(output)
 
if __name__ == '__main__':
    from sys import argv, stderr, exit
    from getopt import getopt, GetoptError
    
    try:
        (options, arguments) = getopt(argv[1:], 'u:p:h',
                ['username', 'strip', 'help'])
    except GetoptError, error:
        print >>stderr, error
        exit(1)
        
    username = None
    strip = 0
    
    for opt,arg in options:
        if opt in ('-u', '--username'):
            username = arg
        if opt in ('-p', '--strip'):
            strip = int(arg)
        if opt in ('-h', '--help'):
            print "%s [-u username] [-p num] [-h] jira_issue" % argv[0]
            exit(0)
            
    if not username:
        username = getpass.getuser()
    passwd = getpass.getpass()
    
    assert len(arguments)
    
    main(arguments[0], username, passwd, strip)
 
    
    
# vi:ai sw=4 ts=4 tw=0 et