Skip to content

Commit 477e92a

Browse files
committed
Refactor internal octokit usage
1 parent e2b975d commit 477e92a

3 files changed

Lines changed: 23 additions & 38 deletions

File tree

src/core.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { GetRepositoryMetadataQuery } from "./github/graphql/generated/oper
22
import {
33
createCommitOnBranchQuery,
44
getRepositoryMetadata,
5+
type Octokit,
56
} from "./github/graphql/queries.ts";
67
import type { CommitChangesOptions, CommitChangesResult } from "./types.ts";
78
import { normalizeCommitMessage, resolveGitRef } from "./utils.ts";
@@ -17,7 +18,7 @@ type CreateCommit = (
1718
* Works in Node.js and browsers.
1819
*/
1920
export async function commitChanges({
20-
octokit,
21+
octokit: partialOctokit,
2122
owner,
2223
repo,
2324
branch,
@@ -26,6 +27,7 @@ export async function commitChanges({
2627
message,
2728
fileChanges,
2829
}: CommitChangesOptions): Promise<CommitChangesResult> {
30+
const octokit = partialOctokit as Octokit;
2931
const baseRef = resolveGitRef(base);
3032

3133
const info = await getRepositoryMetadata(octokit, {
@@ -168,7 +170,10 @@ async function createOrForceUpdateTemporaryBranch({
168170
repo,
169171
tempBranch,
170172
baseSha,
171-
}: Pick<CommitChangesOptions, "octokit" | "owner" | "repo"> & {
173+
}: {
174+
octokit: Octokit;
175+
owner: string;
176+
repo: string;
172177
tempBranch: string;
173178
baseSha: string;
174179
}) {

src/github/graphql/queries.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1-
// Octokit types are messy. To avoid adding any (peer)dependencies we rely on TS structural typing here
2-
export type Octokit = {
3-
graphql: <T>(
4-
query: string,
5-
variables?: Record<string, unknown>,
6-
) => Promise<T>;
7-
rest: {
8-
git: {
9-
createRef: (params: {
10-
owner: string;
11-
repo: string;
12-
ref: string;
13-
sha: string;
14-
}) => Promise<{ data: { node_id?: string } }>;
15-
updateRef: (params: {
16-
owner: string;
17-
repo: string;
18-
ref: string;
19-
sha: string;
20-
force?: boolean;
21-
}) => Promise<{ data: { node_id?: string } }>;
22-
deleteRef: (params: {
23-
owner: string;
24-
repo: string;
25-
ref: string;
26-
}) => Promise<unknown>;
27-
getRef?: (params: {
28-
owner: string;
29-
repo: string;
30-
ref: string;
31-
}) => Promise<unknown>;
32-
};
33-
};
34-
};
1+
import type { getOctokit } from "@actions/github";
2+
3+
export type Octokit = ReturnType<typeof getOctokit>;
4+
5+
/**
6+
* Rough shape of the Octokit instance we need so we don't depend on the full
7+
* dependency and types.
8+
*/
9+
export interface PartialOctokit {
10+
request: unknown;
11+
graphql: unknown;
12+
rest: unknown;
13+
paginate: unknown;
14+
}
3515

3616
import type {
3717
CreateCommitOnBranchMutation,

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
CommitMessage,
33
FileChanges,
44
} from "./github/graphql/generated/types.ts";
5-
import type { Octokit } from "./github/graphql/queries.ts";
5+
import type { PartialOctokit } from "./github/graphql/queries.ts";
66

77
export type GitRef = { branch: string } | { tag: string } | { commit: string };
88

@@ -12,7 +12,7 @@ export interface CommitChangesOptions {
1212
* from `@octokit/core`, `@octokit/plugin-rest-endpoint-methods`, and
1313
* `@octokit/plugin-paginate-rest`.
1414
*/
15-
octokit: Octokit;
15+
octokit: PartialOctokit;
1616
/**
1717
* The owner of the repository.
1818
*/

0 commit comments

Comments
 (0)