Skip to content

Commit 6c0f54b

Browse files
authored
feat(repos): add API to fetch repository contributors (#19)
1 parent 1ab2633 commit 6c0f54b

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

docs/api/repos.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { repos } from "@openally/github.sdk";
88
// Collect all tags
99
const tags = await repos.OpenAlly["github.sdk"].tags();
1010

11+
// Collect all contributors
12+
const contributors = await repos.OpenAlly["github.sdk"].contributors();
13+
1114
// Stream pull requests page by page
1215
for await (const pr of repos.nodejs.node.pulls()) {
1316
console.log(pr.number, pr.title);
@@ -64,6 +67,14 @@ Lists commits.
6467

6568
> GitHub docs: [List commits](https://docs.github.com/en/rest/commits/commits#list-commits)
6669
70+
### `.contributors()`
71+
72+
Returns `ApiEndpoint<Contributor>`.
73+
74+
Lists contributors to the repository, sorted by number of commits.
75+
76+
> GitHub docs: [List repository contributors](https://docs.github.com/en/rest/repos/repos#list-repository-contributors)
77+
6778
### `.workflows()`
6879

6980
Returns `ApiEndpoint<Workflow>`.

src/api/repos.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
PullRequest,
77
Issue,
88
Commit,
9+
Contributor,
910
Workflow,
1011
WorkflowRun,
1112
Job,
@@ -22,6 +23,7 @@ type RepoEndpointMethods = {
2223
pulls: () => ApiEndpoint<PullRequest>;
2324
issues: () => ApiEndpoint<Issue>;
2425
commits: () => ApiEndpoint<Commit>;
26+
contributors: () => ApiEndpoint<Contributor>;
2527
workflows: () => ApiEndpoint<Workflow>;
2628
workflowRuns: (workflowId: string | number) => ApiEndpoint<WorkflowRun>;
2729
runJobs: (runId: number) => ApiEndpoint<Job>;
@@ -44,6 +46,7 @@ function createRepoProxy(
4446
pulls: () => new ApiEndpoint<PullRequest>(`/repos/${owner}/${repo}/pulls`, config),
4547
issues: () => new ApiEndpoint<Issue>(`/repos/${owner}/${repo}/issues`, config),
4648
commits: () => new ApiEndpoint<Commit>(`/repos/${owner}/${repo}/commits`, config),
49+
contributors: () => new ApiEndpoint<Contributor>(`/repos/${owner}/${repo}/contributors`, config),
4750
workflows: () => new ApiEndpoint<Workflow>(
4851
`/repos/${owner}/${repo}/actions/workflows`,
4952
{

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type Tag = Endpoints["GET /repos/{owner}/{repo}/tags"]["response"]["data"
1919
export type PullRequest = Endpoints["GET /repos/{owner}/{repo}/pulls"]["response"]["data"][number];
2020
export type Issue = Endpoints["GET /repos/{owner}/{repo}/issues"]["response"]["data"][number];
2121
export type Commit = Endpoints["GET /repos/{owner}/{repo}/commits"]["response"]["data"][number];
22+
export type Contributor = Endpoints["GET /repos/{owner}/{repo}/contributors"]["response"]["data"][number];
2223
export type Workflow = Endpoints["GET /repos/{owner}/{repo}/actions/workflows"]["response"]["data"]["workflows"][number];
2324
export type WorkflowRun = Endpoints[
2425
"GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs"

test/repos.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe("Repos API", () => {
3737
assert.ok(methods.pulls() instanceof ApiEndpoint);
3838
assert.ok(methods.issues() instanceof ApiEndpoint);
3939
assert.ok(methods.commits() instanceof ApiEndpoint);
40+
assert.ok(methods.contributors() instanceof ApiEndpoint);
4041
assert.ok(methods.workflows() instanceof ApiEndpoint);
4142
});
4243

@@ -128,6 +129,19 @@ describe("Repos API", () => {
128129
assert.deepEqual(result, [{ number: 5, title: "Bug report" }]);
129130
});
130131

132+
it("should fetch contributors for a repo", async() => {
133+
mockAgent
134+
.get(kGithubOrigin)
135+
.intercept({ path: "/repos/octocat/hello-world/contributors", method: "GET" })
136+
.reply(200, JSON.stringify([{ login: "octocat", contributions: 42 }]), {
137+
headers: { "content-type": "application/json" }
138+
});
139+
140+
const result = await repos.octocat["hello-world"].contributors().all();
141+
142+
assert.deepEqual(result, [{ login: "octocat", contributions: 42 }]);
143+
});
144+
131145
it("should fetch commits for a repo", async() => {
132146
mockAgent
133147
.get(kGithubOrigin)

0 commit comments

Comments
 (0)