-
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.
chore: Refactors and unifies the getPullRequestNumber functionality
- Loading branch information
1 parent
2850518
commit 93958d0
Showing
8 changed files
with
165 additions
and
98 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,117 @@ | ||
import type * as core from "@actions/core"; | ||
import { | ||
type Mock, | ||
afterEach, | ||
beforeEach, | ||
describe, | ||
expect, | ||
it, | ||
vi, | ||
} from "vitest"; | ||
import type { Octokit } from "../octokit"; | ||
import { getPullRequestNumber } from "./getPullRequestNumber"; | ||
|
||
type Core = typeof core; | ||
|
||
// Avoid logs | ||
vi.mock("@actions/core", async (importOriginal): Promise<Core> => { | ||
const original: Core = await importOriginal(); | ||
return { | ||
...original, | ||
startGroup: vi.fn(), | ||
info: vi.fn(), | ||
warning: vi.fn(), | ||
debug: vi.fn(), | ||
}; | ||
}); | ||
|
||
const mockContext = vi.hoisted(() => ({ | ||
repo: { | ||
owner: "owner", | ||
repo: "repo", | ||
}, | ||
payload: {}, | ||
eventName: "", | ||
})); | ||
vi.mock("@actions/github", () => ({ | ||
context: mockContext, | ||
})); | ||
|
||
describe("getPullRequestNumber()", () => { | ||
let mockOctokit: Octokit; | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
mockOctokit = { | ||
paginate: { | ||
iterator: vi.fn(), | ||
}, | ||
rest: { | ||
pulls: { | ||
list: vi.fn(), | ||
}, | ||
}, | ||
} as unknown as Octokit; | ||
}); | ||
|
||
afterEach(() => { | ||
mockContext.payload = {}; | ||
mockContext.eventName = ""; | ||
vi.unstubAllEnvs(); | ||
}); | ||
|
||
it("returns the PR number from the input 'pr-number' if valid ", async () => { | ||
vi.stubEnv("INPUT_PR-NUMBER", "123"); | ||
|
||
const result = await getPullRequestNumber(mockOctokit); | ||
expect(result).toBe(123); | ||
}); | ||
|
||
it("returns the PR number from payload.pull_request context if found", async () => { | ||
mockContext.payload = { | ||
pull_request: { | ||
number: 456, | ||
}, | ||
}; | ||
|
||
const result = await getPullRequestNumber(mockOctokit); | ||
expect(result).toBe(456); | ||
}); | ||
|
||
it("returns the PR number from payload.workflow_run context if found", async () => { | ||
mockContext.eventName = "workflow_run"; | ||
mockContext.payload = { | ||
workflow_run: { | ||
pull_requests: [{ number: 789 }], | ||
head_sha: "testsha", | ||
}, | ||
}; | ||
|
||
const result = await getPullRequestNumber(mockOctokit); | ||
expect(result).toBe(789); | ||
}); | ||
|
||
it("calls the API to find PR number by the head_sha of the payload.workflow_run when called from a fork", async () => { | ||
mockContext.eventName = "workflow_run"; | ||
mockContext.payload = { | ||
workflow_run: { | ||
pull_requests: [], | ||
head_sha: "testsha", | ||
}, | ||
}; | ||
|
||
(mockOctokit.paginate.iterator as Mock).mockReturnValue([ | ||
{ | ||
data: [{ number: 101, head: { sha: "testsha" } }], | ||
}, | ||
]); | ||
|
||
const result = await getPullRequestNumber(mockOctokit); | ||
expect(result).toBe(101); | ||
}); | ||
|
||
it("returns undefined if no pr number is found", async () => { | ||
mockContext.payload = {}; | ||
const result = await getPullRequestNumber(mockOctokit); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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