vouchington-tooling 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ export type DependencyUpdate = Record<'dependencyName' | 'directory' | 'newVersion' | 'packageEcosystem' | 'prevVersion' | 'updateType', string>;
2
+ export interface ScriptResult {
3
+ failures: string[];
4
+ infos: string[];
5
+ mutationRequests: Array<{
6
+ body: string;
7
+ headers: Record<string, string>;
8
+ method: string;
9
+ url: string;
10
+ }>;
11
+ restGetCalls: Array<Record<string, unknown>>;
12
+ warnings: string[];
13
+ }
14
+ export declare function update(prevVersion: string, newVersion: string, updateType: string, dependencyName?: string): DependencyUpdate;
15
+ export declare function runPolicy(metadata: DependencyUpdate[] | string | undefined, pullRequestOverrides?: Record<string, unknown>, freshPullRequestOverrides?: Record<string, unknown>, mergeToken?: string | undefined, mutationResult?: {
16
+ jsonError?: Error;
17
+ payload?: unknown;
18
+ status?: number;
19
+ }, dependabot?: {
20
+ directory: string;
21
+ ecosystem: string;
22
+ }, expectedBase?: string, expectedHead?: string, manualRules?: string, confirmedPullRequestOverrides?: Error | Record<string, unknown>, initialRefreshError?: Error): Promise<ScriptResult>;
@@ -0,0 +1,113 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { parse as load } from 'yaml';
3
+ const source = readFileSync('.github/actions/dependabot-automerge/action.yml', 'utf8');
4
+ const action = load(source);
5
+ const script = action.runs.steps.find((step) => step.uses?.startsWith('actions/github-script@'))
6
+ ?.with?.script;
7
+ export function update(prevVersion, newVersion, updateType, dependencyName = 'example') {
8
+ return {
9
+ dependencyName,
10
+ directory: '/',
11
+ newVersion,
12
+ packageEcosystem: 'npm',
13
+ prevVersion,
14
+ updateType,
15
+ };
16
+ }
17
+ export async function runPolicy(metadata, pullRequestOverrides = {}, freshPullRequestOverrides = {}, mergeToken = 'merge-token', mutationResult = {}, dependabot = { directory: '/', ecosystem: 'npm' }, expectedBase, expectedHead, manualRules = '[]', confirmedPullRequestOverrides = freshPullRequestOverrides, initialRefreshError) {
18
+ if (!script)
19
+ throw new Error('Dependabot auto-merge script is missing');
20
+ const failures = [];
21
+ const infos = [];
22
+ const mutationRequests = [];
23
+ const restGetCalls = [];
24
+ const warnings = [];
25
+ const eventPullRequest = {
26
+ auto_merge: null,
27
+ base: { ref: 'main', sha: 'a'.repeat(40) },
28
+ draft: false,
29
+ head: {
30
+ ref: 'dependabot/npm_and_yarn/example-1.5.0',
31
+ repo: { full_name: 'example-owner/example-repo' },
32
+ sha: 'b'.repeat(40),
33
+ },
34
+ node_id: 'PR_node_id',
35
+ number: 123,
36
+ merged: false,
37
+ state: 'open',
38
+ title: 'Bump dependencies',
39
+ user: { login: 'dependabot[bot]' },
40
+ ...pullRequestOverrides,
41
+ };
42
+ const github = {
43
+ rest: {
44
+ pulls: {
45
+ async get(args) {
46
+ restGetCalls.push(args);
47
+ if (restGetCalls.length === 1 && initialRefreshError)
48
+ throw initialRefreshError;
49
+ if (restGetCalls.length > 1 && confirmedPullRequestOverrides instanceof Error)
50
+ throw confirmedPullRequestOverrides;
51
+ const overrides = restGetCalls.length === 1 || confirmedPullRequestOverrides instanceof Error
52
+ ? freshPullRequestOverrides
53
+ : confirmedPullRequestOverrides;
54
+ return { data: { ...eventPullRequest, ...overrides } };
55
+ },
56
+ },
57
+ },
58
+ };
59
+ const context = {
60
+ payload: { pull_request: eventPullRequest, repository: { default_branch: 'main' } },
61
+ repo: { owner: 'example-owner', repo: 'example-repo' },
62
+ };
63
+ const core = {
64
+ setFailed(message) {
65
+ failures.push(message);
66
+ },
67
+ info(message) {
68
+ infos.push(message);
69
+ },
70
+ warning(message) {
71
+ warnings.push(message);
72
+ },
73
+ };
74
+ const environment = {
75
+ DEPENDABOT_DIRECTORY: dependabot.directory,
76
+ DEPENDABOT_ECOSYSTEM: dependabot.ecosystem,
77
+ EXPECTED_BASE_SHA: expectedBase ?? String(eventPullRequest.base.sha),
78
+ EXPECTED_HEAD_SHA: expectedHead ?? String(eventPullRequest.head.sha),
79
+ GRAPHQL_URL: 'https://github.example.test/api/graphql',
80
+ MANUAL_UPDATE_RULES: manualRules,
81
+ };
82
+ if (metadata !== undefined)
83
+ environment.UPDATED_DEPENDENCIES_JSON =
84
+ typeof metadata === 'string' ? metadata : JSON.stringify(metadata);
85
+ if (mergeToken !== undefined)
86
+ environment.AUTOMERGE_TOKEN = mergeToken;
87
+ const fetch = async (url, init) => {
88
+ if (typeof init.body !== 'string')
89
+ throw new TypeError('Dependabot auto-merge mutation body must be a string');
90
+ const body = init.body;
91
+ mutationRequests.push({
92
+ body,
93
+ headers: init.headers,
94
+ method: String(init.method),
95
+ url,
96
+ });
97
+ return {
98
+ async json() {
99
+ if (mutationResult.jsonError)
100
+ throw mutationResult.jsonError;
101
+ return (mutationResult.payload ??
102
+ (body.includes('disablePullRequestAutoMerge')
103
+ ? { data: { disablePullRequestAutoMerge: { clientMutationId: null } } }
104
+ : { data: { enablePullRequestAutoMerge: { clientMutationId: null } } }));
105
+ },
106
+ ok: (mutationResult.status ?? 200) < 400,
107
+ status: mutationResult.status ?? 200,
108
+ };
109
+ };
110
+ const AsyncFunction = Object.getPrototypeOf(async () => undefined).constructor;
111
+ await new AsyncFunction('github', 'context', 'core', 'process', 'fetch', script)(github, context, core, { env: environment }, fetch);
112
+ return { failures, infos, mutationRequests, restGetCalls, warnings };
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vouchington-tooling",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Vouchington CLI and extractable tooling libraries.",
5
5
  "homepage": "https://github.com/vouchington/vouchington-tooling/tree/main/packages/vouchington-tooling#readme",
6
6
  "bugs": {