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

Support IsDryRun within http-request backend action #1233

Merged
merged 1 commit into from
Feb 8, 2024
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its probably a minor, but not a big deal.

---

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
Loading