Skip to content

Commit

Permalink
Add git-commit-info endpoint (#97)
Browse files Browse the repository at this point in the history
It returns JSON with commit sha and timestamp

Signed-off-by: Rafa Porres Molina <[email protected]>
  • Loading branch information
maorfr authored Nov 26, 2020
1 parent 05fca26 commit 4f768b3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ The shas will expire after a certain amount of time:
- `POST /graphql`: the request body should contain the GraphQL query. The query will be directed at the latest bundle.
- `GET /graphql`: redirects to `POST /graphql`.
- `GET /sha256`: returns the sha of the latest bundle.
- `GET /git-commit`: returns the git commit for the latest bundle.
- `GET /git-commit/:sha`: returns the git commit for the specified bundle.
- `GET /git-commit-info`: returns json doc with git commit information (commit sha and timestamp)
- `GET /git-commit-info/:sha`: returns json doc with git commit information (commit sha and timestamp) for the specified bundle
- `GET /cache`: returns a json with the cache information.
- `GET /reload`: reloads data from the configured data source.
- `GET /metrics`: prometheus metrics.
- `GET /git-commit`: returns the git commit for the latest bundle. (deprecated, use git-commit-info instead)
- `GET /git-commit/:sha`: returns the git commit for the specified bundle., use git-commit-info instead

## Metrics

Expand Down
3 changes: 3 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Bundle = {
schema: any[];
fileHash: string;
gitCommit: string;
gitCommitTimestamp: string;
};

const getRefPath = (ref: string): string => /^[^$]*/.exec(ref)[0];
Expand Down Expand Up @@ -68,6 +69,7 @@ const parseBundle = (contents: string) : Bundle => {
resourcefiles: parseResourcefiles(parsedContents.resources),
fileHash: hashDatafile(contents),
gitCommit: parsedContents['git_commit'],
gitCommitTimestamp: parsedContents['git_commit_timestamp'],
schema: parsedContents.graphql,
} as Bundle;
};
Expand Down Expand Up @@ -146,6 +148,7 @@ export const bundleFromEnvironment = async() => {
schema: {},
fileHash: '',
gitCommit: '',
gitCommitTimestamp: '',
} as Bundle;
}
};
23 changes: 23 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ export const appFromBundle = async (bundlePromise: Promise<db.Bundle>) => {
res.send(req.app.get('bundles')[req.params.sha].gitCommit);
});

app.get('/git-commit-info', (req: express.Request, res: express.Response) => {
const bundleSha = req.app.get('latestBundleSha');
const gitCommitInfo: any = {};
gitCommitInfo['commit'] = req.app.get('bundles')[bundleSha].gitCommit;
gitCommitInfo['timestamp'] = req.app.get('bundles')[bundleSha].gitCommitTimestamp;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(gitCommitInfo));
});

app.get('/git-commit-info/:sha', (req: express.Request, res: express.Response) => {
const gitCommitInfo: any = {};

if (!(req.params.sha in req.app.get('bundles'))) {
res.status(404).send(`Bundle ${req.params.sha} not found`);
return;
}

gitCommitInfo['commit'] = req.app.get('bundles')[req.params.sha].gitCommit;
gitCommitInfo['timestamp'] = req.app.get('bundles')[req.params.sha].gitCommitTimestamp;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(gitCommitInfo));
});

app.get('/metrics', (req: express.Request, res: express.Response) => {
res.send(promClient.register.metrics());
});
Expand Down
2 changes: 2 additions & 0 deletions test/server.data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"git_commit": "cf639ded4b97808ffae8bfd4dc3f4c183508e1ca",
"git_commit_timestamp": "1606295532",
"data": {
"/app-A.yml": {
"$schema": "/app-sre/app-1.yml",
Expand Down
20 changes: 20 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ describe('server', async () => {
return response.text.length.should.equal(64);
});

it('GET /git-commit-info returns commit information', async () => {
const response = await chai.request(srv).get('/git-commit-info');
response.body.commit.should.equal('cf639ded4b97808ffae8bfd4dc3f4c183508e1ca');
response.body.timestamp.should.equal('1606295532');
return response.should.have.status(200);
});

it('GET /git-commit-info/:sha returns commit information from sha', async () => {
const shaResponse = await chai.request(srv).get('/sha256');
const commitResponse = await chai.request(srv).get(`/git-commit-info/${shaResponse.text}`);
commitResponse.body.commit.should.equal('cf639ded4b97808ffae8bfd4dc3f4c183508e1ca');
commitResponse.body.timestamp.should.equal('1606295532');
return commitResponse.should.have.status(200);
});

it('GET /git-commit-info/:sha returns 404 on unknown sha', async () => {
const response = await chai.request(srv).get('/git-commit-info/LOL');
return response.should.have.status(404);
});

it('resolves item refs', async () => {
const query = `{
roles: roles_v1 {
Expand Down

0 comments on commit 4f768b3

Please sign in to comment.