~cyborg/blog

37dd56a97e58e16c582d1446b92ef5a8380876f0 — cyborg 19 days ago bccd4e7
update RSS with new post
1 files changed, 149 insertions(+), 2 deletions(-)

M rss.xml
M rss.xml => rss.xml +149 -2
@@ 15,14 15,161 @@
        <copyright>Copyright 2017 - 2021, Christian Cleberg</copyright>
        <language>en-us</language>
        <docs>https://cyber.harvard.edu/rss/index.html</docs>
        <lastBuildDate>Mon, 04 Jan 2021 00:00:00 CST</lastBuildDate>
        <lastBuildDate>Fri, 16 Jul 2021 00:00:00 CST</lastBuildDate>
        <ttl>60</ttl>
        <image>
            <url>https://img.cleberg.io/share/profile.jpg</url>
            <title>Blog | Christian Cleberg</title>
            <link>https://blog.cleberg.io</link>
        </image>
        <lastBuildDate>Sun, 30 May 2021 00:00:00 UTC</lastBuildDate>
        <item>
            <title>How to Delete All GitLab Repositories</title>
            <author>Christian Cleberg</author>
            <dc:creator>Christian Cleberg</dc:creator>
            <link>https://blog.cleberg.io/post/how-to-delete-all-gitlab-repositories.html</link>
            <pubDate>Thu, 15 Jul 2021 00:00:00 UTC</pubDate>
            <guid>https://blog.cleberg.io/post/how-to-delete-all-gitlab-repositories.html</guid>
            <description>
                <![CDATA[Use this simple Python script to delete all GitLab repositories without doing any manual work.]]></description>
            <content:encoded><![CDATA[<!doctype html>
<html lang="en">

<head>
    <title>How to Delete All GitLab Repositories</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="description"
          content="Use this simple Python script to delete all GitLab repositories without doing any manual work.">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="https://cleberg.io/assets/app.css">
    <link rel="stylesheet" href="https://cleberg.io/blog/assets/prism.css">
    <script src="https://cleberg.io/blog/assets/prism.js"></script>
</head>

<body>
<main id="main">
    <nav>
        <span>[<a href="https://cleberg.io">About</a>]</span>
        <span>[<a href="https://cleberg.io/blog/">Blog</a>]</span>
        <span>[<a href="https://cleberg.io/projects/">Projects</a>]</span>
        <span>[<a href="https://cleberg.io/blog/rss.xml">RSS</a>]</span>
    </nav>
    <article class="blog-post">
        <header>
            <h1 class="blog-post-title">How to Delete All GitLab Repositories</h1>
            <time datetime="2021-01-04">2021-07-15</time>
        </header>
        <section class="blog-post-body">
            <h2>Background</h2>
            <p>Have you ever used GitLab to host your source code, moved to a different host, and wanted to delete
                everything from your GitLab account? Well this post covers any scenario where you would want to delete
                all repositories from your GitLab account.</p>
            <p>For me, I currently maintain around 30 repositories and don't like to manually delete them whenever I
                switch hosts. GitHub has a few different tools online to delete all repositories for you, but I have not
                found anything similar for GitLab, so I needed an alternative solution.</p>
            <h2>Use a Python Script</h2>
            <h3>Requirements</h3>
            <p>Before we look at the script, make sure you know your GitLab username. Next, <a
                    href="https://gitlab.com/-/profile/personal_access_tokens" rel="noopener,noreferrer"
                    target="_blank">create an authorization token</a> so that the Python script can delete your
                repositories. Don't lose this token or else you'll need to create a new one.</p>
            <h3>Create the Script</h3>
            <p>To run a Python script, you must first create it. Open a terminal and enter the following commands in
                whichever directory you prefer to store the script. You can do the same things in a file manager, if you
                prefer.</p>
            <pre><code class="language-bash">mkdir delete-gitlab</code></pre>
            <pre><code class="language-bash">cd delete-gitlab</code></pre>
            <pre><code class="language-bash">nano main.py</code></pre>
            <p>Enter the following code into your <code>main.py</code> script.</p>
            <pre><code class="language-python">import request
import json


def get_project_ids():
    url = "https://gitlab.com/api/v4/users/{user-id}/projects"

    querystring = {"owned": "true", "simple": "true", "per_page": "50"}

    payload = ""
    headers = {'authorization': 'Bearer {auth-token}'}

    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

    projects = json.loads(response.text)
    projects_ids = list(map(lambda project: project.get('id'), projects))

    return projects_ids


def remove_project(project_id):
    url_temp = "https://gitlab.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {auth-token}'}
    querystring = ""
    payload = ""

    url = url_temp.format(project=project_id)

    response = requests.request("DELETE", url, data=payload, headers=headers, params=querystring)
    project = json.loads(response.text)
    print(project)


def main():
    projects_ids = get_project_ids()

    url_temp = "https://gitlab.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {auth-token}'}
    querystring = ""
    payload = ""

    for project_id in projects_ids:
        url = url_temp.format(project=project_id)

        response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
        project = json.loads(response.text)
        print(str(project.get('id')) + " " + project.get('name'))
        print("Removing...")
        remove_project(project_id)


if __name__ == "__main__":
    main()
</code></pre>
            <p>Now that you have the proper information, replace <code>{user-id}</code> with your GitLab username and
                <code>{auth-token}</code> with the
                authorization token you created earlier.</p>
            <p>Finally, simply run the script and watch the output. You can also use PyCharm Community Edition to edit and run the Python script, if you don't want to work in a terminal.</p>
            <pre><code class="language-bash">python3 main.py</code></pre>
        </section>
    </article>
</main>

<footer>
    <h2>Contact</h2>

    <h3>Email</h3>
    <p>You can email me at <a href="mailto:hello@cleberg.io">hello@cleberg.io</a>. If you prefer encrypted
        communications, please <a href="https://matrix.to/#/@cyborg.:matrix.org">connect with me on Matrix</a> instead.
    </p>

    <h3>Contribute</h3>
    <p>Found an issue or want to contribute to my projects? You can find the code on <a href="https://sr.ht/~cyborg"
                                                                                        target="_blank"
                                                                                        rel="noopener,noreferrer">Sourcehut</a>.
        If you would like to contribute and a project does not currently have a bug tracker or mailing list, send me an
        email.</p>

    <h3>Copyright</h3>
    <p>This website is covered under the <a href="https://cleberg.io/LICENSE">GNU GPLv3 license</a>.</p>
    <p>Copyright &copy; 2018 - 2021</p>
</footer>

</body>

</html>
]]></content:encoded>
        </item>
        <item>
            <title>Changing Git Authors</title>
            <author>Christian Cleberg</author>