diff --git a/docs/api/repos.md b/docs/api/repos.md index 7d8089a..e64ed68 100644 --- a/docs/api/repos.md +++ b/docs/api/repos.md @@ -8,6 +8,9 @@ import { repos } from "@openally/github.sdk"; // Collect all tags const tags = await repos.OpenAlly["github.sdk"].tags(); +// Collect all contributors +const contributors = await repos.OpenAlly["github.sdk"].contributors(); + // Stream pull requests page by page for await (const pr of repos.nodejs.node.pulls()) { console.log(pr.number, pr.title); @@ -64,6 +67,14 @@ Lists commits. > GitHub docs: [List commits](https://docs.github.com/en/rest/commits/commits#list-commits) +### `.contributors()` + +Returns `ApiEndpoint`. + +Lists contributors to the repository, sorted by number of commits. + +> GitHub docs: [List repository contributors](https://docs.github.com/en/rest/repos/repos#list-repository-contributors) + ### `.workflows()` Returns `ApiEndpoint`. diff --git a/src/api/repos.ts b/src/api/repos.ts index f760b70..9e5ad6a 100644 --- a/src/api/repos.ts +++ b/src/api/repos.ts @@ -6,6 +6,7 @@ import type { PullRequest, Issue, Commit, + Contributor, Workflow, WorkflowRun, Job, @@ -22,6 +23,7 @@ type RepoEndpointMethods = { pulls: () => ApiEndpoint; issues: () => ApiEndpoint; commits: () => ApiEndpoint; + contributors: () => ApiEndpoint; workflows: () => ApiEndpoint; workflowRuns: (workflowId: string | number) => ApiEndpoint; runJobs: (runId: number) => ApiEndpoint; @@ -44,6 +46,7 @@ function createRepoProxy( pulls: () => new ApiEndpoint(`/repos/${owner}/${repo}/pulls`, config), issues: () => new ApiEndpoint(`/repos/${owner}/${repo}/issues`, config), commits: () => new ApiEndpoint(`/repos/${owner}/${repo}/commits`, config), + contributors: () => new ApiEndpoint(`/repos/${owner}/${repo}/contributors`, config), workflows: () => new ApiEndpoint( `/repos/${owner}/${repo}/actions/workflows`, { diff --git a/src/types.ts b/src/types.ts index 7d8b6b3..be80925 100644 --- a/src/types.ts +++ b/src/types.ts @@ -19,6 +19,7 @@ export type Tag = Endpoints["GET /repos/{owner}/{repo}/tags"]["response"]["data" export type PullRequest = Endpoints["GET /repos/{owner}/{repo}/pulls"]["response"]["data"][number]; export type Issue = Endpoints["GET /repos/{owner}/{repo}/issues"]["response"]["data"][number]; export type Commit = Endpoints["GET /repos/{owner}/{repo}/commits"]["response"]["data"][number]; +export type Contributor = Endpoints["GET /repos/{owner}/{repo}/contributors"]["response"]["data"][number]; export type Workflow = Endpoints["GET /repos/{owner}/{repo}/actions/workflows"]["response"]["data"]["workflows"][number]; export type WorkflowRun = Endpoints[ "GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs" diff --git a/test/repos.spec.ts b/test/repos.spec.ts index c92acd3..f7beb68 100644 --- a/test/repos.spec.ts +++ b/test/repos.spec.ts @@ -37,6 +37,7 @@ describe("Repos API", () => { assert.ok(methods.pulls() instanceof ApiEndpoint); assert.ok(methods.issues() instanceof ApiEndpoint); assert.ok(methods.commits() instanceof ApiEndpoint); + assert.ok(methods.contributors() instanceof ApiEndpoint); assert.ok(methods.workflows() instanceof ApiEndpoint); }); @@ -128,6 +129,19 @@ describe("Repos API", () => { assert.deepEqual(result, [{ number: 5, title: "Bug report" }]); }); + it("should fetch contributors for a repo", async() => { + mockAgent + .get(kGithubOrigin) + .intercept({ path: "/repos/octocat/hello-world/contributors", method: "GET" }) + .reply(200, JSON.stringify([{ login: "octocat", contributions: 42 }]), { + headers: { "content-type": "application/json" } + }); + + const result = await repos.octocat["hello-world"].contributors().all(); + + assert.deepEqual(result, [{ login: "octocat", contributions: 42 }]); + }); + it("should fetch commits for a repo", async() => { mockAgent .get(kGithubOrigin)