Created
April 27, 2021 13:49
-
-
Save sontek/8a3838c647f7de9c749423d52ce22b52 to your computer and use it in GitHub Desktop.
Selenium script as a work around for lack of API access to defining webhook secrets in GitLab for the GitHub integration
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
def import_repo_via_selenium(self, repo_name): | |
""" | |
Howto use: | |
1. Check your version of Chrome with about:// | |
2. Download ChromeDriver that matches it: | |
https://chromedriver.chromium.org/downloads | |
3. Unzip and put it somewhere in $PATH, I recommend /usr/local/bin/ | |
""" | |
api_url = 'https://gitlab.com/zapier/' | |
DEFAULT_WAIT_TIME = 15 | |
options = Options() | |
# options.add_argument("--headless") | |
options.add_argument("window-size=1600,900") | |
options.add_argument("--enable-javascript") | |
options.add_argument( | |
f"user-data-dir=/Users/{self.CHROME_USER}/Library/Application Support/Google/Chrome" | |
) | |
options.add_experimental_option("excludeSwitches", ["enable-automation"]) | |
options.add_experimental_option('useAutomationExtension', False) | |
options.add_argument("--disable-blink-features=AutomationControlled") | |
browser = webdriver.Chrome( | |
options=options, | |
) | |
browser.set_window_size(1333, 1024) | |
browser.implicitly_wait(DEFAULT_WAIT_TIME) | |
browser.set_page_load_timeout(60) | |
wait = ui.WebDriverWait(browser, DEFAULT_WAIT_TIME) | |
wait_forever = ui.WebDriverWait(browser, 60 * 15) | |
def scroll_to_element_by_locator(locator, click=False): | |
element = wait.until(expected_conditions.presence_of_element_located(locator)) | |
actions = webdriver.ActionChains(browser) | |
actions.move_to_element(element) | |
actions.perform() | |
browser.execute_script("arguments[0].scrollIntoView(true);", element) | |
time.sleep(0.35) | |
if click: | |
element = wait.until(expected_conditions.element_to_be_clickable(locator)) | |
actions = webdriver.ActionChains(selenium) | |
actions.move_to_element(element) | |
actions.click() | |
actions.perform() | |
return element | |
def find_element_by_id(id): | |
args = by.By.ID, id | |
element = scroll_to_element_by_locator(args) | |
return element | |
def scroll_to_element(element, click=False): | |
actions = webdriver.ActionChains(browser) | |
actions.move_to_element(element) | |
actions.perform() | |
if click: | |
actions = webdriver.ActionChains(browser) | |
actions.move_to_element(element) | |
actions.click() | |
actions.perform() | |
def find_element_by_xpath(xpath, scroll=False): | |
args = by.By.XPATH, xpath | |
element = wait.until(expected_conditions.presence_of_element_located(args)) | |
if element and scroll: | |
scroll_to_element(element) | |
return element | |
def click_by_xpath(xpath, scroll=False, require_visible=False): | |
args = by.By.XPATH, xpath | |
element = wait.until(expected_conditions.element_to_be_clickable(args)) | |
if scroll: | |
scroll_to_element(element) | |
element = browser.find_element_by_xpath(xpath) | |
if require_visible: | |
element = wait.until(expected_conditions.visibility_of_element_located(args)) | |
scroll_to_element(element, click=True) | |
else: | |
if require_visible: | |
element = wait.until(expected_conditions.visibility_of_element_located(args)) | |
element.click() | |
try: | |
browser.get(api_url) | |
# Click the new project button | |
click_by_xpath("//a[contains(@class, 'btn-confirm') and contains(text(), 'New project')]") | |
# Click to make a new CI/CD project | |
click_by_xpath("//h3[contains(text(), 'Run CI/CD for external repository')]") | |
# Click the github button | |
click_by_xpath("//a[contains(@class, 'js-import-github') and contains(text(), 'GitHub')]") | |
# Fill in our personal github token | |
input = find_element_by_id('personal_access_token') | |
input.send_keys(self.GITHUB_MIRROR_KEY) | |
# Click the authorize button | |
click_by_xpath("//input[@data-qa-selector='authenticate_button']") | |
# Filter list to the repo we want to connect | |
ele = find_element_by_xpath("//input[@data-qa-selector='githubish_import_filter_field']") | |
ele.send_keys(repo_name) | |
time.sleep(5) | |
ele.send_keys(Keys.RETURN) | |
# Wait a bit for the search to complete | |
time.sleep(5) | |
# Switch the namespace to Zapier and connect the app | |
tr_ele = find_element_by_xpath( | |
f"(//a[@data-testid='providerLink' and contains(text(), 'zapier/{repo_name}')]/../..)[1]" | |
) | |
tr_ele.find_element_by_xpath("//a[contains(@class, 'select2-choice')]").click() | |
input_ele = find_element_by_xpath("//input[contains(@class, 'select2-focused')]") | |
input_ele.send_keys('zapier') | |
time.sleep(1) | |
input_ele.send_keys(Keys.RETURN) | |
time.sleep(1) | |
click_by_xpath("//button[@data-qa-selector='import_button']") | |
# Wait a long time for the import to complete | |
locator = by.By.XPATH, "//span[contains(text(), 'Importing...')]" | |
element = wait.until( | |
expected_conditions.presence_of_element_located(locator) | |
) | |
finally: | |
browser.quit() | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment