Skip to content

Commit 9a59833

Browse files
refactoring using dependency injection
1 parent a4a8d3f commit 9a59833

4 files changed

Lines changed: 437 additions & 273 deletions

File tree

src/create-deps.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as core from "@actions/core";
2+
import * as fs from "fs/promises";
3+
import { Octokit as ActionKit } from "@octokit/action";
4+
import { createPullRequest } from "octokit-plugin-create-pull-request";
5+
import { exec } from "child_process";
6+
import { promisify } from "util";
7+
import { Dependencies } from "./types/Dependencies.js";
8+
9+
const execAsync = promisify(exec);
10+
11+
// builds the production level Dependencies object from the GitHub Actions environment
12+
export function createProductionDeps(): Dependencies {
13+
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
14+
const adminToken = core.getInput("ADMIN_TOKEN", { required: false });
15+
16+
const MyOctoKit = ActionKit.plugin(createPullRequest);
17+
18+
const octokit = new MyOctoKit({
19+
auth: githubToken,
20+
log: {
21+
debug: core.debug,
22+
info: core.info,
23+
warn: core.warning,
24+
error: core.error,
25+
},
26+
});
27+
28+
const adminOctokit = adminToken
29+
? new MyOctoKit({
30+
auth: adminToken,
31+
log: {
32+
debug: core.debug,
33+
info: core.info,
34+
warn: core.warning,
35+
error: core.error,
36+
},
37+
})
38+
: null;
39+
40+
return {
41+
owner: process.env.GITHUB_REPOSITORY_OWNER ?? "",
42+
repo: process.env.GITHUB_REPOSITORY?.split("/")[1] ?? "",
43+
44+
githubToken,
45+
adminToken,
46+
branch: core.getInput("BRANCH", { required: false }),
47+
skipPR: core.getInput("SKIP_PR", { required: false }) === "true",
48+
isArchived: core.getInput("ARCHIVE", { required: false }) === "true",
49+
50+
octokit: octokit as unknown as Dependencies["octokit"],
51+
adminOctokit: adminOctokit as unknown as Dependencies["adminOctokit"],
52+
53+
exec: execAsync,
54+
readFile: (filepath: string) => fs.readFile(filepath, "utf8"),
55+
56+
log: {
57+
info: core.info,
58+
error: core.error,
59+
warning: core.warning,
60+
debug: core.debug,
61+
},
62+
63+
setOutput: core.setOutput,
64+
setFailed: core.setFailed,
65+
};
66+
}

0 commit comments

Comments
 (0)