Sub topic 2
Setting Fine-Grained Permissions
Assume you know basic GitHub repositories, organizations, REST requests, bearer tokens, and environment variables. This chapter focuses on choosing the smallest useful access for fine-grained personal access tokens and GitHub Apps.
Setting fine-grained permissions
A fine-grained personal access token (PAT) is limited by both where it can act and what it can do.
When creating one, choose:
- The resource owner, such as your personal account or an organization.
- Specific repositories, rather than every repository owned by that account.
- Permissions at the lowest useful level: read, write, or admin.
For example, a workflow that reads issue labels needs Issues: read. A workflow that adds labels needs Issues: write. It does not need repository administration.
Some permissions imply related access. GitHub also grants basic metadata access for selected repositories. Review the permission description before creating the token, and test it against the exact API operation.
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/issuesExpiration, approval, and organization policies
Set an expiration date that matches the task. Short-lived credentials reduce the damage from accidental exposure. An organization can require administrator approval for tokens that access its resources. Organization policies can also restrict which token types, permissions, or repository selections are allowed.
A token may therefore be valid but still fail because the organization has not approved it. Check the token status and the organization’s policy when access is denied.
What a GitHub App is
A GitHub App is an application identity with its own permissions and event subscriptions. It is usually better than a personal token for an integration used by many repositories or users. The app’s permissions describe possible access; an installation determines the actual account and repositories it can access.
Installing an app on accounts and repositories
An app can be installed on a personal account or an organization. During installation, the administrator chooses all repositories or selected repositories. The installation can access only repositories allowed by both the app’s permissions and the installation selection.
App permissions and webhook events
App permissions control API actions such as reading repository contents or writing issues. Webhook event subscriptions control which notifications the app receives. These are separate choices: an app may receive issue events without having permission to modify issues.
Exercise
An integration needs to read issues from two repositories and receive issue events. What should you configure?
Show answer
Give the app Issues: read permission, subscribe to issue events, and install it only on those two repositories.