-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Feat/installation info endpoint #7744
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
tommoor
merged 7 commits into
outline:main
from
AlexandrZagorskiy:feat/installation-info-endpoint
Oct 25, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3087550
feat: add installation.info endpoint using DockerHub API
AlexandrZagorskiy 825384a
feat: UI use an server-side API to show version info
AlexandrZagorskiy bce3a4d
fix: review fixes
AlexandrZagorskiy ec18c1a
test: installation.info endpoint
AlexandrZagorskiy c921701
feat: filtering pre-releases in installation.info endpoint
AlexandrZagorskiy de0a99e
fix: change fetch to ApiClient usage for getting version info
AlexandrZagorskiy 3815569
Undo translation change
tommoor 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./installation"; |
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,31 @@ | ||
import { buildUser } from "@server/test/factories"; | ||
import { getTestServer } from "@server/test/support"; | ||
|
||
const server = getTestServer(); | ||
|
||
describe("installation.info", () => { | ||
it("should require authentication", async () => { | ||
const res = await server.post("/api/installation.info", { | ||
body: {}, | ||
}); | ||
expect(res.status).toEqual(401); | ||
}); | ||
|
||
it("should return installation information", async () => { | ||
const user = await buildUser(); | ||
const res = await server.post("/api/installation.info", { | ||
body: { | ||
token: user.getJwtToken(), | ||
}, | ||
}); | ||
|
||
const body = await res.json(); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(body.data).not.toBeFalsy(); | ||
expect(body.data.version).not.toBeFalsy(); | ||
expect(body.data.latestVersion).not.toBeFalsy(); | ||
expect(typeof body.data.versionsBehind).toBe("number"); | ||
expect(body.policies).not.toBeFalsy(); | ||
}); | ||
}); |
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,24 @@ | ||
import Router from "koa-router"; | ||
import auth from "@server/middlewares/authentication"; | ||
import { APIContext } from "@server/types"; | ||
import { getVersion, getVersionInfo } from "@server/utils/getInstallationInfo"; | ||
|
||
const router = new Router(); | ||
|
||
router.post("installation.info", auth(), async (ctx: APIContext) => { | ||
const currentVersion = getVersion(); | ||
const { latestVersion, versionsBehind } = await getVersionInfo( | ||
currentVersion | ||
); | ||
|
||
ctx.body = { | ||
data: { | ||
version: currentVersion, | ||
latestVersion, | ||
versionsBehind, | ||
}, | ||
policies: [], | ||
}; | ||
}); | ||
|
||
export default router; |
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,62 @@ | ||
import { version } from "../../package.json"; | ||
import fetch from "./fetch"; | ||
|
||
const dockerhubLink = | ||
"https://hub.docker.com/v2/repositories/outlinewiki/outline"; | ||
|
||
function isFullReleaseVersion(versionName: string): boolean { | ||
const releaseRegex = /^(version-)?\d+\.\d+\.\d+$/; // Matches "N.N.N" or "version-N.N.N" for dockerhub releases before v0.56.0" | ||
return releaseRegex.test(versionName); | ||
} | ||
|
||
export async function getVersionInfo(currentVersion: string): Promise<{ | ||
latestVersion: string; | ||
versionsBehind: number; | ||
}> { | ||
let allVersions: string[] = []; | ||
let latestVersion: string | null = null; | ||
let nextUrl: string | null = | ||
dockerhubLink + "/tags?name=&ordering=last_updated&page_size=100"; | ||
|
||
// Continue fetching pages until the required versions are found or no more pages | ||
while (nextUrl) { | ||
const response = await fetch(nextUrl); | ||
const data = await response.json(); | ||
|
||
// Map and filter the versions to keep only full releases | ||
const pageVersions = data.results | ||
.map((result: any) => result.name) | ||
.filter(isFullReleaseVersion); | ||
|
||
allVersions = allVersions.concat(pageVersions); | ||
|
||
// Set the latest version if not already set | ||
if (!latestVersion && pageVersions.length > 0) { | ||
latestVersion = pageVersions[0]; | ||
} | ||
|
||
// Check if the current version is found | ||
const currentIndex = allVersions.findIndex( | ||
(version: string) => version === currentVersion | ||
); | ||
|
||
if (currentIndex !== -1) { | ||
const versionsBehind = currentIndex; // The number of versions behind | ||
return { | ||
latestVersion: latestVersion || currentVersion, // Fallback to current if no latest found | ||
versionsBehind, | ||
}; | ||
} | ||
|
||
nextUrl = data.next || null; | ||
} | ||
|
||
return { | ||
latestVersion: latestVersion || currentVersion, | ||
versionsBehind: -1, // Return -1 if current version is not found | ||
}; | ||
} | 4B72 tr>||
|
||
export function getVersion(): string { | ||
return version; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.