Skip to content

Commit 7f4a35d

Browse files
authored
Update main.yml
1 parent e3e0f10 commit 7f4a35d

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

.github/workflows/main.yml

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
- cron: '0 0 1 * *' # Run monthly on the 1st at midnight
55
workflow_dispatch: # Allow manual trigger
66

7+
permissions:
8+
issues: write
9+
contents: read
10+
711
jobs:
812
check_freshness:
913
runs-on: ubuntu-latest
@@ -15,19 +19,29 @@ jobs:
1519
- uses: actions/setup-python@v4
1620
with:
1721
python-version: '3.x'
22+
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install requests
1827
19-
- name: Create scripts directory
20-
run: mkdir -p .github/scripts
21-
22-
- name: Copy Python script
28+
- name: Create and run freshness check script
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2331
run: |
24-
cat > .github/scripts/check_freshness.py << 'EOL'
32+
cat > check_freshness.py << 'EOL'
2533
import os
2634
import subprocess
35+
import requests
2736
from datetime import datetime, timedelta
2837
29-
FRESHNESS_PERIOD = timedelta(days=180) # 6 months
30-
IGNORED_FILES = ["_index.md"] # Add any files to ignore
38+
# Configuration
39+
FRESHNESS_PERIOD = timedelta(days=180) # 180 days (6 months)
40+
IGNORED_FILES = ["_index.md"]
41+
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
42+
GITHUB_REPO = os.environ.get('GITHUB_REPOSITORY', 'owner/repo')
43+
GITHUB_API_URL = f"https://api.github.com/repos/{GITHUB_REPO}/issues"
44+
3145
now = datetime.now()
3246
3347
def get_last_modified_time(file_path):
@@ -55,7 +69,33 @@ jobs:
5569
stale_files.append((relative_path, last_modified))
5670
return stale_files
5771
58-
docs_directory = "docs/content/master" # Crossplane docs directory
72+
def create_github_issue(body):
73+
if not GITHUB_TOKEN:
74+
print("Error: GITHUB_TOKEN environment variable not set")
75+
return False
76+
77+
headers = {
78+
'Authorization': f'token {GITHUB_TOKEN}',
79+
'Accept': 'application/vnd.github.v3+json'
80+
}
81+
82+
issue_data = {
83+
'title': '📚 Documentation Freshness Check Results',
84+
'body': body,
85+
'labels': ['documentation', 'maintenance']
86+
}
87+
88+
response = requests.post(GITHUB_API_URL, headers=headers, json=issue_data)
89+
if response.status_code == 201:
90+
print(f"Issue created successfully: {response.json()['html_url']}")
91+
return True
92+
else:
93+
print(f"Failed to create issue. Status code: {response.status_code}")
94+
print(f"Response: {response.text}")
95+
return False
96+
97+
# Main execution
98+
docs_directory = "content"
5999
stale_docs = check_freshness(docs_directory)
60100
sorted_stale_docs = sorted(stale_docs, key=lambda x: x[1])
61101
@@ -66,37 +106,10 @@ jobs:
66106
issue_body += f"- `{doc}` (Last Modified: {mod_time.strftime('%Y-%m-%d')})\n"
67107
issue_body += "\nPlease review these documents to ensure they are up-to-date and accurate."
68108
69-
with open("issue_body.txt", "w") as f:
70-
f.write(issue_body)
71-
72-
with open("has_stale.txt", "w") as f:
73-
f.write("true")
109+
print(issue_body) # Print to console
110+
create_github_issue(issue_body) # Create GitHub issue
74111
else:
75-
with open("has_stale.txt", "w") as f:
76-
f.write("false")
112+
print("All documentation is up to date!")
77113
EOL
78-
79-
- name: Run freshness check
80-
run: python3 .github/scripts/check_freshness.py
81-
82-
- name: Check for stale docs
83-
id: check
84-
run: |
85-
HAS_STALE=$(cat has_stale.txt)
86-
echo "has_stale=$HAS_STALE" >> $GITHUB_OUTPUT
87-
88-
- name: Create Issue
89-
if: steps.check.outputs.has_stale == 'true'
90-
uses: actions/github-script@v6
91-
with:
92-
script: |
93-
const fs = require('fs');
94-
const issueBody = fs.readFileSync('issue_body.txt', 'utf8');
95-
96-
await github.rest.issues.create({
97-
owner: context.repo.owner,
98-
repo: context.repo.repo,
99-
title: '📚 Documentation Freshness Check Results',
100-
body: issueBody,
101-
labels: ['documentation', 'maintenance']
102-
});
114+
115+
python check_freshness.py

0 commit comments

Comments
 (0)