GitHub Integration
GitHub Integration
Section titled “GitHub Integration”Copybara has deep integration with GitHub for both reading and writing code, as well as interacting with the GitHub API.
Origins
Section titled “Origins”git.github_origin
Section titled “git.github_origin”Read from a GitHub repository with API integration:
origin = git.github_origin( url = "https://github.com/org/repo", ref = "main",)With Review Requirements
Section titled “With Review Requirements”origin = git.github_origin( url = "https://github.com/org/repo", ref = "main", review_state = "APPROVED", review_approvers = ["maintainer1", "maintainer2"],)git.github_pr_origin
Section titled “git.github_pr_origin”Read from GitHub pull requests:
origin = git.github_pr_origin( url = "https://github.com/org/repo", branch = "main", required_labels = ["ready-to-merge"],)Destinations
Section titled “Destinations”git.github_destination
Section titled “git.github_destination”Push directly to a GitHub repository:
destination = git.github_destination( url = "https://github.com/org/repo", push = "main",)git.github_pr_destination
Section titled “git.github_pr_destination”Create pull requests:
destination = git.github_pr_destination( url = "https://github.com/org/repo", destination_ref = "main", pr_branch = "copybara/sync-${CONTEXT_REFERENCE}", title = "Sync from internal", body = "Automated sync",)Full Configuration
Section titled “Full Configuration”destination = git.github_pr_destination( url = "https://github.com/org/repo", destination_ref = "main", pr_branch = "copybara/sync-${CONTEXT_REFERENCE}", title = "Sync from internal", body = """\## SummaryAutomated sync from internal repository.
## ChangesSee commit history for details.
## Checklist- [ ] Review changes- [ ] Run tests- [ ] Approve and merge""", draft = False, update_description = True, assignees = ["reviewer1", "reviewer2"], labels = ["automated", "documentation"],)GitHub API
Section titled “GitHub API”git.github_api
Section titled “git.github_api”Interact with GitHub API in feedback workflows:
destination = git.github_api( url = "https://github.com/org/repo",)git.github_trigger
Section titled “git.github_trigger”Listen to GitHub events:
origin = git.github_trigger( url = "https://github.com/org/repo", events = ["pull_request", "push"],)API Actions
Section titled “API Actions”In feedback workflows, you can perform API actions:
def _my_action(ctx): # Create an issue ctx.destination.create_issue( title = "Issue title", body = "Issue body", labels = ["bug"], )
# Add a comment ctx.destination.add_comment( number = 123, body = "Automated comment", )
# Get PR info pr = ctx.destination.get_pull_request(123)
return ctx.success()Authentication
Section titled “Authentication”Personal Access Token (Classic)
Section titled “Personal Access Token (Classic)”- Go to GitHub Settings → Developer settings → Personal access tokens
- Generate new token (classic)
- Select scopes:
repo,workflow(if needed) - Copy token
# Configure Gitgit config --global credential.helper storeecho "https://x-access-token:${GITHUB_TOKEN}@github.com" > ~/.git-credentialsFine-Grained Token (Recommended)
Section titled “Fine-Grained Token (Recommended)”- Go to GitHub Settings → Developer settings → Personal access tokens → Fine-grained tokens
- Generate new token
- Select repository access: Only specific repositories
- Select permissions:
- Contents: Read and write
- Pull requests: Read and write
- Metadata: Read
Same as classic token:
git config --global credential.helper storeecho "https://x-access-token:${FINE_GRAINED_TOKEN}@github.com" > ~/.git-credentialsSSH Key
Section titled “SSH Key”# Add SSH keyssh-add ~/.ssh/id_ed25519
# Use SSH URL in configorigin = git.origin( url = "git@github.com:org/repo.git", ref = "main",)PR Variables
Section titled “PR Variables”Available in github_pr_destination:
| Variable | Description |
|---|---|
${CONTEXT_REFERENCE} | Source commit SHA (short) |
${COPYBARA_CONTEXT_REFERENCE} | Same as above |
pr_branch = "copybara/sync-${CONTEXT_REFERENCE}",title = "Sync: ${CONTEXT_REFERENCE}",PR Lifecycle
Section titled “PR Lifecycle”When running multiple times:
| Scenario | Behavior |
|---|---|
| PR doesn’t exist | Create new PR |
| PR exists (open) | Update branch (force push) |
| PR merged | Create new PR |
| PR closed | Create new PR |
Rate Limiting
Section titled “Rate Limiting”GitHub API has rate limits:
- Authenticated: 5,000 requests/hour
- Search API: 30 requests/minute
Copybara handles rate limiting automatically with retries.
Common Patterns
Section titled “Common Patterns”Export to GitHub
Section titled “Export to GitHub”core.workflow( name = "export", origin = git.origin(url = internal_url, ref = "main"), destination = git.github_destination( url = github_url, push = "main", ), ...)Export via PR
Section titled “Export via PR”core.workflow( name = "export-pr", origin = git.origin(url = internal_url, ref = "main"), destination = git.github_pr_destination( url = github_url, destination_ref = "main", ), ...)Import from GitHub
Section titled “Import from GitHub”core.workflow( name = "import", origin = git.github_pr_origin( url = github_url, branch = "main", required_labels = ["approved"], ), destination = git.gerrit_destination( url = internal_url, push_to_refs_for = "main", ), mode = "CHANGE_REQUEST_FROM_SOT", ...)