-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/npm_and_yarn/semantic-release-1ec…
…fe53883
- Loading branch information
Showing
21 changed files
with
619 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; | ||
import { getCommitSHA } from "./getCommitSHA"; | ||
|
||
const mockContext = vi.hoisted(() => ({ | ||
repo: { | ||
owner: "owner", | ||
repo: "repo", | ||
}, | ||
payload: {}, | ||
eventName: "", | ||
sha: "defaultsha", | ||
})); | ||
vi.mock("@actions/github", () => ({ | ||
context: mockContext, | ||
})); | ||
|
||
describe("getCommitSHA()", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
mockContext.payload = {}; | ||
mockContext.eventName = ""; | ||
mockContext.sha = "defaultsha"; | ||
}); | ||
|
||
afterEach(() => { | ||
mockContext.payload = {}; | ||
mockContext.eventName = ""; | ||
mockContext.sha = "defaultsha"; | ||
vi.unstubAllEnvs(); | ||
}); | ||
|
||
it("if in pull-request context, returns the head sha", () => { | ||
mockContext.eventName = "pull_request"; | ||
mockContext.payload = { | ||
pull_request: { | ||
head: { | ||
sha: "prsha", | ||
}, | ||
}, | ||
}; | ||
|
||
const result = getCommitSHA(); | ||
expect(result).toBe("prsha"); | ||
}); | ||
|
||
it("if in pull_request_target context, returns the head sha", () => { | ||
mockContext.eventName = "pull_request_target"; | ||
mockContext.payload = { | ||
pull_request: { | ||
head: { | ||
sha: "prsha", | ||
}, | ||
}, | ||
}; | ||
|
||
const result = getCommitSHA(); | ||
expect(result).toBe("prsha"); | ||
}); | ||
|
||
it("if in workflow_run context, returns the SHA from workflow_run context if found", () => { | ||
mockContext.eventName = "workflow_run"; | ||
mockContext.payload = { | ||
workflow_run: { | ||
head_commit: { | ||
id: "workflowsha", | ||
}, | ||
}, | ||
}; | ||
|
||
const result = getCommitSHA(); | ||
expect(result).toBe("workflowsha"); | ||
}); | ||
|
||
it("returns the default SHA for other events", () => { | ||
mockContext.eventName = "push"; | ||
mockContext.sha = "pushsha"; | ||
|
||
const result = getCommitSHA(); | ||
expect(result).toBe("pushsha"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as github from "@actions/github"; | ||
|
||
type Context = typeof github.context; | ||
type Payload = Context["payload"]; | ||
type PRPayload = NonNullable<Payload["pull_request"]>; | ||
|
||
type PRContext = Context & { | ||
payload: Payload & { | ||
pull_request: PRPayload; | ||
}; | ||
}; | ||
|
||
function isPRContext(context: typeof github.context): context is PRContext { | ||
return ( | ||
context.eventName === "pull_request" || | ||
context.eventName === "pull_request_target" | ||
); | ||
} | ||
|
||
function getCommitSHA(): string { | ||
if (isPRContext(github.context)) { | ||
return github.context.payload.pull_request.head.sha; | ||
} | ||
|
||
if (github.context.eventName === "workflow_run") { | ||
return github.context.payload.workflow_run.head_commit.id; | ||
} | ||
|
||
// For all other events, just return the current SHA | ||
return github.context.sha; | ||
} | ||
|
||
export { getCommitSHA }; |
Oops, something went wrong.