Skip to content

Commit b6e43ae

Browse files
authored
Refactor modules and use jest for tests (#6)
1 parent 54fa882 commit b6e43ae

File tree

9 files changed

+156
-75
lines changed

9 files changed

+156
-75
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- run: pnpm build
2626
- run: pnpm lint
2727
- run: pnpm format:check
28-
- run: pnpm test:ci
28+
- run: pnpm test:integration
2929
env:
3030
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3131
HEAD_OID: ${{ github.base_ref }}

jest.integration.config.cjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
transform: {
5+
"^.+\\.ts$": [
6+
"ts-jest",
7+
{
8+
tsconfig: "tsconfig.json",
9+
},
10+
],
11+
},
12+
/**
13+
* Configure jest to be compatible with the node-js requirement
14+
* of having `.js` extension in the import statement.
15+
*/
16+
moduleNameMapper: {
17+
"^(.+).js$": "$1",
18+
},
19+
testMatch: ["<rootDir>/src/test/integration/**/*.test.ts"],
20+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint": "eslint . --max-warnings 0",
3434
"test": "jest",
3535
"test:watch": "jest --watch",
36-
"test:ci": "ts-node src/test/ci"
36+
"test:integration": "jest --config jest.integration.config.cjs"
3737
},
3838
"devDependencies": {
3939
"@actions/github": "^6.0.0",

src/local.ts renamed to src/fs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { promises as fs } from "fs";
22
import * as path from "path";
3-
import {
3+
import type {
44
CommitMessage,
55
FileAddition,
66
FileDeletion,
@@ -11,8 +11,8 @@ import {
1111
getRepositoryMetadata,
1212
GitHubClient,
1313
} from "./github/graphql/queries";
14-
import { Logger } from "./logging";
15-
import { CreateCommitOnBranchMutationVariables } from "./github/graphql/generated/operations";
14+
import type { CreateCommitOnBranchMutationVariables } from "./github/graphql/generated/operations";
15+
import type { Logger } from "./logging";
1616

1717
export const commitFilesFromDirectory = async (args: {
1818
octokit: GitHubClient;

src/github/graphql/queries.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { GitHub } from "@actions/github/lib/utils";
1+
import type { GitHub } from "@actions/github/lib/utils";
22

33
export type GitHubClient = InstanceType<typeof GitHub>;
44

5-
import {
5+
import type {
66
CreateCommitOnBranchMutation,
77
CreateCommitOnBranchMutationVariables,
88
CreateRefMutation,
99
CreateRefMutationVariables,
10+
DeleteRefMutation,
11+
DeleteRefMutationVariables,
1012
GetRepositoryMetadataQuery,
1113
GetRepositoryMetadataQueryVariables,
1214
} from "./generated/operations";
@@ -16,6 +18,7 @@ const GET_REPOSITORY_METADATA = /* GraphQL */ `
1618
repository(owner: $owner, name: $name) {
1719
id
1820
ref(qualifiedName: $ref) {
21+
id
1922
target {
2023
oid
2124
}
@@ -34,6 +37,14 @@ const CREATE_REF = /* GraphQL */ `
3437
}
3538
`;
3639

40+
const DELETE_REF = /* GraphQL */ `
41+
mutation deleteRef($input: DeleteRefInput!) {
42+
deleteRef(input: $input) {
43+
clientMutationId
44+
}
45+
}
46+
`;
47+
3748
const CREATE_COMMIT_ON_BRANCH = /* GraphQL */ `
3849
mutation createCommitOnBranch($input: CreateCommitOnBranchInput!) {
3950
createCommitOnBranch(input: $input) {
@@ -44,12 +55,6 @@ const CREATE_COMMIT_ON_BRANCH = /* GraphQL */ `
4455
}
4556
`;
4657

47-
export const createCommitOnBranchQuery = async (
48-
o: GitHubClient,
49-
v: CreateCommitOnBranchMutationVariables,
50-
): Promise<CreateCommitOnBranchMutation> =>
51-
o.graphql<CreateCommitOnBranchMutation>(CREATE_COMMIT_ON_BRANCH, v);
52-
5358
export const getRepositoryMetadata = async (
5459
o: GitHubClient,
5560
v: GetRepositoryMetadataQueryVariables,
@@ -66,3 +71,15 @@ export const createRefMutation = async (
6671
v: CreateRefMutationVariables,
6772
): Promise<CreateRefMutation> =>
6873
await o.graphql<CreateRefMutation>(CREATE_REF, v);
74+
75+
export const deleteRefMutation = async (
76+
o: GitHubClient,
77+
v: DeleteRefMutationVariables,
78+
): Promise<DeleteRefMutation> =>
79+
await o.graphql<DeleteRefMutation>(DELETE_REF, v);
80+
81+
export const createCommitOnBranchQuery = async (
82+
o: GitHubClient,
83+
v: CreateCommitOnBranchMutationVariables,
84+
): Promise<CreateCommitOnBranchMutation> =>
85+
o.graphql<CreateCommitOnBranchMutation>(CREATE_COMMIT_ON_BRANCH, v);

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { createCommitOnBranchQuery } from "./github/graphql/queries";
1+
export * as queries from "./github/graphql/queries";
2+
export { commitFilesFromDirectory } from "./fs";

src/test/ci.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/test/integration/fs.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* This file includes tests that will be run in CI.
3+
*/
4+
import * as os from "os";
5+
import * as path from "path";
6+
import { promises as fs } from "fs";
7+
import { getOctokit } from "@actions/github/lib/github";
8+
import pino from "pino";
9+
import { configDotenv } from "dotenv";
10+
11+
import { commitFilesFromDirectory } from "../../fs";
12+
import { randomBytes } from "crypto";
13+
import {
14+
deleteRefMutation,
15+
getRepositoryMetadata,
16+
} from "../../github/graphql/queries";
17+
18+
configDotenv();
19+
20+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
21+
if (!GITHUB_TOKEN) {
22+
throw new Error("GITHUB_TOKEN must be set");
23+
}
24+
25+
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;
26+
27+
const [owner, repository] = GITHUB_REPOSITORY?.split("/") || [];
28+
if (!owner || !repository) {
29+
throw new Error("GITHUB_REPOSITORY must be set");
30+
}
31+
32+
const log = pino({
33+
level: process.env.RUNNER_DEBUG === "1" ? "debug" : "info",
34+
transport: {
35+
target: "pino-pretty",
36+
},
37+
});
38+
39+
const octokit = getOctokit(GITHUB_TOKEN);
40+
41+
const TEST_BRANCH_PREFIX = `test-${randomBytes(4).toString("hex")}`;
42+
43+
const TEST_BRANCHES = {
44+
COMMIT_FILE: `${TEST_BRANCH_PREFIX}-commit-file`,
45+
} as const;
46+
47+
describe("fs", () => {
48+
describe("commitFilesFromDirectory", () => {
49+
it("should commit a file", async () => {
50+
// Create test directory
51+
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "test-"));
52+
await fs.writeFile(path.join(tmpDir, "foo.txt"), "Hello, world!");
53+
54+
await commitFilesFromDirectory({
55+
octokit,
56+
owner,
57+
repository,
58+
branch: TEST_BRANCHES.COMMIT_FILE,
59+
baseBranch: "main",
60+
message: {
61+
headline: "Test commit",
62+
body: "This is a test commit",
63+
},
64+
workingDirectory: tmpDir,
65+
fileChanges: {
66+
additions: ["foo.txt"],
67+
},
68+
log,
69+
});
70+
});
71+
});
72+
73+
afterAll(async () => {
74+
console.info("Cleaning up test branches");
75+
76+
await Promise.all(
77+
Object.values(TEST_BRANCHES).map(async (branch) => {
78+
console.debug(`Deleting branch ${branch}`);
79+
// Get Ref
80+
const ref = await getRepositoryMetadata(octokit, {
81+
owner,
82+
name: repository,
83+
ref: `refs/heads/${branch}`,
84+
});
85+
86+
const refId = ref?.ref?.id;
87+
88+
if (!refId) {
89+
console.warn(`Branch ${branch} not found`);
90+
return;
91+
}
92+
93+
await deleteRefMutation(octokit, {
94+
input: {
95+
refId,
96+
},
97+
});
98+
99+
console.debug(`Deleted branch ${branch}`);
100+
}),
101+
);
102+
});
103+
});

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "tsup";
22

33
export default defineConfig({
4-
entry: ["src/index.ts"],
4+
entry: ["src/index.ts", "src/fs.ts"],
55
format: ["cjs", "esm"],
66
dts: true,
77
});

0 commit comments

Comments
 (0)