-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.ts
More file actions
180 lines (157 loc) · 3.71 KB
/
Copy pathutils.ts
File metadata and controls
180 lines (157 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import crypto from "node:crypto";
import { getOctokit } from "@actions/github";
import { expect } from "vitest";
import {
deleteRefMutation,
getRefTreeQuery,
getRepositoryMetadata,
} from "../../src/github/graphql/queries.ts";
export const githubToken = process.env.GITHUB_TOKEN!;
export const [owner, repo] = process.env.GITHUB_REPOSITORY!.split("/")!;
export const octokit = getOctokit(githubToken);
/**
* GitHub sometimes has a delay between making changes to a git repo,
* and those changes being reflected in the API.
*
* This function is a workaround to wait for GitHub to be ready
* before running these assertions.
*
* It slows down testing a bit,
* but it's better than having flaky tests.
*/
export async function waitForGitHubToBeReady() {
return await new Promise((r) => setTimeout(r, 5000));
}
const runHash = crypto.randomBytes(4).toString("hex");
const runId = process.env.GITHUB_RUN_ID ?? "local";
export function getTempBranch(name: string) {
return `changesets-ghcommit-test-${runHash}-id-${runId}/${name}`;
}
/**
* Calculate the SHA using git blob hash format
*/
export function getSha(contents: Buffer): string {
const header = Buffer.from(`blob ${contents.length}\0`);
return crypto
.createHash("sha1")
.update(header)
.update(contents)
.digest("hex");
}
// #region Assertion helpers
export async function expectBranchHasTree({
branch,
treeSha,
}: {
branch: string;
treeSha: string;
}) {
const ref = (
await getRefTreeQuery(octokit, {
owner,
repo,
ref: `refs/heads/${branch}`,
path: "package.json",
})
).repository?.ref?.target;
if (!ref) {
throw new Error("Unexpected missing ref");
}
expect(ref.tree.oid).toEqual(treeSha);
}
export async function expectBranchHasFile({
branch,
filePath,
fileSha,
}: {
branch: string;
filePath: string;
fileSha: string;
}) {
const ref = (
await getRefTreeQuery(octokit, {
owner,
repo,
ref: `refs/heads/${branch}`,
path: filePath,
})
).repository?.ref?.target;
if (!ref) {
throw new Error("Unexpected missing ref");
}
expect(ref.file?.oid).toEqual(fileSha);
}
export async function expectBranchNotHaveFile({
branch,
filePath,
}: {
branch: string;
filePath: string;
}) {
await expect(() =>
getRefTreeQuery(octokit, {
owner,
repo,
ref: `refs/heads/${branch}`,
path: filePath,
}),
).rejects.toThrow("Could not resolve file for path");
}
export async function expectParentHasSha({
branch,
sha,
}: {
branch: string;
sha: string;
}) {
const ref = (
await getRefTreeQuery(octokit, {
owner,
repo,
ref: `refs/heads/${branch}`,
path: "package.json",
})
).repository?.ref?.target;
if (!ref || !("parents" in ref)) {
throw new Error("Unexpected result");
}
expect(ref.parents.nodes?.[0]?.oid).toEqual(sha);
}
export async function expectBranchDoesNotExist(branch: string) {
await expect(
octokit.rest.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
}),
).rejects.toMatchObject({
status: 404,
});
}
// #endregion
// #region Octokit helpers
export async function deleteBranch(branch: string, allowNotExist = false) {
try {
const ref = await getRepositoryMetadata(octokit, {
owner,
repo,
baseRef: `refs/heads/${branch}`,
targetRef: `refs/heads/${branch}`,
});
const refId = ref?.baseRef?.id;
if (!refId) {
if (!allowNotExist) {
console.warn(`Branch ${branch} not found`);
}
return;
}
await deleteRefMutation(octokit, {
input: {
refId,
},
});
} catch (error) {
console.error(`Failed to delete branch ${branch}:`, error);
}
}
// #endregion