Last active
December 21, 2021 00:13
-
-
Save NHDaly/160f627c29aa83901a2ec1cbd96d576b to your computer and use it in GitHub Desktop.
A script through the GitHub API to subscribe a new user to all the threads that an old user was subscribed to.
This file contains hidden or 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
using HTTP, JSON3 | |
using ProgressMeter # (optional) | |
function subscribe_user(issue, user_login) | |
assign_an_issue_to_new_user(issue, user_login) | |
unassign_an_issue_from_new_user(issue, user_login) | |
end | |
function assign_an_issue_to_new_user(issue, new_user_login::String) | |
return modify_issue_assignee(issue, new_user_login::String, "POST") | |
end | |
function unassign_an_issue_from_new_user(issue, new_user_login::String) | |
return modify_issue_assignee(issue, new_user_login::String, "DELETE") | |
end | |
function modify_issue_assignee(issue, new_user_login::String, POST_OR_DELETE) | |
auth_issue_url = replace(replace(issue.url, "https://" => "https://$(new_user):$(new_pwd)@"), | |
"http://" => "https://$(new_user):$(new_pwd)@") | |
new_issue_response = HTTP.request(POST_OR_DELETE, "$(auth_issue_url)/assignees" , [], | |
JSON3.write(Dict("assignees" => [new_user_login]))) | |
@assert new_issue_response.status in (200,201) "Request to update '$(issue.url)' has failed" | |
resp = JSON3.read(new_issue_response.body) | |
i = 1 | |
success_func = POST_OR_DELETE == "POST" ? in : !in | |
while !success_func(lowercase(new_user_login), lowercase.([a.login for a in resp.assignees])) | |
new_issue_response = HTTP.request("GET", "$(auth_issue_url)" ) | |
resp = JSON3.read(new_issue_response.body) | |
# retry logic | |
i > 10 && (@info "Gave up retries waiting to $(POST_OR_DELETE) for '$(issue.url)'. Got: $(resp.assignees)"; break) | |
i += 1 | |
sleep(1) | |
end | |
# Current issue status | |
return resp | |
end | |
# ----------------------- | |
old_user = "<put your old username here>" | |
new_user = "<put your new username here>" | |
old_password_file = "$(homedir())/github-rai-api-token" | |
new_password_file = "$(homedir())/github-api-token" | |
old_pwd = rstrip(read(old_password_file, String)) | |
new_pwd = rstrip(read(new_password_file, String)) | |
# curl -i -u <old-user> -H "Accept: application/json" | |
# 'https://api.github.com/search/issues?q=mentions%3A<old-user>' --output curl.txt | |
function get_pagified(url) | |
@info "Fetching items from $url" | |
first = JSON3.read(HTTP.request("GET", url).body) | |
total_items = first.total_count | |
@info " total items: $total_items" | |
page = 1 | |
step = length(first.items) | |
items = [] | |
@showprogress for _ in 1:step:total_items + step | |
pagified_url = replace(url, "?"=>"?page=$page&") | |
section = JSON3.read(HTTP.request("GET", pagified_url).body) | |
append!(items, section.items) | |
sleep(2) # 30 requests per minute | |
page += 1 | |
end | |
if length(items) < total_items | |
@info "MISSING ITEMS from $url!" | |
end | |
return items | |
end | |
#mentions = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=mentions%3A$old_user"); | |
#authors = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=author%3A$old_user"); | |
#reviews = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=review-requested%3A$old_user"); | |
#commenters = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=commenter%3A$old_user"); | |
# "Only the first 1000 search results are available" :( 👎 | |
involves1 = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=created:<2020-06-01+involves%3A$old_user"); | |
involves2 = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=created:2020-06-01..2021-06-01+involves%3A$old_user"); | |
involves3 = get_pagified("https://$(old_user):$(old_pwd)@api.github.com/search/issues?q=created:>2021-06-01+involves%3A$old_user"); | |
#items = [mentions..., authors..., reviews...] | |
items = [involves1..., involves2..., involves3...] | |
JSON3.write(homedir()*"/Downloads/github-dump/involves.json", items) | |
@info "Total items: $(length(items))" | |
failures = [] | |
function run_script!(items) | |
@showprogress 1 for item in items | |
try | |
subscribe_user(item, new_user) | |
catch e | |
@info "Failure: $(item.url)" | |
push!(failures, (e, item)) | |
end | |
end | |
end | |
run_script!(items) | |
# | |
# #----------------------------------------------------- | |
# #--- Failed attempts to use the Notifications API | |
# #--- It seems that notification "thread_id"s are unique to a user, so this doesn't work? | |
# #----------------------------------------------------- | |
# | |
# id = 1424677266 | |
# | |
# subscribe_response = HTTP.request("GET", | |
# "https://$(new_user):$(new_pwd)@api.github.com/notifications/threads/$(id)/subscription") | |
# | |
# page = 1 | |
# notifications = [] | |
# for _ in 1:100 | |
# # after=Y3Vyc29yOjUw | |
# notifs = JSON3.read(HTTP.request("GET", | |
# "https://$(old_user):$(old_pwd)@api.github.com/notifications?&all=true&page=$(page)&query=").body) | |
# if length(notifs) == 0 | |
# break | |
# end | |
# append!(notifications, notifs) | |
# page += 1 | |
# end | |
# | |
# |
This file contains hidden or 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
[deps] | |
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" | |
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" | |
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment