Skip to content
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

feature(api): version endpoint #142

Merged
merged 12 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ USER node
COPY --chown=node:node . .
RUN npm install

ARG GIT_SHA=dev
ARG RUN_ID=unknown
# Get the current HSTS list
RUN npm run updateHsts

RUN env

ENV RUN_ID=${RUN_ID}
ENV GIT_SHA=${GIT_SHA}
ENV NODE_EXTRA_CA_CERTS=node_modules/extra_certs/ca_bundle/ca_intermediate_bundle.pem
EXPOSE 8080
CMD [ "node", "src/api/index.js" ]
2 changes: 2 additions & 0 deletions src/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import analyzeApiV2 from "./v2/analyze/index.js";
import scanApiV2 from "./v2/scan/index.js";
import statsApiV2 from "./v2/stats/index.js";
import recommendationMatrixApiV2 from "./v2/recommendations/index.js";
import version from "./version/index.js";
import globalErrorHandler from "./global-error-handler.js";
import pool from "@fastify/postgres";
import { poolOptions } from "../database/repository.js";
Expand Down Expand Up @@ -76,6 +77,7 @@ export async function createServer() {
server.register(scanApiV2, { prefix: "/api/v2" }),
server.register(statsApiV2, { prefix: "/api/v2" }),
server.register(recommendationMatrixApiV2, { prefix: "/api/v2" }),
server.register(version, { prefix: "/api/v2" }),
]);

["SIGINT", "SIGTERM"].forEach((signal) => {
Expand Down
19 changes: 18 additions & 1 deletion src/api/v2/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ const gradeDistributionResponse = {
},
};

const versionResponse = {
type: "object",
properties: {
commit: { type: "string" },
version: { type: "string" },
source: { type: "string" },
build: { type: "string" },
},
required: ["commit", "version", "source", "build"],
};

const recommendationMatrixResponse = {
type: "array",
items: {
Expand Down Expand Up @@ -200,6 +211,12 @@ export const SCHEMAS = {
200: recommendationMatrixResponse,
},
},

version: {
response: {
200: versionResponse,
},
},
};

/**
Expand Down Expand Up @@ -318,4 +335,4 @@ export class PolicyResponse {
}

// import jsdoc from "json-schema-to-jsdoc";
// console.log(jsdoc(SCHEMAS.gradeDistribution.response["200"]));
// console.log(jsdoc(SCHEMAS.version.response["200"]));
40 changes: 40 additions & 0 deletions src/api/version/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { version } from "tough-cookie";
import { SCHEMAS } from "../v2/schemas.js";
import fs from "node:fs";
import path from "path";
import { fileURLToPath } from "url";

// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const packageJson = JSON.parse(
fs.readFileSync(
path.join(__dirname, "..", "..", "..", "package.json"),
"utf8"
)
);

/**
* Register the API - default export
* @param {import('fastify').FastifyInstance} fastify
* @returns {Promise<void>}
*/
export default async function (fastify) {
const pool = fastify.pg.pool;

fastify.get(
"/version",
{ schema: SCHEMAS.version },
async (request, reply) => {
/** @type {import("../../types.js").VersionResponse} */
const ret = {
version: packageJson.version,
commit: process.env.GIT_SHA || "unknown",
source: "https://github.com/mdn/mdn-http-observatory",
build: process.env.RUN_ID || "unknown",
};
return ret;
}
);
}
8 changes: 8 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,11 @@ export class Policy {
* @property {string} grade
* @property {number} count
*/

/**
* @typedef {object} VersionResponse
* @property {string} commit
* @property {string} version
* @property {string} source
* @property {string} build
*/
20 changes: 20 additions & 0 deletions test/apiv2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../src/database/repository.js";
import { EventEmitter } from "events";
import { NUM_TESTS } from "../src/constants.js";
import fs from "node:fs";

const pool = createPool();
EventEmitter.defaultMaxListeners = 20;
Expand All @@ -25,6 +26,25 @@ describeOrSkip("API V2", function () {
await migrateDatabase("max", pool);
});

it("serves the version path", async function () {
process.env.RUN_ID = "buildinfo";
process.env.GIT_SHA = "commitinfo";
const app = await createServer();
const response = await app.inject({
method: "GET",
url: "/api/v2/version",
});
assert.equal(response.statusCode, 200);
const j = response.json();
const p = JSON.parse(fs.readFileSync("package.json", "utf8"));
assert.deepEqual(j, {
version: p.version,
commit: "commitinfo",
build: "buildinfo",
source: "https://github.com/mdn/mdn-http-observatory",
});
});

it("serves the root path with a greeting", async function () {
const app = await createServer();
const response = await app.inject({
Expand Down
Loading