Skip to content

Commit

Permalink
feat: rewrite action and add new commands (#245)
Browse files Browse the repository at this point in the history
* refactor: update function params

* test pr to fix release action

* refactor: update comment class to support other cmds

* still refactoring...

* added a few commands, next -> fix tests

* ignore tests for now

* refactor cmds and add debug logs

* debugging...

* hum, weird

* debugging /assign cmd

* oh boy

* testing action output

* return after setting output

* testing yaml arrays and comment suggestion

* revert maintainers array input

* move common texts to an array

* ship it
  • Loading branch information
takanome-dev authored Oct 19, 2024
1 parent 54dd171 commit cc6c349
Show file tree
Hide file tree
Showing 51 changed files with 43,997 additions and 24,685 deletions.
Empty file modified .eslintrc.json
100644 → 100755
Empty file.
Empty file modified .github/CODEOWNERS
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/bug.yml
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/documentation.yml
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/feature_request.yml
100644 → 100755
Empty file.
Empty file modified .github/PULL_REQUEST_TEMPLATE.md
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
language: ['javascript']
node-version: [18.x]
node-version: [20.x]

steps:
- name: Checkout repository
Expand All @@ -27,7 +27,7 @@ jobs:
run: npm ci

- name: Build files
run: npm run build
run: npm run tsup

- name: Setup CodeQL
uses: github/codeql-action/init@v3
Expand Down
Empty file modified .github/workflows/development.yml
100644 → 100755
Empty file.
150 changes: 0 additions & 150 deletions .github/workflows/release.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ coverage/
.dccache

TODO.md
.env
.env
test.json
Empty file modified .husky/.gitignore
100644 → 100755
Empty file.
Empty file modified .prettierignore
100644 → 100755
Empty file.
Empty file modified .prettierrc.json
100644 → 100755
Empty file.
Empty file modified CODE_OF_CONDUCT.md
100644 → 100755
Empty file.
Empty file modified CONTRIBUTING.md
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
56 changes: 31 additions & 25 deletions README.md
100644 → 100755

Large diffs are not rendered by default.

Empty file modified SECURITY.md
100644 → 100755
Empty file.
Empty file modified __mocks__/@actions/github.ts
100644 → 100755
Empty file.
170 changes: 170 additions & 0 deletions __tests__/comment-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import * as core from '@actions/core';
import * as github from '@actions/github';

import CommentHandler from '../src/handlers/comment-handler';
import { getDefaultValues } from '../src/utils/helpers/default-values';

jest.mock('@actions/core');
jest.mock('@actions/github');

// function calculateDaysUntilUnassign(createAt: string, totalDays: number) {
// const createdAt = new Date(createAt);
// const currentDate = new Date();
// const diffTime = Math.abs(currentDate.getTime() - createdAt.getTime());
// const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

// return totalDays - diffDays;
// }

describe('Comment Handler', () => {
// let client: ReturnType<typeof github.getOctokit>;
let cmtHandler: CommentHandler;

beforeAll(() => {
jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
return jest.requireActual('@actions/core').getInput(name, options);
});
jest.spyOn(core, 'setFailed').mockImplementation((message) => {
return jest.requireActual('@actions/core').setFailed(message);
});
jest.spyOn(core, 'info').mockImplementation((message) => {
return jest.requireActual('@actions/core').info(message);
});

// client = github.getOctokit('_');
cmtHandler = new CommentHandler();

Object.assign(process.env, getDefaultValues());
});

beforeEach(() => {
process.env.INPUT_GITHUB_TOKEN = 'token';
process.env.INPUT_REQUIRED_LABEL = '';
github.context.payload.comment!.body =
"Hey, I'm interested in this issue. Can you /assign-me please?";
});

afterAll(() => {
jest.restoreAllMocks();
});

it('should exit early if token is not provided', async () => {
process.env.INPUT_GITHUB_TOKEN = '';
await cmtHandler.handle_issue_comment();
expect(core.setFailed).toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith(
`🚫 Missing required input "token", received "${process.env.INPUT_GITHUB_TOKEN}"`,
);
});

// it('should ignore the comment if it does not contain the trigger', async () => {
// github.context.payload.comment!.body = 'Hey, can you assign this to me?';
// await commentHandler.handleAssignIssue();
// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Ignoring comment: ${github.context.payload.comment?.body}`,
// );
// });

// it('should failed if required_label is provided but not in the issue', async () => {
// commentHandler = new Comment();
// process.env.INPUT_REQUIRED_LABEL = 'Required';
// await commentHandler.handleAssignIssue();
// expect(core.setFailed).toHaveBeenCalled();
// expect(core.setFailed).toHaveBeenCalledWith(
// `🚫 Missing required label: "[${process.env.INPUT_REQUIRED_LABEL}]" label not found in issue #${github.context.payload.issue?.number}.`,
// );
// });

// it('should let the user know the issue is already assigned', async () => {
// const commenter = github.context.payload.comment!.user.login;
// const assignee = github.context.payload.issue!.assignee!.login;
// const daysUntilUnassign = calculateDaysUntilUnassign(
// github.context.payload.issue!.created_at,
// Number(process.env.INPUT_DAYS_UNTIL_UNASSIGN),
// );

// await commentHandler.handleAssignIssue();
// expect(client.rest.issues.listComments).toHaveBeenCalled();
// expect(client.rest.issues.listComments).toHaveBeenCalledWith({
// owner: 'TAKANOME-DEV',
// repo: 'test-action',
// issue_number: 1,
// });
// expect(client.rest.issues.createComment).toHaveBeenCalled();
// expect(client.rest.issues.createComment).toHaveBeenCalledWith({
// owner: 'TAKANOME-DEV',
// repo: 'test-action',
// issue_number: 1,
// body: `👋 Hey @${commenter}, this issue is already assigned to @${assignee}.

// ⚠️ It will become unassigned if it isn't closed within **${daysUntilUnassign} days**.

// 🔧 A maintainer can also add you to the list of assignees or swap you with the current assignee.`,
// });
// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Issue #${github.context.payload.issue?.number} is already assigned to @${assignee}`,
// );
// });

// it('assign the user if there is no assignee', async () => {
// github.context.payload.issue!.assignee = null;
// await commentHandler.handleAssignIssue();

// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Assigning @${github.context.payload.comment?.user.login} to issue #${github.context.payload.issue?.number}`,
// );
// expect(client.rest.issues.addAssignees).toHaveBeenCalled();
// expect(client.rest.issues.addAssignees).toHaveBeenCalledWith({
// owner: 'TAKANOME-DEV',
// repo: 'test-action',
// issue_number: 1,
// assignees: ['john-doe'],
// });
// });

// it('add "Assigned" label if user is assigned', async () => {
// await commentHandler.handleAssignIssue();

// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Assigning @${github.context.payload.comment?.user.login} to issue #${github.context.payload.issue?.number}`,
// );
// expect(client.rest.issues.addLabels).toHaveBeenCalled();
// expect(client.rest.issues.addLabels).toHaveBeenCalledWith({
// owner: 'TAKANOME-DEV',
// repo: 'test-action',
// issue_number: 1,
// labels: ['📍 Assigned'],
// });
// });

// it('should create a comment', async () => {
// const commenter = github.context.payload.comment!.user.login;
// const totalDays = process.env.INPUT_DAYS_UNTIL_UNASSIGN;
// const pinnedLabel = process.env.INPUT_PIN_LABEL;

// await commentHandler.handleAssignIssue();
// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Adding comment to issue #${github.context.payload.issue?.number}`,
// );
// expect(client.rest.issues.createComment).toHaveBeenCalled();
// expect(client.rest.issues.createComment).toHaveBeenCalledWith({
// owner: 'TAKANOME-DEV',
// repo: 'test-action',
// issue_number: 1,
// body: `👋 Hey @${commenter}, thanks for your interest in this issue! 🎉

// ⚠ Note that this issue will become unassigned if it isn't closed within **${totalDays} days**.

// 🔧 A maintainer can also add the **${pinnedLabel}** label to prevent it from being unassigned automatically.`,
// });
// expect(core.info).toHaveBeenCalled();
// expect(core.info).toHaveBeenCalledWith(
// `🤖 Issue #${github.context.payload.issue?.number} assigned!`,
// );
// });
});
Loading

0 comments on commit cc6c349

Please sign in to comment.