-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: consolidate Octopus version checks and support local builds #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| import { isLocalOctopusVersion, isServerVersionAtLeast, ensureServerVersionAtLeast } from "./versionCheck"; | ||
| import type { Client } from "./client"; | ||
|
|
||
| describe("isLocalOctopusVersion", () => { | ||
| test.each([ | ||
| ["0.0.0", true], | ||
| ["0.0.0-local", true], | ||
| ["0.0.0-local-build.5", true], | ||
| ["0.0.0+sha1234", true], | ||
| ["0.0.0-alpha+sha1234", true], | ||
| ["0.0.1", false], | ||
| ["0.0.10", false], | ||
| ["1.0.0", false], | ||
| ["2022.3.5512", false], | ||
| ["", false], | ||
| ])("isLocalOctopusVersion(%s) === %s", (version, expected) => { | ||
| expect(isLocalOctopusVersion(version as string)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isServerVersionAtLeast", () => { | ||
| const min = "2022.3.5512"; | ||
|
|
||
| test("returns true when server version is greater than minimum", () => { | ||
| expect(isServerVersionAtLeast("2023.1.0", min)).toBe(true); | ||
| }); | ||
|
|
||
| test("returns true when server version equals minimum", () => { | ||
| expect(isServerVersionAtLeast("2022.3.5512", min)).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false when server version is below minimum", () => { | ||
| expect(isServerVersionAtLeast("2022.3.5511", min)).toBe(false); | ||
| expect(isServerVersionAtLeast("2021.1.0", min)).toBe(false); | ||
| }); | ||
|
|
||
| test("returns true for local development versions regardless of minimum", () => { | ||
| expect(isServerVersionAtLeast("0.0.0", min)).toBe(true); | ||
| expect(isServerVersionAtLeast("0.0.0-local", min)).toBe(true); | ||
| expect(isServerVersionAtLeast("0.0.0+sha1234", min)).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false for invalid version strings", () => { | ||
| expect(isServerVersionAtLeast("not-a-version", min)).toBe(false); | ||
| expect(isServerVersionAtLeast("", min)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("ensureServerVersionAtLeast", () => { | ||
| const min = "2022.3.5512"; | ||
| const feature = "creating releases using the Executions API"; | ||
| const expectedMessage = | ||
| `The Octopus instance doesn't support creating releases using the Executions API, ` + | ||
| `it will need to be upgraded to at least 2022.3.5512 in order to access this API.`; | ||
|
|
||
| function stubClient(version: string): { client: Client; errorCalls: string[] } { | ||
| const errorCalls: string[] = []; | ||
| const client = { | ||
| getServerInformation: async () => ({ version, installationId: "test" }), | ||
| error: (msg: string) => errorCalls.push(msg), | ||
| } as unknown as Client; | ||
| return { client, errorCalls }; | ||
| } | ||
|
|
||
| test("resolves silently when server version satisfies minimum", async () => { | ||
| const { client, errorCalls } = stubClient("2022.3.5512"); | ||
| await expect(ensureServerVersionAtLeast(client, min, feature)).resolves.toBeUndefined(); | ||
| expect(errorCalls).toEqual([]); | ||
| }); | ||
|
|
||
| test("resolves silently for local development versions", async () => { | ||
| const { client, errorCalls } = stubClient("0.0.0-local"); | ||
| await expect(ensureServerVersionAtLeast(client, min, feature)).resolves.toBeUndefined(); | ||
| expect(errorCalls).toEqual([]); | ||
| }); | ||
|
|
||
| test("throws and logs identical message when server version is too old", async () => { | ||
| const { client, errorCalls } = stubClient("2022.3.5511"); | ||
| await expect(ensureServerVersionAtLeast(client, min, feature)).rejects.toThrow(expectedMessage); | ||
| expect(errorCalls).toEqual([expectedMessage]); | ||
| }); | ||
|
|
||
| test("does not throw if client.error is undefined", async () => { | ||
| const client = { | ||
| getServerInformation: async () => ({ version: "2022.3.5511", installationId: "test" }), | ||
| error: undefined, | ||
| } as unknown as Client; | ||
| await expect(ensureServerVersionAtLeast(client, min, feature)).rejects.toThrow(expectedMessage); | ||
| }); | ||
| }); |
This file contains hidden or 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,29 @@ | ||
| import { lt, valid } from "semver"; | ||
| import type { Client } from "./client"; | ||
|
|
||
| // Local development builds of Octopus report a version of "0.0.0" (optionally with a | ||
| // prerelease tag like "-local" or build metadata). Treat those as "latest" so version | ||
| // gates do not block developers running against a locally-built server. | ||
| export function isLocalOctopusVersion(version: string): boolean { | ||
| return /^0\.0\.0(?:[-+].*)?$/.test(version); | ||
| } | ||
|
|
||
| // Returns true when the running server's version satisfies the minimum, OR when the | ||
| // running server is a local development build. | ||
| export function isServerVersionAtLeast(serverVersion: string, minimumVersion: string): boolean { | ||
| if (isLocalOctopusVersion(serverVersion)) return true; | ||
| if (!valid(serverVersion)) return false; | ||
| return !lt(serverVersion, minimumVersion); | ||
| } | ||
|
|
||
| // Looks up the running server's version and throws a uniform error if it is too old. | ||
| // `featureDescription` is the snippet that fills the "<FEATURE>" slot in the error | ||
| // template (e.g. "creating releases using the Executions API"). | ||
| export async function ensureServerVersionAtLeast(client: Client, minimumVersion: string, featureDescription: string): Promise<void> { | ||
| const serverInformation = await client.getServerInformation(); | ||
| if (isServerVersionAtLeast(serverInformation.version, minimumVersion)) return; | ||
|
|
||
| const message = `The Octopus instance doesn't support ${featureDescription}, it will need to be upgraded to at least ${minimumVersion} in order to access this API.`; | ||
| client.error?.(message); | ||
| throw new Error(message); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new check, but I think it's fine. It should always be semver afaik