Skip to content

A number of misc improvements #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-buttons-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@s0/ghcommit": minor
---

Rename repository argument to repo, and deprecate old argument
5 changes: 5 additions & 0 deletions .changeset/long-rabbits-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@s0/ghcommit": minor
---

Allow message to be specified as single string
2 changes: 1 addition & 1 deletion .github/workflows/release-and-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
run:
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" >> .npmrc
- name: Run Changeset Workflow
uses: changesets/action@aba318e9165b45b7948c60273e0b72fce0a64eb9 # v1.4.7
uses: s0/changesets-action@63d3e3fda2c00696414ca2d6683e046289c13fd8 # v2.1.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ All the functions below accept a single object as its argument, and share the fo
{
octokit: GitHubClient;
owner: string;
repository: string;
repo: string;
branch: string;
/**
* Push the commit even if the branch exists and does not match what was
Expand All @@ -69,7 +69,7 @@ All the functions below accept a single object as its argument, and share the fo
/**
* The commit message
*/
message: CommitMessage;
message: string | CommitMessage;
log?: Logger;
}
```
Expand Down Expand Up @@ -111,12 +111,9 @@ const octokit = getOctokit(process.env.GITHUB_TOKEN);
// e.g. if you're just using @ations/checkout
await commitChangesFromRepo({
octokit,
owner: "my-org",
repository: "my-repo",
...context.repo,
branch: "new-branch-to-create",
message: {
headline: "[chore] do something",
},
message: "[chore] do something",
});

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

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

```ts
import { getOctokit } from "@actions/github";
import { context, getOctokit } from "@actions/github";
import { commitFilesFromDirectory } from "@s0/ghcommit/fs";

const octokit = getOctokit(process.env.GITHUB_TOKEN);
Expand All @@ -192,12 +187,9 @@ const octokit = getOctokit(process.env.GITHUB_TOKEN);
// based on the main branch
await commitFilesFromDirectory({
octokit,
owner: "my-org",
repository: "my-repo",
...context.repo,
branch: "new-branch-to-create",
message: {
headline: "[chore] do something",
},
message: "[chore] do something",
base: {
branch: "main",
},
Expand All @@ -210,12 +202,9 @@ await commitFilesFromDirectory({
// Push just the index.html file to a new branch called docs, based off the tag v1.0.0
await commitFilesFromDirectory({
octokit,
owner: "my-org",
repository: "my-repo",
...context.repo,
branch: "docs",
message: {
headline: "[chore] do something",
},
message: "[chore] do something",
force: true, // Overwrite any existing branch
base: {
tag: "v1.0.0",
Expand Down Expand Up @@ -255,20 +244,17 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
Example:

```ts
import { getOctokit } from "@actions/github";
import { context, getOctokit } from "@actions/github";
import { commitFilesFromBuffers } from "@s0/ghcommit/node";

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

// Add a file called hello-world
await commitFilesFromBuffers({
octokit,
owner: "my-org",
repository: "my-repo",
...context.repo,
branch: "new-branch-to-create",
message: {
headline: "[chore] do something",
},
message: "[chore] do something",
base: {
branch: "main",
},
Expand Down
19 changes: 17 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CommitFilesResult,
GitBase,
} from "./interface.js";
import { CommitMessage } from "./github/graphql/generated/types.js";

const getBaseRef = (base: GitBase): string => {
if ("branch" in base) {
Expand Down Expand Up @@ -47,6 +48,7 @@ const getOidFromRef = (
export const commitFilesFromBase64 = async ({
octokit,
owner,
repo,
repository,
branch,
base,
Expand All @@ -58,11 +60,16 @@ export const commitFilesFromBase64 = async ({
const repositoryNameWithOwner = `${owner}/${repository}`;
const baseRef = getBaseRef(base);
const targetRef = `refs/heads/${branch}`;
repo = repo ?? repository;

if (!repo) {
throw new Error(`Argument 'repo' must be provided`);
}

log?.debug(`Getting repo info ${repositoryNameWithOwner}`);
const info = await getRepositoryMetadata(octokit, {
owner,
name: repository,
repo,
baseRef,
targetRef,
});
Expand Down Expand Up @@ -155,14 +162,22 @@ export const commitFilesFromBase64 = async ({
}
}

const finalMessage: CommitMessage =
typeof message === "string"
? {
headline: message.split("\n")[0]?.trim() ?? "",
body: message.split("\n").slice(1).join("\n").trim(),
}
: message;

await log?.debug(`Creating commit on branch ${branch}`);
const createCommitMutation: CreateCommitOnBranchMutationVariables = {
input: {
branch: {
id: refId,
},
expectedHeadOid: baseOid,
message,
message: finalMessage,
fileChanges,
},
};
Expand Down
9 changes: 5 additions & 4 deletions src/github/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import type {
const GET_REPOSITORY_METADATA = /* GraphQL */ `
query getRepositoryMetadata(
$owner: String!
$name: String!
$repo: String!
$baseRef: String!
$targetRef: String!
) {
repository(owner: $owner, name: $name) {
repository(owner: $owner, name: $repo) {
id
baseRef: ref(qualifiedName: $baseRef) {
id
Expand Down Expand Up @@ -89,11 +89,11 @@ const CREATE_COMMIT_ON_BRANCH = /* GraphQL */ `
const GET_REF_TREE = /* GraphQL */ `
query getRefTree(
$owner: String!
$name: String!
$repo: String!
$ref: String!
$path: String!
) {
repository(owner: $owner, name: $name) {
repository(owner: $owner, name: $repo) {
ref(qualifiedName: $ref) {
target {
... on Commit {
Expand All @@ -105,6 +105,7 @@ const GET_REF_TREE = /* GraphQL */ `
oid
}
}
message
file(path: $path) {
oid
}
Expand Down
6 changes: 4 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export type GitBase =
export interface CommitFilesBasedArgs {
octokit: GitHubClient;
owner: string;
repository: string;
repo?: string;
/** @deprecated use {@link repo} instead */
repository?: string;
branch: string;
/**
* Push the commit even if the branch exists and does not match what was
Expand All @@ -34,7 +36,7 @@ export interface CommitFilesBasedArgs {
/**
* The commit message
*/
message: CommitMessage;
message: string | CommitMessage;
log?: Logger;
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/integration/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ if (!GITHUB_TOKEN) {

const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;

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

export const ENV = {
GITHUB_TOKEN,
};

export const REPO = { owner, repository };
export const REPO = { owner, repo };

export const log = pino({
level: process.env.RUNNER_DEBUG === "1" ? "debug" : "info",
Expand Down
11 changes: 5 additions & 6 deletions src/test/integration/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const expectBranchHasFile = async ({
if (oid === null) {
expect(() =>
getRefTreeQuery(octokit, {
owner: REPO.owner,
name: REPO.repository,
...REPO,
ref: `refs/heads/${branch}`,
path,
}),
Expand All @@ -40,8 +39,7 @@ const expectBranchHasFile = async ({
}
const ref = (
await getRefTreeQuery(octokit, {
owner: REPO.owner,
name: REPO.repository,
...REPO,
ref: `refs/heads/${branch}`,
path,
})
Expand All @@ -67,8 +65,7 @@ const expectParentHasOid = async ({
}) => {
const commit = (
await getRefTreeQuery(octokit, {
owner: REPO.owner,
name: REPO.repository,
...REPO,
ref: `refs/heads/${branch}`,
path: "README.md",
})
Expand Down Expand Up @@ -159,6 +156,7 @@ describe("git", () => {

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

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

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

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