Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Last active July 1, 2018 19:20
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 thcipriani/9b09ca451852ac09ff924220b7770c1b to your computer and use it in GitHub Desktop.
Save thcipriani/9b09ca451852ac09ff924220b7770c1b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
import urllib.parse
# Unix Epoch last Friday: date +%s --date "2018-06-29"
START_DATE = 1530252000
BASE_URL = 'https://phabricator.wikimedia.org/api'
CURL = '/usr/bin/curl'
try:
with open(os.path.join(os.environ['HOME'], '.phab-token')) as f:
API_TOKEN = f.read().strip()
except FileNotFoundError:
print("Couldn't find ~/.phab-token, exiting...")
sys.exit(1)
def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument('-t', '--task', help='task to update', required=True)
ap.add_argument(
'-u', '--user',
help='username of person who vandalized task', required=True)
return ap.parse_args()
def get_transactions(task, token, date):
cmd = [
CURL,
os.path.join(
BASE_URL,
'transaction.search'
),
'-d', 'api.token={}'.format(token),
'-d', 'objectIdentifier={}'.format(task),
'-d', 'after={}'.format(date)
]
return json.loads(
subprocess.check_output(cmd)
)['result']['data']
def get_user(name, token):
cmd = [
CURL,
os.path.join(
BASE_URL,
'user.search'
),
'-d', 'api.token={}'.format(token),
'-d', 'constraints[usernames][0]={}'.format(name)
]
return json.loads(
subprocess.check_output(cmd)
)['result']['data'][0]['phid']
def update_task(token, task, update_type, value):
cmd = [
CURL,
os.path.join(
BASE_URL,
'maniphest.edit'
),
'-d', 'api.token={}'.format(token),
'-d',
'transactions[0][type]={}'.format(
urllib.parse.quote_plus(update_type)),
'-d',
'transactions[0][value]={}'.format(
urllib.parse.quote_plus(value)),
'-d', 'objectIdentifier={}'.format(task),
]
subprocess.check_call(cmd)
def user_transactions(user, transactions):
user_transactions = []
for transaction in transactions:
if transaction['authorPHID'] == user:
user_transactions.append(transaction)
return user_transactions
def main():
args = parse_args()
user = get_user(args.user, API_TOKEN)
transactions = get_transactions(args.task, API_TOKEN, START_DATE)
transactions_to_undo = user_transactions(user, transactions)
for transaction in transactions_to_undo:
if transaction['type']:
update_task(
API_TOKEN,
args.task,
transaction['type'],
transaction['fields']['old']
)
else:
print('"{}" failed'.format(transaction['phid']))
pass
if __name__ == '__main__':
main()
#!/usr/bin/env python3
import argparse
import os
import subprocess
import sys
import urllib.parse
# Unix Epoch last Friday: date +%s --date "2018-06-29"
START_DATE = 1530252000
BASE_URL = 'https://phabricator.wikimedia.org/api'
CURL = '/usr/bin/curl'
# Seems to be the list of projects that recent spammer added
PROJECTS = [
"PHID-PROJ-clwtwgdwe3z4zm24wfzp", # CheckUser
"PHID-PROJ-hewjqnqvdffs4aj6ulxh", # Gamepress
"PHID-PROJ-e5iv63qha3h73d6mmcdr", # Hashtags
"PHID-PROJ-5bhzhy2sfove2cz56aiu", # JADE
"PHID-PROJ-gqelrt5sz46k4qy7i75r", # KartoEditor
"PHID-PROJ-e5jsd437ommc7emml7wd", # Language-2018-Apr-June
"PHID-PROJ-iwl7ucugidn6j6bvitax", # Mail
"PHID-PROJ-tlkvo4xt3veuo4i2akcq", # New-Editor-Experiences
"PHID-PROJ-ohhlvzggtc75tqba6rof", # Tamil-Sites
"PHID-PROJ-ptnfbfyq36kkebaxugcz", # TCB-Team
]
try:
with open(os.path.join(os.environ['HOME'], '.phab-token')) as f:
API_TOKEN = f.read().strip()
except FileNotFoundError:
print("Couldn't find ~/.phab-token, exiting...")
sys.exit(1)
def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument('-t', '--task', help='task to update', required=True)
return ap.parse_args()
def update_task(token, task, update_type, projects):
cmd = [
CURL,
os.path.join(
BASE_URL,
'maniphest.edit'
),
'-d', 'api.token={}'.format(token),
'-d', 'objectIdentifier={}'.format(task),
'-d',
'transactions[0][type]={}'.format(
urllib.parse.quote_plus(update_type)),
]
count = 0
for project in projects:
cmd += [
'-d',
'transactions[0][value][{}]={}'.format(
count,
urllib.parse.quote_plus(project))
]
count += 1
subprocess.check_call(cmd)
def main():
args = parse_args()
update_task(
API_TOKEN,
args.task,
'projects.remove',
PROJECTS
)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment