Skip to content

Commit

Permalink
feat: Add optional project_name to action (#755)
Browse files Browse the repository at this point in the history
* feat: Add optional project_name to action

Add project_name property as optional so that the comments created by
the Github action are more unique, allowing monorepos with multiple
projects to use the editCommentOnPr flag properly

* Refactor approach to not pass projectName via user input

* Refactor projectSettings usage

* Refactor projectName to pass directly to handlePullRequestMessage

* Build project in order to test it locally

* Revert "Build project in order to test it locally"

This reverts commit eba3969.

* Fix rebase issues
  • Loading branch information
miguel-botelho authored Nov 10, 2022
1 parent 6c5830d commit a9fe814
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

- feat: Always show login information

- feat: Includes the project name in comments, eliminating conflicts when
working with multiple projects in the same repository.
([#563](https://github.com/pulumi/actions/issues/563))

--

## 3.19.1 (2022-09-06)
Expand Down
41 changes: 36 additions & 5 deletions src/libs/__tests__/pr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import * as gh from '@actions/github';
import { Config } from '../../config';
import { handlePullRequestMessage } from '../pr';

const comments = [{ id: 2, body: 'test' }];
const comments = [{ id: 2, body: '#### :tropical_drink: `preview` on myFirstProject/staging. <summary>Pulumi report</summary>' }];
const resp = { data: comments };
const projectName = 'myFirstProject';
const defaultOptions = {
command: 'preview',
stackName: 'staging',
options: {},
} as Config;
const createComment = jest.fn();
const updateComment = jest.fn();
const listComments = jest.fn(() => resp);
jest.mock('@actions/github', () => ({
context: {},
Expand All @@ -13,6 +20,7 @@ jest.mock('@actions/github', () => ({
issues: {
createComment,
listComments,
updateComment,
},
},
})),
Expand All @@ -35,16 +43,19 @@ describe('pr.ts', () => {

process.env.GITHUB_REPOSITORY = 'pulumi/actions';

await handlePullRequestMessage({ options: {} } as Config, 'test');
expect(createComment).toHaveBeenCalled();
await handlePullRequestMessage(defaultOptions, projectName, 'test');
expect(createComment).toHaveBeenCalledWith({
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>',
issue_number: 123
});
});

it('should fail if no pull request data', async () => {
process.env.GITHUB_REPOSITORY = 'pulumi/actions';
// @ts-ignore
gh.context = { payload: {} };
await expect(
handlePullRequestMessage({ options: {} } as Config, 'test'),
handlePullRequestMessage(defaultOptions, projectName, 'test'),
).rejects.toThrow('Missing pull request event data');
});

Expand All @@ -60,12 +71,32 @@ describe('pr.ts', () => {
};

await handlePullRequestMessage(
{ options: {} } as Config,
defaultOptions,
projectName,
'a'.repeat(65_000),
);

const call = createComment.mock.calls[0][0];
expect(call.body.length).toBeLessThan(65_536);
expect(call.body).toContain('The output was too long and trimmed');
});

it('should edit the comment if it finds a previous created one', async () => {
// @ts-ignore
gh.context = { repo: {} };

const options = {
command: 'preview',
stackName: 'staging',
commentOnPrNumber: 123,
options: { editCommentOnPr: true },
} as Config;


await handlePullRequestMessage(options, projectName, 'test');
expect(updateComment).toHaveBeenCalledWith({
comment_id: 2,
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>'
});
});
});
4 changes: 3 additions & 1 deletion src/libs/pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Config } from '../config';

export async function handlePullRequestMessage(
config: Config,
projectName: string,
output: string,
): Promise<void> {
const {
Expand All @@ -15,7 +16,8 @@ export async function handlePullRequestMessage(
options: { editCommentOnPr },
} = config;

const heading = `#### :tropical_drink: \`${command}\` on ${stackName}`;
const heading = `#### :tropical_drink: \`${command}\` on ${projectName}/${stackName}`;

const summary = '<summary>Pulumi report</summary>';

const rawBody = output.substring(0, 64_000);
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const main = async () => {
? LocalWorkspace.createOrSelectStack(stackArgs, stackOpts)
: LocalWorkspace.selectStack(stackArgs, stackOpts));

const projectSettings = await stack.workspace.projectSettings();
const projectName = projectSettings.name;

const onOutput = (msg: string) => {
core.debug(msg);
core.info(msg);
Expand Down Expand Up @@ -93,7 +96,7 @@ const main = async () => {
if (config.commentOnPr && config.isPullRequest) {
core.debug(`Commenting on pull request`);
invariant(config.githubToken, 'github-token is missing.');
handlePullRequestMessage(config, output);
handlePullRequestMessage(config, projectName, output);
}

core.endGroup();
Expand Down

0 comments on commit a9fe814

Please sign in to comment.