Last active
December 4, 2019 11:27
-
-
Save PeterMinin/22a8533c996472ec0a23ba51f42d3ad9 to your computer and use it in GitHub Desktop.
Batch-assigning repository identities in Phabricator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
""" | |
Motivation: | |
I made a mirror of a third-party repository in Phabricator to keep track | |
of changes in specific files using Owners. I'm still trying this workflow, | |
and I still have some issues with it that might need patching Phabricator | |
to solve, so I'm not sure I recommend doing this. | |
Anyway, importing the repository to Diffusion created a few hundred | |
unassigned repository identities in <base_url>/diffusion/identity/. | |
In order to clean it up, I created a bot account to assign all these | |
identities to. The UI, quite naturally, doesn't support batch-assigning, | |
neither does the Conduit API have any identity-related methods, | |
so I had to automate it in a more ad-hoc way. | |
Usage: | |
1. Keeping the browser's devtools open, assign one of the identities | |
manually, then find the recorded POST request (the URL will end with | |
"/edit/<number>/"). | |
2. Copy the cookies and POST parameters and edit the values in the script. | |
3. Get the list of the identities' IDs, such as by copy-pasting search | |
results page by page to your favorite text editor and using Find and | |
Replace to leave just the numbers and insert commas between them. | |
Paste the list in the script. | |
4. Run the script. Tested with Python 3.5. | |
""" | |
from urllib.parse import quote | |
from subprocess import check_call | |
params = dict( | |
base_url = 'https://YOURVALUE', | |
# POST params | |
manuallySetUserPHID_0 = 'YOURVALUE', | |
csrf = quote('YOURVALUE', safe=[]), | |
# Cookies | |
phusr = 'YOURVALUE', | |
phsid = 'YOURVALUE', | |
connect_sid = quote('YOURVALUE', safe=[]), | |
rc_uid = 'YOURVALUE', | |
rc_token = 'YOURVALUE', | |
) | |
unassigned_identity_ids = [] # [4, 5, ..., 519, 520]; order not important | |
curl_template = [ | |
'curl', | |
'{base_url}/diffusion/identity/edit/{{identity}}/'.format(**params), | |
'-H', 'Content-Type: application/x-www-form-urlencoded', | |
'-H', 'Cookie: phsid={phsid}; phusr={phusr}; connect.sid={connect_sid}; rc_uid={rc_uid}; rc_token={rc_token}; jump_to_hisec=yes'.format(**params), | |
'-H', 'Pragma: no-cache', | |
'-H', 'Cache-Control: no-cache', | |
'--data', '__csrf__={csrf}&__form__=1&editEngine=true&manuallySetUserPHID%5B0%5D={manuallySetUserPHID_0}'.format(**params) | |
] | |
for identity in unassigned_identity_ids: | |
args = [arg.format(identity=identity) for arg in curl_template] | |
print('Identity {}'.format(identity), end='... ') | |
check_call(args) | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment