From 9cb17193d5f790ba720ee1374815ca59262ff884 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Robotka?= <robotka.adrian@gmail.com>
Date: Sat, 15 Aug 2020 16:51:57 +0200
Subject: [PATCH] ANS-3 added disable_shared_runners.py

---
 git-sch-scripts/.gitignore                |  1 +
 git-sch-scripts/README.md                 | 24 +++++++++++
 git-sch-scripts/disable_shared_runners.py | 49 +++++++++++++++++++++++
 git-sch-scripts/env.example               |  1 +
 git-sch-scripts/regulator.sh              | 17 ++++++++
 5 files changed, 92 insertions(+)
 create mode 100644 git-sch-scripts/.gitignore
 create mode 100644 git-sch-scripts/README.md
 create mode 100644 git-sch-scripts/disable_shared_runners.py
 create mode 100644 git-sch-scripts/env.example
 create mode 100755 git-sch-scripts/regulator.sh

diff --git a/git-sch-scripts/.gitignore b/git-sch-scripts/.gitignore
new file mode 100644
index 0000000..0a764a4
--- /dev/null
+++ b/git-sch-scripts/.gitignore
@@ -0,0 +1 @@
+env
diff --git a/git-sch-scripts/README.md b/git-sch-scripts/README.md
new file mode 100644
index 0000000..d45e2c5
--- /dev/null
+++ b/git-sch-scripts/README.md
@@ -0,0 +1,24 @@
+# Scripts to regulate git.sch repo's
+
+## Usage
+
+```sh
+# install virtualenv on your machine
+pip3 install --user virtualenv
+export PATH="~/.local/bin:$PATH"
+
+# create virtualenv
+virtualenv .venv
+
+# enter virtualenv
+source .venv/bin/activate
+
+# install requirements
+pip install -r requirements.python.txt
+
+cp env.example env
+vim env
+
+# rule them all
+./regulator.sh
+```
\ No newline at end of file
diff --git a/git-sch-scripts/disable_shared_runners.py b/git-sch-scripts/disable_shared_runners.py
new file mode 100644
index 0000000..d5ac53e
--- /dev/null
+++ b/git-sch-scripts/disable_shared_runners.py
@@ -0,0 +1,49 @@
+# importing the requests library
+import requests
+import os
+
+host = "git.sch.bme.hu"
+personal_token = os.environ['PERSONAL_API_TOKEN']
+
+def get_url(path) -> str:
+    url = "https://"
+    url += host
+    url += "/api/v4/"
+    url += path
+    url += "?"
+    url += "access_token=" + personal_token
+    return url
+
+
+def request(path):
+    r = requests.get(url=get_url(path))
+    return r.json()
+
+
+def iterate_group(group_id: int):
+    url = "groups/" + str(group_id)
+    group = request(url)
+    for project_meta in group['projects']:
+        repo_action(project_meta)
+    subgroups = request(url + "/subgroups")
+
+    for subgroup in subgroups:
+        iterate_group(str(subgroup['id']))
+
+
+def repo_action(project_meta):
+    if project_meta['shared_runners_enabled']:
+        has_shared_runner_action(project_meta)
+
+
+def has_shared_runner_action(project_meta):
+    path = 'projects/' + str(project_meta['id'])
+    data = {'shared_runners_enabled': False}
+    r = requests.put(url=get_url(path), data=data)
+    if r.ok:
+        print("Changed: " + project_meta['name'])
+    else:
+        print("ERROR: " + project_meta['name'])
+
+
+iterate_group(1604)
diff --git a/git-sch-scripts/env.example b/git-sch-scripts/env.example
new file mode 100644
index 0000000..d0898fe
--- /dev/null
+++ b/git-sch-scripts/env.example
@@ -0,0 +1 @@
+PERSONAL_API_TOKEN=secret
diff --git a/git-sch-scripts/regulator.sh b/git-sch-scripts/regulator.sh
new file mode 100755
index 0000000..f446fbf
--- /dev/null
+++ b/git-sch-scripts/regulator.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+# halt on error
+set -e
+
+source ./env
+export PERSONAL_API_TOKEN
+
+log() {
+    echo
+    echo '######################'
+    echo "# $*"
+    echo '######################'
+}
+
+log 'Disable shared runners'
+python3 disable_shared_runners.py
-- 
GitLab