ghrepos
Version:
Interact with the GitHub repos API
120 lines (72 loc) • 3.49 kB
Markdown
**A Node.js library to interact with the GitHub repos API**
[](https://nodei.co/npm/ghrepos/)
- Node.js >= 20
```js
import * as ghrepos from 'ghrepos'
const auth = { token: 'your-github-token' }
// list all repos for a user
const repos = await ghrepos.listUser(auth, 'rvagg')
console.log(repos)
// list all repos for an org
const orgRepos = await ghrepos.listOrg(auth, 'nodejs')
console.log(orgRepos)
// get branch data
const branch = await ghrepos.getBranch(auth, 'nodejs', 'node', 'main')
console.log(branch)
// get commit comments
const comments = await ghrepos.getCommitComments(auth, 'nodejs', 'node', '75318e46b')
console.log(comments)
```
The auth data is compatible with [ghauth](https://github.com/rvagg/ghauth) so you can connect them together:
```js
import ghauth from 'ghauth'
import * as ghrepos from 'ghrepos'
const auth = await ghauth({
configName: 'repo-lister',
scopes: ['user']
})
const repos = await ghrepos.listUser(auth, 'rvagg')
console.log('Repos for rvagg:')
repos.forEach((r) => {
console.log('%s: %s (fork: %s)', r.name, r.description, r.fork)
})
```
All methods return Promises.
List all repos for a user. If `user` is falsy, lists repos for the authenticated user.
List all repos for an organisation.
Get git ref data for all refs in a repo.
List git tags for a repo.
List git branches for a repo.
List git commits for a repo.
Get git ref data for a particular ref string. The `refs/` prefix is automatically stripped if present.
Get git branch data for a given branch name.
Get git commit data for a given SHA.
Get commit comments for a given SHA.
Creates a function that lists sub-resources under `/repos/:org/:repo/:type`, e.g. `'issues'`, `'pulls'` or `'releases'`. The returned function has the signature: `async function (auth, org, repo, options)`.
### ghrepos.baseUrl(org, repo, options)
Returns the base API URL for a repo: `https://api.github.com/repos/:org/:repo`.
## Authentication
See [ghauth](https://github.com/rvagg/ghauth) for an easy way to obtain and cache GitHub authentication tokens. The `auth` object returned by ghauth is directly compatible with all ghrepos methods.
## See also
* [ghissues](https://github.com/rvagg/ghissues) - interact with the GitHub issues API
* [ghusers](https://github.com/rvagg/ghusers) - interact with the GitHub users API
* [ghteams](https://github.com/rvagg/ghteams) - interact with the GitHub teams API
* [ghpulls](https://github.com/rvagg/ghpulls) - interact with the GitHub pull requests API
* [ghauth](https://github.com/rvagg/ghauth) - GitHub authentication
## License
**ghrepos** is Copyright (c) 2014-2025 Rod Vagg [@rvagg](https://github.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.