-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (76 loc) · 2.48 KB
/
sync_readme.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
name: Sync README across Repositories
on:
# Triggers the workflow on push to the main branch of the source repository
push:
branches: [ main ]
paths:
- 'README.md'
# Allow manual triggering of the workflow
workflow_dispatch:
jobs:
sync-readme:
runs-on: ubuntu-latest
steps:
# Checkout the repository
- uses: actions/checkout@v4
# Set up Python
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
# Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install PyGithub
# Run the sync script
- name: Sync README
env:
# Use GitHub Actions secret for authentication
GITHUB_TOKEN: ${{ secrets.SYNC_README_TOKEN }}
run: |
```python
import os
from github import Github
from github.GithubException import UnknownObjectException
def sync_readme(org_name, repo_a_name, repo_b_name, target_subdir):
github_token = os.environ.get('GITHUB_TOKEN')
if not github_token:
raise ValueError("GitHub token must be set")
g = Github(github_token)
try:
org = g.get_organization(org_name)
repo_a = org.get_repo(repo_a_name)
repo_b = org.get_repo(repo_b_name)
try:
readme_a = repo_a.get_contents("README.md")
except UnknownObjectException:
print(f"No README.md found in {repo_a_name}")
return
target_path = f"{target_subdir}/lunch-and-learn.md"
try:
existing_readme = repo_b.get_contents(target_path)
repo_b.update_file(
path=target_path,
message=f"Update README from {repo_a_name}",
content=readme_a.decoded_content,
sha=existing_readme.sha
)
print(f"Updated README in {repo_b_name}/{target_path}")
except UnknownObjectException:
repo_b.create_file(
path=target_path,
message=f"Add README from {repo_a_name}",
content=readme_a.decoded_content
)
print(f"Created README in {repo_b_name}/{target_path}")
except Exception as e:
print(f"An error occurred: {e}")
# Replace these with your specific repository details
sync_readme(
org_name="nimh-dsst",
repo_a_name="lunch-and-learn",
repo_b_name="dsst-rtd",
target_subdir="docs"
)
EOF