Skip to content

Commit 358129b

Browse files
committed
Load deploy hash based on current commit hash
1 parent 0357b47 commit 358129b

File tree

5 files changed

+117
-117
lines changed

5 files changed

+117
-117
lines changed

actions/cursor-deploy/dist/main/index.js

+29-18
Original file line numberDiff line numberDiff line change
@@ -24280,6 +24280,12 @@ async function copyFileToS3({
2428024280
}) {
2428124281
await (0, import_exec.exec)("aws s3 cp", [path, `s3://${bucket}/${key}`]);
2428224282
}
24283+
async function readFileFromS3({ key, bucket }) {
24284+
await (0, import_exec.exec)("aws s3 cp", [`s3://${bucket}/${key}`, "tmp"]);
24285+
const data = await execReadOutput("cat", ["tmp"]);
24286+
await (0, import_exec.exec)("rm", ["tmp"]);
24287+
return data;
24288+
}
2428324289
async function removeFileFromS3({ key, bucket }) {
2428424290
await (0, import_exec.exec)("aws s3 rm", [`s3://${bucket}/${key}`]);
2428524291
}
@@ -24298,11 +24304,8 @@ async function runAction(action) {
2429824304
async function isHeadAncestor(commitHash) {
2429924305
return execIsSuccessful("git merge-base", [`--is-ancestor`, commitHash, `HEAD`]);
2430024306
}
24301-
async function getTreeHashForCommitHash(commit) {
24302-
return execReadOutput("git rev-parse", [`${commit}:`]);
24303-
}
24304-
async function getCurrentRepoTreeHash() {
24305-
return getTreeHashForCommitHash("HEAD");
24307+
async function getCommitHashFromRef(ref) {
24308+
return execReadOutput(`git rev-parse`, [ref]);
2430624309
}
2430724310

2430824311
// cursor-deploy/index.ts
@@ -24318,7 +24321,7 @@ runAction(async () => {
2431824321
rollbackCommitHash,
2431924322
ref: ((_b = (_a2 = github.context.payload) == null ? void 0 : _a2.pull_request) == null ? void 0 : _b.head.ref) ?? github.context.ref
2432024323
});
24321-
core2.setOutput("tree_hash", output.treeHash);
24324+
core2.setOutput("deploy_hash", output.deployHash);
2432224325
core2.setOutput("branch_label", output.branchLabel);
2432324326
});
2432424327
async function cursorDeploy({
@@ -24329,7 +24332,8 @@ async function cursorDeploy({
2432924332
}) {
2433024333
const deployMode = getDeployMode(deployModeInput);
2433124334
const branchLabel = branchNameToHostnameLabel(ref);
24332-
const treeHash = await getDeploymentHash(deployMode, rollbackCommitHash);
24335+
const commitHash = await getDeployCommitHash(deployMode, rollbackCommitHash);
24336+
const deployHash = await getDeploymentHashFromCommitHash(bucket, commitHash);
2433324337
const rollbackKey = `rollbacks/${branchLabel}`;
2433424338
const deployKey = `deploys/${branchLabel}`;
2433524339
if (deployMode === "default" || deployMode === "unblock") {
@@ -24341,9 +24345,9 @@ async function cursorDeploy({
2434124345
throw new Error(`${branchLabel} does not have an active rollback, you can't unblock.`);
2434224346
}
2434324347
}
24344-
await writeLineToFile({ text: treeHash, path: branchLabel });
24348+
await writeLineToFile({ text: deployHash, path: branchLabel });
2434524349
await copyFileToS3({ path: branchLabel, bucket, key: deployKey });
24346-
core2.info(`Tree hash ${treeHash} is now the active deployment for ${branchLabel}.`);
24350+
core2.info(`Deploy hash ${deployHash} is now the active deployment for ${branchLabel}.`);
2434724351
if (deployMode === "rollback") {
2434824352
await copyFileToS3({ path: branchLabel, bucket, key: rollbackKey });
2434924353
core2.info(`${branchLabel} marked as rolled back, automatic deploys paused.`);
@@ -24352,7 +24356,7 @@ async function cursorDeploy({
2435224356
await removeFileFromS3({ bucket, key: rollbackKey });
2435324357
core2.info(`${branchLabel} has automatic deploys resumed.`);
2435424358
}
24355-
return { treeHash, branchLabel };
24359+
return { deployHash, branchLabel };
2435624360
}
2435724361
function getDeployMode(deployMode) {
2435824362
function assertDeployMode(value) {
@@ -24363,19 +24367,26 @@ function getDeployMode(deployMode) {
2436324367
assertDeployMode(deployMode);
2436424368
return deployMode;
2436524369
}
24366-
async function getDeploymentHash(deployMode, rollbackCommitHash) {
24370+
async function getDeployCommitHash(deployMode, rollbackCommitHash) {
2436724371
if (deployMode === "rollback") {
2436824372
if (!!rollbackCommitHash && !await isHeadAncestor(rollbackCommitHash)) {
2436924373
throw new Error("The selected rollback commit is not present on the branch");
2437024374
}
24371-
const commit = rollbackCommitHash || "HEAD^";
24372-
const treeHash2 = await getTreeHashForCommitHash(commit);
24373-
core2.info(`Rolling back to tree hash ${treeHash2} (commit ${commit})`);
24374-
return treeHash2;
24375+
return getCommitHashFromRef(rollbackCommitHash || "HEAD^");
24376+
}
24377+
return getCommitHashFromRef("HEAD");
24378+
}
24379+
async function getDeploymentHashFromCommitHash(bucket, commitHash) {
24380+
const commitKey = `deploys/commits/${commitHash}`;
24381+
const deployHash = await readFileFromS3({
24382+
bucket,
24383+
key: commitKey
24384+
});
24385+
if (!deployHash) {
24386+
throw Error(`Deploy hash for commit ${commitHash} not found.`);
2437524387
}
24376-
const treeHash = await getCurrentRepoTreeHash();
24377-
core2.info(`Using current root tree hash ${treeHash}`);
24378-
return treeHash;
24388+
core2.info(`Using deploy hash ${deployHash} (commit ${commitHash})`);
24389+
return deployHash;
2437924390
}
2438024391
function branchNameToHostnameLabel(ref) {
2438124392
var _a2, _b;

actions/cursor-deploy/index.test.ts

+28-34
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe(`Cursor Deploy Action`, () => {
1919
And the tree hash used is the current repo tree hash
2020
`,
2121
async () => {
22-
const treeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
23-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
22+
const deployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
23+
mockedUtils.readFileFromS3.mockResolvedValue(deployHash)
2424
mockedUtils.fileExistsInS3.mockResolvedValue(false)
2525

2626
const output = await cursorDeploy({
@@ -33,13 +33,13 @@ describe(`Cursor Deploy Action`, () => {
3333
expectRollbackFileChecked('my-bucket', 'rollbacks/main')
3434

3535
expectCursorFileUpdated({
36-
treeHash: treeHash,
36+
deployHash: deployHash,
3737
branch: 'main',
3838
bucket: 'my-bucket',
3939
key: 'deploys/main'
4040
})
4141

42-
expect(output.treeHash).toBe(treeHash)
42+
expect(output.deployHash).toBe(deployHash)
4343
expect(output.branchLabel).toBe('main')
4444
}
4545
)
@@ -53,9 +53,9 @@ describe(`Cursor Deploy Action`, () => {
5353
And the tree hash used is the current repo tree hash
5454
`,
5555
async () => {
56-
const treeHash = '553b0cb96ac21ffc0583e5d8d72343b1faa90dfd'
56+
const deployHash = '553b0cb96ac21ffc0583e5d8d72343b1faa90dfd'
5757
const sanitizedBranch = 'lol-my-feature-branch-30-better'
58-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
58+
mockedUtils.readFileFromS3.mockResolvedValue(deployHash)
5959
mockedUtils.fileExistsInS3.mockResolvedValue(false)
6060

6161
const output = await cursorDeploy({
@@ -68,13 +68,13 @@ describe(`Cursor Deploy Action`, () => {
6868
expectRollbackFileChecked('my-bucket', 'rollbacks/lol-my-feature-branch-30-better')
6969

7070
expectCursorFileUpdated({
71-
treeHash: treeHash,
71+
deployHash: deployHash,
7272
branch: sanitizedBranch,
7373
bucket: 'my-bucket',
7474
key: 'deploys/lol-my-feature-branch-30-better'
7575
})
7676

77-
expect(output.treeHash).toBe(treeHash)
77+
expect(output.deployHash).toBe(deployHash)
7878
expect(output.branchLabel).toBe(sanitizedBranch)
7979
}
8080
)
@@ -87,8 +87,8 @@ describe(`Cursor Deploy Action`, () => {
8787
And the action returns a error
8888
`,
8989
async () => {
90-
const treeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
91-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
90+
const deployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
91+
mockedUtils.readFileFromS3.mockResolvedValue(deployHash)
9292
mockedUtils.fileExistsInS3.mockResolvedValue(true)
9393

9494
const promise = cursorDeploy({
@@ -118,12 +118,10 @@ describe(`Cursor Deploy Action`, () => {
118118
And the tree hash used is the previous commit tree hash
119119
`,
120120
async () => {
121-
const currentTreeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
122-
const commitTreeHash = '32439d157a7e346d117a6a3c47d511526bd45012'
121+
const currentDeployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
123122

124-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(currentTreeHash)
123+
mockedUtils.readFileFromS3.mockResolvedValue(currentDeployHash)
125124
mockedUtils.fileExistsInS3.mockResolvedValue(false)
126-
mockedUtils.getTreeHashForCommitHash.mockResolvedValue(commitTreeHash)
127125

128126
const output = await cursorDeploy({
129127
bucket: 'my-prod-bucket',
@@ -134,11 +132,11 @@ describe(`Cursor Deploy Action`, () => {
134132

135133
expect(mockedUtils.fileExistsInS3).not.toHaveBeenCalled()
136134
expect(mockedUtils.isHeadAncestor).not.toHaveBeenCalled()
137-
expect(mockedUtils.getTreeHashForCommitHash).toHaveBeenCalledWith('HEAD^')
135+
expect(mockedUtils.getCommitHashFromRef).toHaveBeenCalledWith('HEAD^')
138136

139137
expect(mockedUtils.writeLineToFile).toHaveBeenCalledTimes(1)
140138
expect(mockedUtils.writeLineToFile).toHaveBeenCalledWith({
141-
text: commitTreeHash,
139+
text: currentDeployHash,
142140
path: 'main'
143141
})
144142

@@ -155,7 +153,7 @@ describe(`Cursor Deploy Action`, () => {
155153
key: 'rollbacks/main'
156154
})
157155

158-
expect(output.treeHash).toBe(commitTreeHash)
156+
expect(output.deployHash).toBe(currentDeployHash)
159157
expect(output.branchLabel).toBe('main')
160158
}
161159
)
@@ -168,14 +166,12 @@ describe(`Cursor Deploy Action`, () => {
168166
And the tree hash used is the tree hash of the passed commit hash
169167
`,
170168
async () => {
171-
const currentTreeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
172-
const commitTreeHash = 'b6e1c0468f4705b8cd0f18a04cd28ef7b9da7425'
169+
const currentDeployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
173170
const commitHash = 'fc24d309398cbf6d53237e05e4d2a8cd2de57cc7'
174171

175-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(currentTreeHash)
172+
mockedUtils.readFileFromS3.mockResolvedValue(currentDeployHash)
176173
mockedUtils.fileExistsInS3.mockResolvedValue(false)
177174
mockedUtils.isHeadAncestor.mockResolvedValue(true)
178-
mockedUtils.getTreeHashForCommitHash.mockResolvedValue(commitTreeHash)
179175

180176
const output = await cursorDeploy({
181177
bucket: 'my-bucket',
@@ -186,11 +182,11 @@ describe(`Cursor Deploy Action`, () => {
186182

187183
expect(mockedUtils.fileExistsInS3).not.toHaveBeenCalled()
188184
expect(mockedUtils.isHeadAncestor).toHaveBeenCalledWith(commitHash)
189-
expect(mockedUtils.getTreeHashForCommitHash).toHaveBeenCalledWith(commitHash)
185+
expect(mockedUtils.getCommitHashFromRef).toHaveBeenCalledWith(commitHash)
190186

191187
expect(mockedUtils.writeLineToFile).toHaveBeenCalledTimes(1)
192188
expect(mockedUtils.writeLineToFile).toHaveBeenCalledWith({
193-
text: commitTreeHash,
189+
text: currentDeployHash,
194190
path: 'main'
195191
})
196192

@@ -207,7 +203,7 @@ describe(`Cursor Deploy Action`, () => {
207203
key: 'rollbacks/main'
208204
})
209205

210-
expect(output.treeHash).toBe(commitTreeHash)
206+
expect(output.deployHash).toBe(currentDeployHash)
211207
expect(output.branchLabel).toBe('main')
212208
}
213209
)
@@ -221,9 +217,9 @@ describe(`Cursor Deploy Action`, () => {
221217
And the tree hash used is the tree hash of the passed commit hash
222218
`,
223219
async () => {
224-
const treeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
220+
const deployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
225221

226-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
222+
mockedUtils.readFileFromS3.mockResolvedValue(deployHash)
227223
mockedUtils.fileExistsInS3.mockResolvedValue(true)
228224
mockedUtils.isHeadAncestor.mockResolvedValue(true)
229225

@@ -237,7 +233,7 @@ describe(`Cursor Deploy Action`, () => {
237233
expectRollbackFileChecked('my-bucket', 'rollbacks/main')
238234

239235
expectCursorFileUpdated({
240-
treeHash: treeHash,
236+
deployHash: deployHash,
241237
branch: 'main',
242238
bucket: 'my-bucket',
243239
key: 'deploys/main'
@@ -248,7 +244,7 @@ describe(`Cursor Deploy Action`, () => {
248244
key: 'rollbacks/main'
249245
})
250246

251-
expect(output.treeHash).toBe(treeHash)
247+
expect(output.deployHash).toBe(deployHash)
252248
expect(output.branchLabel).toBe('main')
253249
}
254250
)
@@ -282,14 +278,12 @@ describe(`Cursor Deploy Action`, () => {
282278
Then the action fails with an informative error
283279
`,
284280
async () => {
285-
const currentTreeHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
281+
const currentDeployHash = 'b017ebdf289ba78787da4e9c3291f0b7959e7059'
286282
const commitHash = 'fc24d309398cbf6d53237e05e4d2a8cd2de57cc7'
287-
const commitTreeHash = 'b6e1c0468f4705b8cd0f18a04cd28ef7b9da7425'
288283

289-
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(currentTreeHash)
284+
mockedUtils.readFileFromS3.mockResolvedValue(currentDeployHash)
290285
mockedUtils.fileExistsInS3.mockResolvedValue(false)
291286
mockedUtils.isHeadAncestor.mockResolvedValue(false)
292-
mockedUtils.getTreeHashForCommitHash.mockResolvedValue(commitTreeHash)
293287

294288
const promise = cursorDeploy({
295289
bucket: 'my-bucket',
@@ -370,14 +364,14 @@ describe('Branch Sanitize - branchNameToHostnameLabel', () => {
370364
//#region Custom Assertions
371365

372366
function expectCursorFileUpdated(args: {
373-
treeHash: string
367+
deployHash: string
374368
branch: string
375369
bucket: string
376370
key: string
377371
}) {
378372
expect(mockedUtils.writeLineToFile).toHaveBeenCalledTimes(1)
379373
expect(mockedUtils.writeLineToFile).toHaveBeenCalledWith({
380-
text: args.treeHash,
374+
text: args.deployHash,
381375
path: args.branch
382376
})
383377

0 commit comments

Comments
 (0)