Skip to content

Commit

Permalink
feat: author and committer options (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
  • Loading branch information
tylermichael and gr2m committed Jan 9, 2023
1 parent 81af389 commit 91f6fed
Show file tree
Hide file tree
Showing 6 changed files with 2,557 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
node-version: ${{ matrix.node_version }}
cache: npm
- run: npm ci
- run: jest --coverage
- run: npm test

test:
runs-on: ubuntu-latest
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ octokit
},
commit:
"creating file1.txt, file2.png, deleting file3.txt, updating file4.txt (if it exists), file5.sh",
/* optional: if not passed, will be the authenticated user and the current date */
author: {
name: "Author LastName",
email: "Author.LastName@acme.com",
date: new Date().toISOString(), // must be ISO date string
},
/* optional: if not passed, will use the information set in author */
committer: {
name: "Committer LastName",
email: "Committer.LastName@acme.com",
date: new Date().toISOString(), // must be ISO date string
},
},
],
})
Expand Down
4 changes: 3 additions & 1 deletion src/create-commit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Changes, State } from "./types";
import type { Committer, Changes, State } from "./types";

export async function createCommit(
state: Required<State>,
Expand All @@ -20,6 +20,8 @@ export async function createCommit(
owner: ownerOrFork,
repo,
message,
author: changes.author,
committer: changes.committer,
tree: state.latestCommitTreeSha,
parents: [latestCommitSha],
}
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type Changes = {
};
emptyCommit?: boolean | string;
commit: string;
committer?: Committer;
author?: Author | undefined;
};

// https://developer.github.com/v3/git/blobs/#parameters
Expand Down Expand Up @@ -58,3 +60,15 @@ export type State = {
latestCommitTreeSha?: string;
treeSha?: string;
};

export type Committer = {
name?: string;
email?: string;
date?: string;
};

export type Author = {
name: string;
email: string;
date?: string;
};
86 changes: 86 additions & 0 deletions test/create-commits-with-author-and-committer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Octokit as Core } from "@octokit/core";
import { RequestError } from "@octokit/request-error";

import { createPullRequest } from "../src";
const Octokit = Core.plugin(createPullRequest);

test("author and committer", async () => {
const fixtures = require("./fixtures/create-commits-with-author-and-committer");
const fixturePr = fixtures[fixtures.length - 1].response;
const octokit = new Octokit();

octokit.hook.wrap("request", (_, options) => {
const currentFixtures = fixtures.shift();
const {
baseUrl,
method,
url,
request,
headers,
mediaType,
draft,
...params
} = options;

expect(
`${currentFixtures.request.method} ${currentFixtures.request.url}`
).toEqual(`${options.method} ${options.url}`);

Object.keys(params).forEach((paramName) => {
expect(currentFixtures.request[paramName]).toStrictEqual(
params[paramName]
);
});

if (currentFixtures.response.status >= 400) {
throw new RequestError("Error", currentFixtures.response.status, {
request: currentFixtures.request,
headers: currentFixtures.response.headers,
});
}

return currentFixtures.response;
});

const pr = await octokit.createPullRequest({
owner: "gr2m",
repo: "sandbox",
title: "One comes, one goes",
body: "because",
head: "test-branch-tv12s",
changes: [
{
files: {
"path/to/file1.txt": "Content for file1",
"path/to/file2.txt": "Content for file2",
},
author: {
name: "Author LastName",
email: "Author.LastName@acme.com",
date: "2022-12-06T19:58:39.672Z",
},
committer: {
name: "Committer LastName",
email: "Committer.LastName@acme.com",
date: "2022-12-06T19:58:39.672Z",
},
commit: "why",
},
{
files: {
"path/to/file1.txt": "New content",
"path/to/file4.txt": "Content for file4",
},
committer: {
name: "Committer Smith",
email: "Committer.Smith@acme.com",
date: "2022-12-06T19:58:39.672Z",
},
commit: "Make a fix",
},
],
});

expect(pr).toStrictEqual(fixturePr);
expect(fixtures.length).toEqual(0);
});
Loading

0 comments on commit 91f6fed

Please sign in to comment.