Skip to content

Commit a704fb3

Browse files
authored
A number of misc improvements (#16)
* Test new changeset action * chore: ensure git tests don't leave behind branches * Rename repository argument to repo Allow for direct destructuring of repo context in actions by naming the arguments consistently with what github provides. * Allow message to be specified as single string
1 parent 6529a43 commit a704fb3

File tree

11 files changed

+153
-53
lines changed

11 files changed

+153
-53
lines changed

.changeset/empty-buttons-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@s0/ghcommit": minor
3+
---
4+
5+
Rename repository argument to repo, and deprecate old argument

.changeset/long-rabbits-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@s0/ghcommit": minor
3+
---
4+
5+
Allow message to be specified as single string

.github/workflows/release-and-publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run:
3939
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" >> .npmrc
4040
- name: Run Changeset Workflow
41-
uses: changesets/action@aba318e9165b45b7948c60273e0b72fce0a64eb9 # v1.4.7
41+
uses: s0/changesets-action@63d3e3fda2c00696414ca2d6683e046289c13fd8 # v2.1.0
4242
env:
4343
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4444
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ All the functions below accept a single object as its argument, and share the fo
5959
{
6060
octokit: GitHubClient;
6161
owner: string;
62-
repository: string;
62+
repo: string;
6363
branch: string;
6464
/**
6565
* Push the commit even if the branch exists and does not match what was
@@ -69,7 +69,7 @@ All the functions below accept a single object as its argument, and share the fo
6969
/**
7070
* The commit message
7171
*/
72-
message: CommitMessage;
72+
message: string | CommitMessage;
7373
log?: Logger;
7474
}
7575
```
@@ -111,12 +111,9 @@ const octokit = getOctokit(process.env.GITHUB_TOKEN);
111111
// e.g. if you're just using @ations/checkout
112112
await commitChangesFromRepo({
113113
octokit,
114-
owner: "my-org",
115-
repository: "my-repo",
114+
...context.repo,
116115
branch: "new-branch-to-create",
117-
message: {
118-
headline: "[chore] do something",
119-
},
116+
message: "[chore] do something",
120117
});
121118

122119
// Commit & push the files from a specific directory
@@ -126,21 +123,19 @@ await commitChangesFromRepo({
126123
owner: "my-org",
127124
repository: "my-repo",
128125
branch: "another-new-branch-to-create",
129-
message: {
130-
headline: "[chore] do something else",
131-
},
126+
message: "[chore] do something else\n\nsome more details",
132127
repoDirectory: "/tmp/some-repo",
133128
});
134129

135130
// Commit & push the files from the current directory,
136131
// but ensure changes from any locally-made commits are also included
137132
await commitChangesFromRepo({
138133
octokit,
139-
owner: "my-org",
140-
repository: "my-repo",
134+
...context.repo,
141135
branch: "another-new-branch-to-create",
142136
message: {
143137
headline: "[chore] do something else",
138+
body: "some more details",
144139
},
145140
base: {
146141
// This will be the original sha from the workflow run,
@@ -183,7 +178,7 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
183178
Example:
184179

185180
```ts
186-
import { getOctokit } from "@actions/github";
181+
import { context, getOctokit } from "@actions/github";
187182
import { commitFilesFromDirectory } from "@s0/ghcommit/fs";
188183

189184
const octokit = getOctokit(process.env.GITHUB_TOKEN);
@@ -192,12 +187,9 @@ const octokit = getOctokit(process.env.GITHUB_TOKEN);
192187
// based on the main branch
193188
await commitFilesFromDirectory({
194189
octokit,
195-
owner: "my-org",
196-
repository: "my-repo",
190+
...context.repo,
197191
branch: "new-branch-to-create",
198-
message: {
199-
headline: "[chore] do something",
200-
},
192+
message: "[chore] do something",
201193
base: {
202194
branch: "main",
203195
},
@@ -210,12 +202,9 @@ await commitFilesFromDirectory({
210202
// Push just the index.html file to a new branch called docs, based off the tag v1.0.0
211203
await commitFilesFromDirectory({
212204
octokit,
213-
owner: "my-org",
214-
repository: "my-repo",
205+
...context.repo,
215206
branch: "docs",
216-
message: {
217-
headline: "[chore] do something",
218-
},
207+
message: "[chore] do something",
219208
force: true, // Overwrite any existing branch
220209
base: {
221210
tag: "v1.0.0",
@@ -255,20 +244,17 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
255244
Example:
256245

257246
```ts
258-
import { getOctokit } from "@actions/github";
247+
import { context, getOctokit } from "@actions/github";
259248
import { commitFilesFromBuffers } from "@s0/ghcommit/node";
260249

261250
const octokit = getOctokit(process.env.GITHUB_TOKEN);
262251

263252
// Add a file called hello-world
264253
await commitFilesFromBuffers({
265254
octokit,
266-
owner: "my-org",
267-
repository: "my-repo",
255+
...context.repo,
268256
branch: "new-branch-to-create",
269-
message: {
270-
headline: "[chore] do something",
271-
},
257+
message: "[chore] do something",
272258
base: {
273259
branch: "main",
274260
},

src/core.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CommitFilesResult,
1414
GitBase,
1515
} from "./interface.js";
16+
import { CommitMessage } from "./github/graphql/generated/types.js";
1617

1718
const getBaseRef = (base: GitBase): string => {
1819
if ("branch" in base) {
@@ -47,6 +48,7 @@ const getOidFromRef = (
4748
export const commitFilesFromBase64 = async ({
4849
octokit,
4950
owner,
51+
repo,
5052
repository,
5153
branch,
5254
base,
@@ -58,11 +60,16 @@ export const commitFilesFromBase64 = async ({
5860
const repositoryNameWithOwner = `${owner}/${repository}`;
5961
const baseRef = getBaseRef(base);
6062
const targetRef = `refs/heads/${branch}`;
63+
repo = repo ?? repository;
64+
65+
if (!repo) {
66+
throw new Error(`Argument 'repo' must be provided`);
67+
}
6168

6269
log?.debug(`Getting repo info ${repositoryNameWithOwner}`);
6370
const info = await getRepositoryMetadata(octokit, {
6471
owner,
65-
name: repository,
72+
repo,
6673
baseRef,
6774
targetRef,
6875
});
@@ -155,14 +162,22 @@ export const commitFilesFromBase64 = async ({
155162
}
156163
}
157164

165+
const finalMessage: CommitMessage =
166+
typeof message === "string"
167+
? {
168+
headline: message.split("\n")[0]?.trim() ?? "",
169+
body: message.split("\n").slice(1).join("\n").trim(),
170+
}
171+
: message;
172+
158173
await log?.debug(`Creating commit on branch ${branch}`);
159174
const createCommitMutation: CreateCommitOnBranchMutationVariables = {
160175
input: {
161176
branch: {
162177
id: refId,
163178
},
164179
expectedHeadOid: baseOid,
165-
message,
180+
message: finalMessage,
166181
fileChanges,
167182
},
168183
};

src/github/graphql/queries.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import type {
2020
const GET_REPOSITORY_METADATA = /* GraphQL */ `
2121
query getRepositoryMetadata(
2222
$owner: String!
23-
$name: String!
23+
$repo: String!
2424
$baseRef: String!
2525
$targetRef: String!
2626
) {
27-
repository(owner: $owner, name: $name) {
27+
repository(owner: $owner, name: $repo) {
2828
id
2929
baseRef: ref(qualifiedName: $baseRef) {
3030
id
@@ -89,11 +89,11 @@ const CREATE_COMMIT_ON_BRANCH = /* GraphQL */ `
8989
const GET_REF_TREE = /* GraphQL */ `
9090
query getRefTree(
9191
$owner: String!
92-
$name: String!
92+
$repo: String!
9393
$ref: String!
9494
$path: String!
9595
) {
96-
repository(owner: $owner, name: $name) {
96+
repository(owner: $owner, name: $repo) {
9797
ref(qualifiedName: $ref) {
9898
target {
9999
... on Commit {
@@ -105,6 +105,7 @@ const GET_REF_TREE = /* GraphQL */ `
105105
oid
106106
}
107107
}
108+
message
108109
file(path: $path) {
109110
oid
110111
}

src/interface.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export type GitBase =
2424
export interface CommitFilesBasedArgs {
2525
octokit: GitHubClient;
2626
owner: string;
27-
repository: string;
27+
repo?: string;
28+
/** @deprecated use {@link repo} instead */
29+
repository?: string;
2830
branch: string;
2931
/**
3032
* Push the commit even if the branch exists and does not match what was
@@ -34,7 +36,7 @@ export interface CommitFilesBasedArgs {
3436
/**
3537
* The commit message
3638
*/
37-
message: CommitMessage;
39+
message: string | CommitMessage;
3840
log?: Logger;
3941
}
4042

src/test/integration/env.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ if (!GITHUB_TOKEN) {
1818

1919
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;
2020

21-
const [owner, repository] = GITHUB_REPOSITORY?.split("/") || [];
22-
if (!owner || !repository) {
21+
const [owner, repo] = GITHUB_REPOSITORY?.split("/") || [];
22+
if (!owner || !repo) {
2323
throw new Error("GITHUB_REPOSITORY must be set");
2424
}
2525

2626
export const ENV = {
2727
GITHUB_TOKEN,
2828
};
2929

30-
export const REPO = { owner, repository };
30+
export const REPO = { owner, repo };
3131

3232
export const log = pino({
3333
level: process.env.RUNNER_DEBUG === "1" ? "debug" : "info",

src/test/integration/git.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const expectBranchHasFile = async ({
3030
if (oid === null) {
3131
expect(() =>
3232
getRefTreeQuery(octokit, {
33-
owner: REPO.owner,
34-
name: REPO.repository,
33+
...REPO,
3534
ref: `refs/heads/${branch}`,
3635
path,
3736
}),
@@ -40,8 +39,7 @@ const expectBranchHasFile = async ({
4039
}
4140
const ref = (
4241
await getRefTreeQuery(octokit, {
43-
owner: REPO.owner,
44-
name: REPO.repository,
42+
...REPO,
4543
ref: `refs/heads/${branch}`,
4644
path,
4745
})
@@ -67,8 +65,7 @@ const expectParentHasOid = async ({
6765
}) => {
6866
const commit = (
6967
await getRefTreeQuery(octokit, {
70-
owner: REPO.owner,
71-
name: REPO.repository,
68+
...REPO,
7269
ref: `refs/heads/${branch}`,
7370
path: "README.md",
7471
})
@@ -159,6 +156,7 @@ describe("git", () => {
159156

160157
it("should correctly commit all changes", async () => {
161158
const branch = `${TEST_BRANCH_PREFIX}-multiple-changes`;
159+
branches.push(branch);
162160

163161
await fs.promises.mkdir(testDir, { recursive: true });
164162
const repoDirectory = path.join(testDir, "repo-1");
@@ -213,6 +211,7 @@ describe("git", () => {
213211

214212
it("should correctly be able to base changes off specific commit", async () => {
215213
const branch = `${TEST_BRANCH_PREFIX}-specific-base`;
214+
branches.push(branch);
216215

217216
await fs.promises.mkdir(testDir, { recursive: true });
218217
const repoDirectory = path.join(testDir, "repo-2");

0 commit comments

Comments
 (0)