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

[PAPI-2823] feat: read release notes from file path #34

Merged
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
28 changes: 20 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions readFile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const core = require('@actions/core');
const { promises: fs } = require('fs');
const { promises: fsAsync, fsSync } = require('fs');
const yaml = require('yaml');

const readFile = async (path) => {
const readSchemaFile = async (path) => {

core.info(`Reading ${path} file ...`);
let content = await fs.readFile(path, 'utf8');
let content = await fsAsync.readFile(path, 'utf8');

try {
core.debug(`File read`);
Expand All @@ -19,6 +19,13 @@ const readFile = async (path) => {
} catch (ye) { /* empty */ }
throw e;
}
}
};

module.exports = readFile;
const readReleaseNotes = async (path) => {
if (fsSync.existsSync(path)) {
let content = await fsAsync.readFile(path, 'utf8');
return content.toString();
}
};

module.exports = { readSchemaFile, readReleaseNotes };
10 changes: 5 additions & 5 deletions readFile.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const { promises: fs } = require('fs');
const readFile = require('./readFile');
const { readSchemaFile } = require('./readFile');


describe('test readFile', () => {
test("retrieves the file contents if it's a valid JSON and formats code", async () => {
jest.spyOn(fs, 'readFile').mockImplementation(() => Buffer.from('{"field": "value"}'));
const content = await readFile("index.json");
const content = await readSchemaFile("index.json");
expect(content).toEqual('{\n "field": "value"\n}');
});
test("throws an exception when the contents of the file are not a valid JSON", async () => {
jest.spyOn(fs, 'readFile').mockImplementation(() => Buffer.from('wrong json'));
await expect(readFile("index.json")).rejects.toThrow(SyntaxError) ;
await expect(readSchemaFile("index.json")).rejects.toThrow(SyntaxError) ;
});
test("retrieves the file contents if it's a valid YAML and formats code", async () => {
jest.spyOn(fs, 'readFile').mockImplementation(() => Buffer.from('foo: [24,"42"]\n'));
const content = await readFile("index.yaml");
const content = await readSchemaFile("index.yaml");
expect(content).toEqual("foo: [24,\"42\"]\n");
});
test("throws an exception when the contents of the file are not a valid YAML", async () => {
jest.spyOn(fs, 'readFile').mockImplementation(() => Buffer.from('foo: ,'));
await expect(readFile("index.yaml")).rejects.toThrow(SyntaxError) ;
await expect(readSchemaFile("index.yaml")).rejects.toThrow(SyntaxError) ;
});
});

11 changes: 8 additions & 3 deletions run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const core = require('@actions/core');
const readFile = require('./readFile');
const { readSchemaFile, readReleaseNotes } = require('./readFile');
const updateSchemaFile = require('./updateSchemaFile');
const createNewVersion = require('./createNewVersion');
const getSchemaId = require("./getSchemaId");
Expand All @@ -13,25 +13,30 @@ async function run() {
const fileName = core.getInput('api-path-to-file-name');
const versionName = core.getInput('version-name');
const releaseNotes = core.getInput('release-notes');
const releaseNotesFileName = core.getInput('release-notes-file-name');

core.info(`Inputs:`);
core.info(` path-to-definition: ${path}`);
core.info(` api-id: ${apiId}`);
core.info(` api-path-to-file-name: ${fileName}`);
core.info(` version-name: ${versionName}`);
core.info(` release-notes: ${releaseNotes}`);
core.info(` release-notes-file-name: ${releaseNotesFileName}`);

core.info(`Retrieving the Schema id from the API ...`);
const schemaId = await getSchemaId(postmanApiKey, apiId);

core.info(`Reading OpenAPI definition file ...`);
const openAPIFileContents = await readFile(path);
const openAPIFileContents = await readSchemaFile(path);

core.info(`Updating schema file ...`);
await updateSchemaFile(postmanApiKey, apiId, schemaId, openAPIFileContents, fileName);

core.info(`Reading Release notes from file or string ...`);
const releaseNotesContent = releaseNotesFileName ? await readReleaseNotes(releaseNotesFileName) : releaseNotes;

core.info(`Creating new version ...`);
const createdVersionId = await createNewVersion(postmanApiKey, apiId, schemaId, versionName, releaseNotes);
const createdVersionId = await createNewVersion(postmanApiKey, apiId, schemaId, versionName, releaseNotesContent);

core.setOutput('createdVersionId', createdVersionId);
} catch (error) {
Expand Down
44 changes: 34 additions & 10 deletions run.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const core = require('@actions/core');

const getSchemaId = require('./getSchemaId');
const readFile = require('./readFile');
const { readSchemaFile, readReleaseNotes } = require('./readFile');
const updateSchemaFile = require('./updateSchemaFile');
const createNewVersion = require('./createNewVersion');
const run = require('./run');
Expand Down Expand Up @@ -37,18 +37,42 @@ describe('test run', () => {

test('when everything works properly', async () => {
getSchemaId.mockReturnValue('SCHEMA_ID');
readFile.mockReturnValue('text');
readSchemaFile.mockReturnValue('text');
createNewVersion.mockReturnValue(CREATED_ID);

await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readFile).toHaveBeenCalledWith('./openAPI.json');
expect(readSchemaFile).toHaveBeenCalledWith('./openAPI.json');
expect(updateSchemaFile).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', 'text', 'index.json');
expect(createNewVersion).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', '1.0.0', 'First release');
expect(core.setOutput).toHaveBeenCalledWith('createdVersionId', CREATED_ID);
});

test('when everything works properly using release notes as file path', async () => {
INPUT_VARIABLES['release-notes-file-name'] = './notes/release-v1.0.0.md';

jest.spyOn(core, 'getInput').mockImplementation((argument) => {
return INPUT_VARIABLES[argument];
});

getSchemaId.mockReturnValue('SCHEMA_ID');
readSchemaFile.mockReturnValue('text');
readReleaseNotes.mockReturnValue("*RELEASE NOTES* \n - test 1\n - test 2");
createNewVersion.mockReturnValue(CREATED_ID);

await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readSchemaFile).toHaveBeenCalledWith('./openAPI.json');
expect(readReleaseNotes).toHaveBeenCalledWith('./notes/release-v1.0.0.md');
expect(updateSchemaFile).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', 'text', 'index.json');
expect(createNewVersion).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', '1.0.0', "*RELEASE NOTES* \n - test 1\n - test 2");
expect(core.setOutput).toHaveBeenCalledWith('createdVersionId', CREATED_ID);

delete INPUT_VARIABLES['release-notes-file-name'];
});

test('when getSchemaId fails', async () => {
getSchemaId.mockImplementation(() => {
throw new Error('Error retrieving schema id');
Expand All @@ -57,29 +81,29 @@ describe('test run', () => {
await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readFile).not.toHaveBeenCalled();
expect(readSchemaFile).not.toHaveBeenCalled();
expect(updateSchemaFile).not.toHaveBeenCalled();
expect(createNewVersion).not.toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error retrieving schema id');
});

test('when readFile fails', async () => {
readFile.mockImplementation(() => {
readSchemaFile.mockImplementation(() => {
throw new Error('Error reading file');
});

await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readFile).toHaveBeenCalledWith('./openAPI.json');
expect(readSchemaFile).toHaveBeenCalledWith('./openAPI.json');
expect(updateSchemaFile).not.toHaveBeenCalled();
expect(createNewVersion).not.toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error reading file');
});

test('when updateSchemaFile fails', async () => {
getSchemaId.mockReturnValue('SCHEMA_ID');
readFile.mockReturnValue('text');
readSchemaFile.mockReturnValue('text');
updateSchemaFile.mockImplementation(() => {
const error = new Error('Error updating schema');
error.response = {
Expand All @@ -92,15 +116,15 @@ describe('test run', () => {
await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readFile).toHaveBeenCalledWith('./openAPI.json');
expect(readSchemaFile).toHaveBeenCalledWith('./openAPI.json');
expect(updateSchemaFile).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', 'text', 'index.json');
expect(createNewVersion).not.toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error updating schema. Error code: 400. Error body: "Error"');
});

test('when createNewVersion fails', async () => {
getSchemaId.mockReturnValue('SCHEMA_ID');
readFile.mockReturnValue('text');
readSchemaFile.mockReturnValue('text');
createNewVersion.mockImplementation(() => {
const error = new Error('Error creating new version');
error.response = {
Expand All @@ -113,7 +137,7 @@ describe('test run', () => {
await run();

expect(getSchemaId).toHaveBeenCalledWith('API_KEY', 'API_ID');
expect(readFile).toHaveBeenCalledWith('./openAPI.json');
expect(readSchemaFile).toHaveBeenCalledWith('./openAPI.json');
expect(updateSchemaFile).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', 'text', 'index.json');
expect(createNewVersion).toHaveBeenCalledWith('API_KEY', 'API_ID', 'SCHEMA_ID', '1.0.0', 'First release');
expect(core.setFailed).toHaveBeenCalledWith('Error creating new version. Error code: 400. Error body: "Error"');
Expand Down
Loading