Skip to content

Commit

Permalink
diff endpoint for single file (#183)
Browse files Browse the repository at this point in the history
to support some advanced change-type processes in qontract-reconciles change-owner integration, a flavour of the exiting diff endpoint is introduced to allow fetching diffs for an individual datafile or resourcefile.

`GET /diff/:sha/:another_sha/:filetype/:path` where `filetype` is `datafile` or `resourcefile`

Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle authored Jan 9, 2023
1 parent ecbabe7 commit 8ad0d28
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ The shas will expire after a certain amount of time:
- `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
- `GET /diff/:sha/:another_sha`: return the difference between two bundles
- `GET /diff/:sha/:another_sha/:filetype/:path`: return the difference for a single `datafile` or `resourcefile`

## Metrics

Expand Down
37 changes: 37 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,43 @@ export const appFromBundle = async (bundlePromises: Promise<db.Bundle>[]) => {
res.send(req.app.get('bundles')[bundleSha].fileHash);
});

app.get(
'/diff/:base_sha/:head_sha/:filetype/*?',
(req: express.Request, res: express.Response) => {
const baseBundle: db.Bundle = req.app.get('bundles')[req.params.base_sha];
const headBundle: db.Bundle = req.app.get('bundles')[req.params.head_sha];

const filepath = `/${req.params[0]}`;
if (req.params.filetype === 'datafile') {
const oldRes = baseBundle.datafiles.get(filepath);
const newRes = headBundle.datafiles.get(filepath);
if (oldRes === undefined && newRes === undefined) {
res.status(404).send('datafile not found');
} else {
res.send({
datafilepath: filepath,
datafileschema: (newRes !== undefined ? newRes : oldRes).$schema,
old: oldRes,
new: newRes,
});
}
} else if (req.params.filetype === 'resourcefile') {
const oldRes = baseBundle.resourcefiles.get(filepath);
const newRes = headBundle.resourcefiles.get(filepath);
if (oldRes === undefined && newRes === undefined) {
res.status(404).send('resourcefile not found');
} else {
res.send({
resourcepath: filepath,
old: oldRes,
new: newRes,
});
}
} else {
res.status(400).send(`unknown filetype ${req.params.filetype}`);
}
});

app.get('/diff/:base_sha/:head_sha', (req: express.Request, res: express.Response) => {
const baseBundle: db.Bundle = req.app.get('bundles')[req.params.base_sha];
const headBundle: db.Bundle = req.app.get('bundles')[req.params.head_sha];
Expand Down
40 changes: 38 additions & 2 deletions test/diff/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ describe('diff', async() => {
return response.text.should.equal(newSha);
});

it('serve diff', async() => {
it('serve full diff', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}`);
resp.should.have.status(200);
logger.info(JSON.stringify(resp.body));

const changed = resp.body.datafiles['/cluster.yml'];
changed.datafilepath.should.equal('/cluster.yml');
Expand All @@ -44,6 +43,43 @@ describe('diff', async() => {
resource.resourcepath.should.equal('/changed_resource.yml');
});

it('serve single datafile diff', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}/datafile/cluster.yml`);
resp.should.have.status(200);

resp.body.datafilepath.should.equal('/cluster.yml');
resp.body.datafileschema.should.equal('/openshift/cluster-1.yml');
resp.body.old.automationToken.path.should.equals('secret-old');
resp.body.new.automationToken.path.should.equals('secret-new');
});

it('serve single datafile diff missing', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}/datafile/does_not_exit.yml`);
resp.should.have.status(404);
});

it('serve single resourcefile diff', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}/resourcefile/changed_resource.yml`);
resp.should.have.status(200);

resp.body.resourcepath.should.equal('/changed_resource.yml');
});

it('serve single resourcefile diff not found', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}/resourcefile/does_not_exist.yml`);
resp.should.have.status(404);
});

it('serve single diff unknown file type', async() => {
const resp = await chai.request(srv)
.get(`/diff/${oldSha}/${newSha}/unknown_file_type/does_not_exist.yml`);
resp.should.have.status(400);
});

after(() => {
delete process.env.INIT_BUNDLES;
});
Expand Down

0 comments on commit 8ad0d28

Please sign in to comment.