Skip to content

Commit

Permalink
Merge pull request #1233 from electrofelix/support-dry-run-http-request
Browse files Browse the repository at this point in the history
Support IsDryRun within http-request backend action
  • Loading branch information
punkle authored Feb 8, 2024
2 parents e8d1a74 + 9bfc49e commit 4738a82
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-beers-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/scaffolder-backend-module-http-request': patch
---

Allow dry-run for HTTP methods that don't modify resources
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('http:backstage:request', () => {
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
isDryRun: false,
};

describe('when the action runs correctly', () => {
Expand Down Expand Up @@ -389,5 +390,65 @@ describe('http:backstage:request', () => {
);
});
});

describe('with dry run enabled', () => {
beforeEach(() => {
mockContext.isDryRun = true;
});

afterEach(() => {
mockContext.isDryRun = false;
});

it('should call http when a safe method is passed', async () => {
(http as jest.Mock).mockReturnValue({
code: 200,
headers: {},
body: {},
});
const expectedLog =
'Creating GET request with http:backstage:request scaffolder action';
await action.handler({
...mockContext,
input: {
path: '/api/proxy/foo',
method: 'GET',
logRequestPath: false,
},
});
expect(loggerSpy).toBeCalledTimes(1);
expect(loggerSpy.mock.calls[0]).toContain(expectedLog);
expect(http).toBeCalledWith(
{
url: 'http://backstage.tests/api/proxy/foo',
method: 'GET',
headers: {},
},
logger,
false,
);
});

it('should skip when an unsafe method is passed', async () => {
(http as jest.Mock).mockReturnValue({
code: 200,
headers: {},
body: {},
});
const expectedLog =
"Dry run mode. Skipping non dry-run safe method 'POST' request to http://backstage.tests/api/proxy/foo";
await action.handler({
...mockContext,
input: {
path: '/api/proxy/foo',
method: 'POST',
logRequestPath: false,
},
});
expect(loggerSpy).toBeCalledTimes(2);
expect(loggerSpy.mock.calls[1]).toContain(expectedLog);
expect(http).not.toHaveBeenCalled();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function createHttpBackstageAction(options: {
id: 'http:backstage:request',
description:
'Sends a HTTP request to the Backstage API. It uses the token of the user who triggers the task to authenticate requests.',
supportsDryRun: true,
schema: {
input: {
type: 'object',
Expand Down Expand Up @@ -167,6 +168,14 @@ export function createHttpBackstageAction(options: {
httpOptions.headers.authorization = `Bearer ${token}`;
}

const dryRunSafeMethods = new Set(['GET', 'HEAD', 'OPTIONS']);
if (ctx.isDryRun === true && !dryRunSafeMethods.has(method)) {
ctx.logger.info(
`Dry run mode. Skipping non dry-run safe method '${method}' request to ${httpOptions.url}`,
);
return;
}

const { code, headers, body } = await http(
httpOptions,
ctx.logger,
Expand Down

0 comments on commit 4738a82

Please sign in to comment.