Created
April 30, 2018 15:02
-
-
Save tarnacious/591076ff223387ffd6437d5e06448e74 to your computer and use it in GitHub Desktop.
Delete all the CI/CD logs for a gitlab project
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
# Delete all the CI/CD logs for a gitlab project | |
# | |
# Requires: | |
# python 3 | |
# | |
# requests==2.18.4 | |
# lxml==4.2.1 | |
# cssselect==1.0.3 | |
# | |
import requests | |
from lxml import html | |
jobs_url = "https://gitlab.com/some-org/some-repo/-/jobs" | |
username = "" | |
password = "" | |
login_url = "https://gitlab.com/users/sign_in" | |
session_requests = requests.session() | |
result = session_requests.get(login_url) | |
tree = html.fromstring(result.text) | |
authenticity_token = list(set(tree.xpath("//meta[@name='csrf-token']")))[0].values()[1] | |
print("authenticity_token:", authenticity_token) | |
login_form = { | |
"authenticity_token": authenticity_token, | |
"user[login]": username, | |
"user[password]": password, | |
"user[remember_me]": 0, | |
"utf8": "✓" | |
} | |
result = session_requests.post( | |
login_url, | |
data = login_form, | |
headers = dict(referer=login_url) | |
) | |
next_page = 1 | |
all_values = [] | |
while(next_page != None): | |
print("Downloading page:", next_page) | |
page = session_requests.get(jobs_url + "?page=" + str(next_page)) | |
tree = html.fromstring(page.text) | |
items = tree.xpath("//a[@class='build-link']") | |
items = tree.cssselect('.build-link') | |
values = list(map(lambda x: x.text[1:], items)) | |
all_values.extend(values) | |
if len(values) == 0: | |
next_page = None | |
else: | |
next_page = next_page + 1 | |
for value in all_values: | |
print("Deleting job:", value) | |
url = jobs_url + "/" + value | |
result = session_requests.get(url) | |
tree = html.fromstring(result.text) | |
authenticity_token = list(set(tree.xpath("//meta[@name='csrf-token']")))[0].values()[1] | |
form = { | |
"authenticity_token": authenticity_token, | |
"_method": "post" | |
} | |
url = jobs_url + "/" + value + "/erase" | |
print(url) | |
result = session_requests.post( | |
url, | |
data = form, | |
headers = dict(referer=jobs_url + "/" + value) | |
) | |
print(result) | |
print("Finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment