Skip to content

Instantly share code, notes, and snippets.

@afjoseph
Last active July 6, 2019 09:14
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 afjoseph/9165f991d3f8a9873e7c3616224217ec to your computer and use it in GitHub Desktop.
Save afjoseph/9165f991d3f8a9873e7c3616224217ec to your computer and use it in GitHub Desktop.
Empty a GitHub repo from issues using a headless Firefox browser
"""
Selenium script to recursively delete issues from a GH repo
WARNING: this WILL delete all an issues repos indiscriminately. DO NOT USE ON PRODUCTION REPOS!
TODO:
- Script only does one page at a time. It should be run after a page has been emptied out
Usage:
# Make a Firefox profile that is logged-in to GitHub
$ python3 -m pip install selenium
# Change hard-coded constants (The ones in CAPS)
$ python3 delete_gh_issues.py
"""
import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
# User must be logged in to GitHub with this profile for things to work
# For MacOS: this path is usually in /Users/lovely/Library/Application Support/Firefox/Profiles/<somehex>.default
FIREFOX_PROFILE_PATH = ""
# Get this from `brew info geckodriver`
GECKODRIVER_PATH = ""
# Link to the repo (don't include '/issues')
GH_REPO_ISSUES_LINK = ""
def delete_issue(driver, title, link):
print("Deleting issue [{}] with link [{}]".format(title, link))
driver.get(link)
elements = driver.find_elements_by_xpath(
"//details[@class='details-reset details-overlay details-overlay-dark']")
delete_issue_elem = elements[-1]
delete_issue_elem.click()
element = driver.find_element_by_xpath("//button[@name='verify_delete']")
element.click()
print("Done")
time.sleep(.5)
def loop_through_issues(driver):
while True:
issue_container = driver.find_element_by_xpath(
"//div[@class='js-navigation-container js-active-navigation-container']")
issue_elem = issue_container.find_element_by_xpath(
"//a[@data-hovercard-type='issue']")
link = issue_elem.get_attribute("href")
delete_issue(driver, issue_elem.text, link)
def main():
try:
options = Options()
options.headless = True
profile = webdriver.FirefoxProfile(FIREFOX_PROFILE_PATH)
driver = webdriver.Firefox(
firefox_profile=profile, options=options, executable_path=os.path.abspath(GECKODRIVER_PATH))
driver.get("{}/issues".format(GH_REPO_ISSUES_LINK))
loop_through_issues(driver)
except Exception as e:
print(e)
driver.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment