aboutsummaryrefslogtreecommitdiff
path: root/wmfphablib/phabapi.py
blob: e1d753d4f534b0f49b3bec21805ef2b399096b02 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import base64
import config
import phabricator
from phabricator import Phabricator
import phabdb
from util import log
from util import vlog
from util import errorlog as elog

class phabapi:
    """wrapper for phab api"""

    def __init__(self, user, cert, host):

        self.user = user
        self.cert = cert
        self.host = host

        if host:
            self.con = Phabricator(username=user,
                                certificate=cert,
                                host=host)
        else:
            self.con = None


    def blog_update(self, botname, title, body):
        blogphid = phabdb.get_bot_blog(botname)
        if blogphid is None:
            elog('blogphid is none')
            return
        return self.con.phame.createpost(blogPHID=blogphid,
                                         body=body,
                                         title=title,
                                         phameTitle=title)

    def sync_assigned(self, userphid, id, prepend):
        refs = phabdb.reference_ticket('%s%s' % (prepend, id))
        if not refs:
            log('reference ticket not found for %s' % ('%s%s' % (prepend, id),))
            return
        current = self.con.maniphest.query(phids=[refs[0]])
        if current[current.keys()[0]]['ownerPHID']:
            log('current owner found for => %s' % (str(id),))
            return current
        log('assigning T%s to %s' % (str(id), userphid))
        return phabdb.set_issue_assigned(refs[0], userphid)

    def synced_authored(self, phid, id, ref):
        refs = phabdb.reference_ticket('%s%s' % (ref, id))
        if not refs:
            log('reference ticket not found for %s' % ('%s%s' % (ref, id),))
            return
        log('reference ticket found for %s' % ('%s%s' % (ref, id),))
        newid = self.ticket_id_by_phid(refs[0])
        log("Updating author for %s to %s" % (refs, phid))
        phabdb.set_task_author(phid, newid)

        descript = phabdb.get_task_description(refs[0])
        try:
            clean_description = descript.split('**Description:**\n', 1)[1]
            phabdb.set_task_description(refs[0], clean_description)
        except:
            phabdb.set_task_description(refs[0], descript)

    def task_comment(self, task, msg):
        out = self.con.maniphest.update(id=task, comments=msg)
        return out

    def set_status(self, task, status):
        out = self.con.maniphest.update(id=task, status=status)
        return out

    def task_create(self, title, desc, id, priority, security, ccPHIDs=[], projects=[], refcode=''):
        if refcode:
            reference = '%s%s' % (refcode, id)
        else:
            reference = id

        return self.con.maniphest.createtask(title=title,
                                        description="%s" % desc,
                                        projectPHIDs=projects,
                                        priority=priority,
                                        auxiliary={"std:maniphest:external_reference":"%s" % (reference,),
                                                   "std:maniphest:security_topic": security})

    def get_project_phid(self, name):
        proj = self.con.project.query(names=[name])
        if len(proj['data']) > 0:
            phid = proj['data'][proj['data'].keys()[0]]['phid']
            return phid
        else:
            return None

    def ensure_project(self, project_name,
                             parent_phid=None,
                             pmembers=[],
                             view='public',
                             edit='public'):
        """make sure project exists
        :param project_name: str
        :param pmembers: list
        :param view: str
        :param edit str"""
        existing_proj = self.con.project.query(names=[project_name])
        log(str(existing_proj))
        #existing_proj = phabdb.get_project_phid(project_name)
        #print "EXISTING PROJ: ", existing_proj
        #print "EXISTING PROJ TYPE: ", type(existing_proj)
        #existing_proj = self.con.project.query(names=[project_name])
        if len(existing_proj['data']) < 1:
            log('need to create project(s) ' + project_name)
            #try:
            print "name = %s" % project_name
            print "members = %s" % pmembers
                # obsoleted API call
                #new_proj = self.con.project.create(name=project_name, members=pmembers)
            tlist = [
                {"type":"name", "value":project_name},
                {"type":"view", "value":view},
                {"type":"edit", "value":edit},
            ]

            if len(pmembers) > 0:
                tlist.append( {"type":"members.set", "value":pmembers} )
            if parent_phid != None:
                tlist.append( {"type":"parent", "value":parent_phid} )

            existing_proj = self.con.project.edit( transactions = tlist )
            phid = existing_proj['object']['phid']
        else:
            print existing_proj
            phid = existing_proj['data'].keys()[0]
        return phid

    def upload_file(self, name, data, viewPolicy, dump=False):

        if dump:
            with open(name, 'wb') as f:
                f.write(data)

        log("upload policy for %s is %s" % (name, viewPolicy))
        out = {}
        self.con.timeout = config.file_upload_timeout
        encoded = base64.b64encode(data)
        uploadphid = self.con.file.upload(name=name,
                                          data_base64=encoded,
                                          viewPolicy=viewPolicy)
        out['phid'] = uploadphid
        log("%s upload response: %s" % (name, uploadphid.response))
        fileid = phabdb.get_file_id_by_phid(uploadphid.response)
        out['id'] = int(fileid)
        out['name'] = name
        out['objectName'] = "F%s" % (fileid,)
        log("Created file id: %s" % (fileid,))
        self.con.timeout = 5
        return out

    def ticket_id_by_phid(self, phid):
         tinfo = self.con.maniphest.query(phids=[phid])
         if not tinfo:
             return ''
         if not tinfo.keys():
             return ''
         return tinfo[tinfo.keys()[0]]['id']