Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize README.md to the DSST Lunch & Learn page #9

Open
ericearl opened this issue Dec 11, 2024 · 7 comments
Open

Synchronize README.md to the DSST Lunch & Learn page #9

ericearl opened this issue Dec 11, 2024 · 7 comments
Assignees

Comments

@ericearl
Copy link
Member

I would like it if one of two things happened here:

A) This repo houses discussion summaries, presentations, etc. and the DSST L&L page houses the README.md current and future contents; or

B) The README.md here automatically synchronized to the dsst-rtd repo's lunch-and-learn.md

@joshlawrimore
Copy link
Collaborator

I personally think B is a better option... Here is what I had in mind. We use (pyGitHub)[https://github.com/PyGithub/PyGithub] to create a script (see below) that updates the dsst-rtd repo. We automate the script using GitHub actions, see example YAML file below. Its my first time doing GitHub Actions, so I may need to debug what I got from Claude

GitHub Actions YAML

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 - << EOF
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}/README.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="your-organization", 
    repo_a_name="source-repo", 
    repo_b_name="destination-repo", 
    target_subdir="docs"
)
EOF

@ericearl
Copy link
Member Author

That looks like it should work (I briefly reviewed it) as long as you set:

  1. SYNC_README_TOKEN in the secrets
  2. Replace the target_path line with target_path = f"{target_subdir}/lunch-and-learn.md" in the actions script
  3. Fill this in the actions script:
    # Replace these with your specific repository details
    sync_readme(
        org_name="your-organization", 
        repo_a_name="source-repo", 
        repo_b_name="destination-repo", 
        target_subdir="docs"
    )
    

@joshlawrimore
Copy link
Collaborator

joshlawrimore commented Dec 12, 2024

@ericearl, can you generate the access token? I don't see settings for NIMH-DSST. I am just a member, so I don't think I can generate the token. I don't see "developer settings" when I access the NIMH-DSST org setting...

@ericearl
Copy link
Member Author

@joshlawrimore
Copy link
Collaborator

My triggering of the Action worked, but I got the same 403 Forbidden error on the Request PUT /repos/nimh-dsst/dsst-rtd/contents/docs/lunch-and-learn.md command that your last job did. I think this is due to the use of the GITHUB_TOKEN rather than using a Personal Access Token as a secret.

According to permission for GITHUB_TOKEN, "The token's permissions are limited to the repository that contains your workflow."

When I attempt to create a PAT, I don't have the option to get read/write addess to NIMH-DSST repositories. We need to get an access token that can read from the lunch-and-learn repo and write to the dsst-rtd.

@ericearl
Copy link
Member Author

I wasn't able to do that either (I tried, but couldn't figure it out), but I'll elevate your permissions and see if you can do that?

@joshlawrimore
Copy link
Collaborator

Success on manual trigger!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants