From da452de31bda575eb312a040330d652fcc970311 Mon Sep 17 00:00:00 2001
From: "p.delpy@dkfz-heidelberg.de"
Date: Tue, 15 Oct 2024 07:59:34 +0200
Subject: [PATCH 1/5] feat: add auto archiving action
---
.github/scripts/rename_inactive_branches.py | 33 +++++++++++++++++++
.../workflows/rename-inactive-branches.yml | 30 +++++++++++++++++
2 files changed, 63 insertions(+)
create mode 100644 .github/scripts/rename_inactive_branches.py
create mode 100644 .github/workflows/rename-inactive-branches.yml
diff --git a/.github/scripts/rename_inactive_branches.py b/.github/scripts/rename_inactive_branches.py
new file mode 100644
index 00000000..a101c3ef
--- /dev/null
+++ b/.github/scripts/rename_inactive_branches.py
@@ -0,0 +1,33 @@
+import os
+import requests
+from datetime import datetime, timedelta
+
+# Configuration
+GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
+REPO = 'samply/bridgehead'
+HEADERS = {'Authorization': f'token {GITHUB_TOKEN}', 'Accept': 'application/vnd.github.v3+json'}
+API_URL = f'https://api.github.com/repos/{REPO}/branches'
+INACTIVE_DAYS = 365
+CUTOFF_DATE = datetime.now() - timedelta(days=INACTIVE_DAYS)
+
+# Fetch all branches
+def get_branches():
+ response = requests.get(API_URL, headers=HEADERS)
+ return response.json() if response.status_code == 200 else []
+
+# Rename inactive branches
+def rename_branch(old_name, new_name):
+ rename_url = f'https://api.github.com/repos/{REPO}/branches/{old_name}/rename'
+ response = requests.post(rename_url, json={'new_name': new_name}, headers=HEADERS)
+ print(f"Renamed branch {old_name} to {new_name}" if response.status_code == 201 else f"Failed to rename {old_name}: {response.status_code}")
+
+# Check if the branch is inactive
+def is_inactive(commit_url):
+ last_commit_date = requests.get(commit_url, headers=HEADERS).json()['commit']['committer']['date']
+ return datetime.strptime(last_commit_date, '%Y-%m-%dT%H:%M:%SZ') < CUTOFF_DATE
+
+# Rename inactive branches
+for branch in get_branches():
+ if is_inactive(branch['commit']['url']):
+ #rename_branch(branch['name'], f"archived/{branch['name']}")
+ print(f"[LOG] Branch '{branch['name']}' is inactive and would be renamed to archived/'{branch['name']}'")
\ No newline at end of file
diff --git a/.github/workflows/rename-inactive-branches.yml b/.github/workflows/rename-inactive-branches.yml
new file mode 100644
index 00000000..2c188e66
--- /dev/null
+++ b/.github/workflows/rename-inactive-branches.yml
@@ -0,0 +1,30 @@
+name: Cleanup - Rename Inactive Branches
+
+on:
+ schedule:
+ - cron: '0 0 * * 0' # Runs every Sunday at midnight
+ #todo delete; only trigger for testing
+ pull_request:
+ branches:
+ - develop
+jobs:
+ cleanup-rename-branches:
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout Repository
+ uses: actions/checkout@v2
+
+ - name: Set up Python
+ uses: actions/setup-python@v2
+ with:
+ python-version: '3.x'
+
+ - name: Install Libraries
+ run: pip install requests
+
+ - name: Run Script to Rename Inactive Branches
+ run: |
+ python .github/scripts/rename_inactive_branches.py
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
\ No newline at end of file
From 05dbbb20ab5895ee52d438bd3d0bf3dcddcb8ad4 Mon Sep 17 00:00:00 2001
From: "p.delpy@dkfz-heidelberg.de"
Date: Tue, 15 Oct 2024 08:41:40 +0200
Subject: [PATCH 2/5] to squash
---
.github/scripts/rename_inactive_branches.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.github/scripts/rename_inactive_branches.py b/.github/scripts/rename_inactive_branches.py
index a101c3ef..e62fe835 100644
--- a/.github/scripts/rename_inactive_branches.py
+++ b/.github/scripts/rename_inactive_branches.py
@@ -30,4 +30,4 @@ def is_inactive(commit_url):
for branch in get_branches():
if is_inactive(branch['commit']['url']):
#rename_branch(branch['name'], f"archived/{branch['name']}")
- print(f"[LOG] Branch '{branch['name']}' is inactive and would be renamed to archived/'{branch['name']}'")
\ No newline at end of file
+ print(f"[LOG] Branch '{branch['name']}' is inactive and would be renamed to 'archived/{branch['name']}'")
\ No newline at end of file
From 7ef4c8b9c595cf5f8cb424aa006d97b4d44be17d Mon Sep 17 00:00:00 2001
From: Pierre Delpy <75260699+PierreDelpy@users.noreply.github.com>
Date: Tue, 15 Oct 2024 09:35:07 +0200
Subject: [PATCH 3/5] Update .github/workflows/rename-inactive-branches.yml
Co-authored-by: Martin Lablans <6804500+lablans@users.noreply.github.com>
---
.github/workflows/rename-inactive-branches.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.github/workflows/rename-inactive-branches.yml b/.github/workflows/rename-inactive-branches.yml
index 2c188e66..e45d0398 100644
--- a/.github/workflows/rename-inactive-branches.yml
+++ b/.github/workflows/rename-inactive-branches.yml
@@ -8,7 +8,7 @@ on:
branches:
- develop
jobs:
- cleanup-rename-branches:
+ archive-stale-branches:
runs-on: ubuntu-latest
steps:
From ac0274cfba1c61120eee33061dc11254d5562a0c Mon Sep 17 00:00:00 2001
From: Pierre Delpy
Date: Tue, 15 Oct 2024 09:38:24 +0200
Subject: [PATCH 4/5] feat: add exceptions and boilerplate
---
.github/scripts/rename_inactive_branches.py | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/.github/scripts/rename_inactive_branches.py b/.github/scripts/rename_inactive_branches.py
index e62fe835..b9bd3597 100644
--- a/.github/scripts/rename_inactive_branches.py
+++ b/.github/scripts/rename_inactive_branches.py
@@ -13,12 +13,14 @@
# Fetch all branches
def get_branches():
response = requests.get(API_URL, headers=HEADERS)
+ response.raise_for_status()
return response.json() if response.status_code == 200 else []
# Rename inactive branches
def rename_branch(old_name, new_name):
rename_url = f'https://api.github.com/repos/{REPO}/branches/{old_name}/rename'
response = requests.post(rename_url, json={'new_name': new_name}, headers=HEADERS)
+ response.raise_for_status()
print(f"Renamed branch {old_name} to {new_name}" if response.status_code == 201 else f"Failed to rename {old_name}: {response.status_code}")
# Check if the branch is inactive
@@ -27,7 +29,11 @@ def is_inactive(commit_url):
return datetime.strptime(last_commit_date, '%Y-%m-%dT%H:%M:%SZ') < CUTOFF_DATE
# Rename inactive branches
-for branch in get_branches():
- if is_inactive(branch['commit']['url']):
- #rename_branch(branch['name'], f"archived/{branch['name']}")
- print(f"[LOG] Branch '{branch['name']}' is inactive and would be renamed to 'archived/{branch['name']}'")
\ No newline at end of file
+def main():
+ for branch in get_branches():
+ if is_inactive(branch['commit']['url']):
+ #rename_branch(branch['name'], f"archived/{branch['name']}")
+ print(f"[LOG] Branch '{branch['name']}' is inactive and would be renamed to 'archived/{branch['name']}'")
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
From 8a2218037b3e6df2973d3d392287b69d9e04730f Mon Sep 17 00:00:00 2001
From: Pierre Delpy
Date: Tue, 15 Oct 2024 12:58:12 +0200
Subject: [PATCH 5/5] remove test
---
.github/workflows/rename-inactive-branches.yml | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/.github/workflows/rename-inactive-branches.yml b/.github/workflows/rename-inactive-branches.yml
index e45d0398..9bcca79e 100644
--- a/.github/workflows/rename-inactive-branches.yml
+++ b/.github/workflows/rename-inactive-branches.yml
@@ -3,10 +3,7 @@ name: Cleanup - Rename Inactive Branches
on:
schedule:
- cron: '0 0 * * 0' # Runs every Sunday at midnight
- #todo delete; only trigger for testing
- pull_request:
- branches:
- - develop
+
jobs:
archive-stale-branches:
runs-on: ubuntu-latest