-
Notifications
You must be signed in to change notification settings - Fork 0
59 lines (50 loc) · 1.95 KB
/
cleanup-news.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
name: Cleanup Old News Folders
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install PyGitHub
run: |
python -m pip install --upgrade pip
pip install PyGitHub
- name: Remove outdated folders via GitHub API
env:
GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
run: |
python - <<EOF
import os
from datetime import datetime, timedelta, timezone
from github import Github
# Initialize GitHub client
token = os.getenv("GITHUB_TOKEN")
repo_name = "Damantha126/Helakuru-Esana-API"
g = Github(token)
repo = g.get_repo(repo_name)
# Define the cutoff time for outdated folders
cutoff_time = datetime.now(timezone.utc) - timedelta(days=7) # Use timezone-aware datetime
# List contents of the News directory
contents = repo.get_contents("News")
for content in contents:
if content.type == "dir":
# Get last commit date of the folder
commits = repo.get_commits(path=content.path)
if commits.totalCount > 0:
last_modified = commits[0].commit.author.date # This is already timezone-aware
# Check if folder is outdated
if last_modified < cutoff_time:
print(f"Deleting outdated folder: {content.path}")
# GitHub API requires a file to delete
files = repo.get_contents(content.path)
for file in files:
repo.delete_file(file.path, f"Delete outdated file {file.path}", file.sha)
print(f"Delete outdated file {file.path}")
print(f"Deleted folder: {content.path}")
EOF