Page MenuHomePhabricator
Paste P13072

Phab api script work in progress
ActivePublic

Authored by Mhurd on Oct 26 2020, 3:51 PM.
Tags
None
Referenced Files
F32414776: Phab api script work in progress
Oct 26 2020, 3:51 PM
Subscribers
None
#!/usr/local/bin/python3
import json, urllib.parse, urllib.request, subprocess;
# BASE_URL = 'https://phab.wmflabs.org'
BASE_URL = 'http://phabricator.local.com'
# API_TOKEN = 'xxx'
API_TOKEN = 'api-gugkkmcues5uhog2zvg4kwkthrmo'
def fetch(url, values):
data = urllib.parse.urlencode(values)
request = urllib.request.Request(url = url, headers = {}, data = data.encode('utf-8'))
response = urllib.request.urlopen(request)
page = response.read()
return json.loads(page.decode('utf-8'))
def getColumnPHID(name, projectPHID):
result = fetch(BASE_URL + '/api/project.column.search', {
'api.token' : API_TOKEN,
'constraints[projects][0]' : projectPHID
})
column = next(x for x in result['result']['data'] if x['fields']['name'] == name )
return column['phid']
def getProjectPHID(name):
return getPHID('#' + name)
def getUserPHID(user):
return getPHID('@' + user)
def getTicketPHID(number):
return getPHID('T' + str(number))
# Prefix 'name' with:
# '@' for User
# '#' for Project
# 'T' for Ticket
# See: https://stackoverflow.com/a/52923649/135557 and https://secure.phabricator.com/w/object_name_prefixes/
def getPHID(name):
result = fetch(BASE_URL + '/api/phid.lookup', {
'api.token' : API_TOKEN,
'names[0]' : name
})
return result['result'][name]['phid']
def getColumnTickets(columnPHID):
result = fetch(BASE_URL + '/api/maniphest.search', {
'api.token' : API_TOKEN,
'constraints[columnPHIDs][0]' : columnPHID
})
tasks = list(filter(lambda x: x['type'] == 'TASK' and x['fields']['status']['name'] == 'Open', result['result']['data']))
return list(map(lambda x: {'id': x['id'], 'phid': x['phid'], 'name': x['fields']['name']}, tasks))
def getProjectTickets(projectPHID):
result = fetch(BASE_URL + '/api/maniphest.search', {
'api.token' : API_TOKEN,
'queryKey' : 'all',
'constraints[projects][0]' : projectPHID
})
tasks = list(filter(lambda x: x['type'] == 'TASK' and x['fields']['status']['name'] == 'Open', result['result']['data']))
return list(map(lambda x: {'id': x['id'], 'phid': x['phid'], 'name': x['fields']['name']}, tasks))
def sendToChrome(object, extension):
filePath = '/tmp/chrome.tmp.' + extension
with open(filePath, 'w') as outfile:
json.dump(object, outfile, indent=4, sort_keys=True)
subprocess.run('open -a "Google Chrome" ' + filePath, shell=True, check=True, text=True)
def addTicketToColumn(ticketPHID, columnPHID, afterPHIDs):
result = fetch(BASE_URL + '/api/maniphest.edit', {
'api.token' : API_TOKEN,
'transactions' : [
{
'type': 'column',
'value': [{
'columnPHID': columnPHID,
'afterPHIDs': afterPHIDs
}]
}
],
'objectIdentifier' : ticketPHID
})
return result
# def addTicketToColumn(ticketPHID, columnPHID, afterPHIDs):
# result = fetch(BASE_URL + '/api/maniphest.edit', {
# 'api.token' : API_TOKEN,
# 'transactions[0][type]' : 'column',
# 'transactions[0][value][columnPHID]' : columnPHID,
# 'transactions[0][value][afterPHIDs][0]' : afterPHIDs,
# 'objectIdentifier' : ticketPHID
# })
# return result
# https://discourse.phabricator-community.org/t/am-i-calling-this-maniphest-edit-endpoint-incorrectly-or-is-this-error-a-bug/1748/2
# The problem was that I was passing columnPHID and afterPHID as children of value.
# Instead, both properties need to be provided instead of value. So, to correct my screenshotted call above:
#
# {
# "0": {
# "type": "column",
# "columnPHID": "PHID-PCOL-tu5zihi26jrf3lxz2ekr",
# "afterPHID": "PHID-TASK-rey5dznxa7neh6abdbhj"
# }
# }
# [{"columnPHID": "PHID-PCOL-4444", "afterPHID": "PHID-TASK-5555"}]
# '[{"columnPHID": " + + "}]'
def addTicketToProject(ticketPHID, projectPHID):
result = fetch(BASE_URL + '/api/maniphest.edit', {
'api.token' : API_TOKEN,
'transactions[0][type]' : 'projects.add',
'transactions[0][value][0]' : projectPHID,
'objectIdentifier' : ticketPHID
})
return result
def removeTicketFromProject(ticketPHID, projectPHID):
result = fetch(BASE_URL + '/api/maniphest.edit', {
'api.token' : API_TOKEN,
'transactions[0][type]' : 'projects.remove',
'transactions[0][value][0]' : projectPHID,
'objectIdentifier' : ticketPHID
})
return result
def addTicketsFromProjectColumnToProjectColumn(sourceProjectName, sourceColumnName, destProjectName, destColumnName):
destProjectPHID = getProjectPHID(destProjectName)
sourceTickets = getColumnTickets(getColumnPHID(sourceColumnName, getProjectPHID(sourceProjectName)))
sourceTicketPHIDs = list(map(lambda x: x['phid'], sourceTickets))
existingDestTickets = getProjectTickets(destProjectPHID)
existingDestTicketPHIDs = list(map(lambda x: x['phid'], existingDestTickets))
diff = lambda l1,l2: [x for x in l1 if x not in l2]
sourceTicketPHIDsNotAlreadyOnDest = diff(sourceTicketPHIDs, existingDestTicketPHIDs)
# for ticketPHID in sourceTicketPHIDsNotAlreadyOnDest: addTicketToProject(ticketPHID, destProjectPHID)
for ticketPHID in sourceTicketPHIDsNotAlreadyOnDest: addTicketToColumn(ticketPHID, getColumnPHID(destColumnName, destProjectPHID)) #, sourceTicketPHIDs)
return 'done'
# output = addTicketToProject(getTicketPHID(24), getProjectPHID('Test-Backlog'))
# output = removeTicketFromProject(getTicketPHID(24), getProjectPHID('Test-Backlog'))
# output = addTicketToProject(getTicketPHID(21), getProjectPHID('Test-Backlog'))
# output = addTicketToProject(getTicketPHID(21), getProjectPHID('Test-Incoming'))
output = getTicketPHID(1)
# output = addTicketToProject(getTicketPHID(1), getProjectPHID('Test-Project2'))
# output = addTicketToColumn(getTicketPHID(1), getColumnPHID('Backlog', getProjectPHID('Test-Project2')), getTicketPHID(2))
# output = addTicketToColumn("PHID-TASK-3snjxnwccmi5ryvumk5h", "PHID-PCOL-p7pg6oysxn7mawbtaa76", ["PHID-TASK-slv6wwwta3xwrlltwoan"])
# output = getColumnTickets(getColumnPHID('Backlog', getProjectPHID('Test-Workboard')))
# output = addTicketsFromProjectColumnToProjectColumn('Test-Workboard', 'QA', 'Test-Incoming', 'Backlog')
# output = addTicketsFromProjectColumnToProjectColumn('Test-Workboard2', 'QA Ready', 'Test-Incoming', 'Test-Column')
# output = getColumnTickets(getColumnPHID('QA Ready', getProjectPHID('Test-Workboard2')))
# Question: once a ticket is tagged into this other column, how should we prevent it from being re-added to that column once whatever action is taken on it removing it from that destination column?
sendToChrome(output, 'json')