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

Add set labels to console output #35

Merged
merged 8 commits into from
Sep 14, 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
if: success()
continue-on-error: true
run: |
npm test
npm run test:ci

- name: Upload coverage to Codecov
if: steps.test.outcome != 'skipped'
Expand Down
25 changes: 22 additions & 3 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Please ensure that your changes pass all existing tests and add new tests for an
To run the tests, use the following command:

```bash
npm test
npm test:ci
```

This command will:
Expand All @@ -58,7 +58,13 @@ This command will:
If you want to run only the Jest tests without cleaning or linting, you can use:

```bash
npx jest
npm test
```

or with debugging

```bash
npm test:debug
```

### Running Individual Tests
Expand All @@ -68,9 +74,22 @@ To run a specific test file, use:
```bash
npx jest path/to/your/test-file.test.ts
```

or

```bash
npm test -- path/to/your/test-file.test.ts
```

Replace `path/to/your/test-file.test.ts` with the actual path to the test file you want to run.

To run a specific test file, use the `--coverage=false`:
To run a specific test file without coverage, use the `--coverage=false`:

```bash
npm test -- --coverage=false path/to/your/test-file.test.ts
```

or

```bash
npx jest --coverage=false path/to/your/test-file.test.ts
Expand Down
13 changes: 12 additions & 1 deletion __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: {
payload: {
action: 'opened',
},
repo: {
repo: 'testRepo',
owner: 'testOwner',
Expand Down Expand Up @@ -104,6 +107,8 @@ describe('Relabeler', () => {
expect(mockedCore.setOutput).toHaveBeenCalledWith('time', expect.any(String));
expect(loadConfig).toHaveBeenCalledWith('/test/workspace', undefined);
expect(mockedCore.debug).toHaveBeenCalledWith(`Loaded config: ${JSON.stringify(mockConfig)}`);

expect(core.setFailed).not.toHaveBeenCalled();
});

it('should load all possible properties in the config', async () => {
Expand Down Expand Up @@ -133,6 +138,8 @@ describe('Relabeler', () => {
expect(removeConditions).toContainEqual({ when: { labeled: 'bug' } });
expect(removeConditions).toContainEqual({ when: { labeled: ['feature', 'enhancement'] } });
expect(removeConditions).toContainEqual({ when: { reviewApproved: false } });

expect(core.setFailed).not.toHaveBeenCalled();
});
});

Expand All @@ -151,6 +158,8 @@ describe('Relabeler', () => {
'repository',
'testOwner/testRepo'
);

expect(core.setFailed).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -187,6 +196,8 @@ describe('Relabeler', () => {
expect(mockedCore.setOutput).toHaveBeenCalledWith('time', expect.any(String));
expect(loadConfig).toHaveBeenCalledWith('/test/workspace', customConfigPath);
expect(mockedCore.debug).toHaveBeenCalledWith(`Loaded config: ${JSON.stringify(mockConfig)}`);

expect(core.setFailed).not.toHaveBeenCalled();
});
});

Expand All @@ -200,7 +211,7 @@ describe('Relabeler', () => {
sha: '1234567890abcdef1234567890abcdef12345678',
ref: 'refs/heads/main',
workflow: 'Test Workflow',
action: 'run',
action: 'opened',
actor: 'username',
job: 'test-job',
runNumber: 1,
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"clean": "rm -rf dist",
"clean:coverage": "rm -rf coverage testResults",
"build": "npm run clean && npx ncc build src/index.ts -o dist --source-map --license licenses.txt",
"test": "npm run clean:coverage && jest --coverage --verbose --detectOpenHandles && npm run lint",
"test": "jest --verbose --detectOpenHandles",
"test:debug": "jest --coverage=false --verbose --detectOpenHandles --debug",
"test:watch": "jest --watchAll --coverage=false --verbose --detectOpenHandles",
"test:ci": "npm run clean:coverage && jest --coverage --verbose --detectOpenHandles && npm run lint",
"lint": "eslint 'src/**/*.{ts,js}'",
"lint:fix": "eslint 'src/**/*.{ts,js}' --fix"
},
Expand All @@ -19,7 +22,8 @@
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"js-yaml": "^4.1.0",
"ajv": "^8.17.1"
"ajv": "^8.17.1",
"@octokit/webhooks-types": "^7.5.1"
},
"devDependencies": {
"typescript": "5.5.4",
Expand Down
30 changes: 30 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import { loadConfig } from './config';
import { PullRequestOpenedEvent, PullRequestReopenedEvent, PullRequestClosedEvent, PullRequestLabeledEvent, PullRequestUnlabeledEvent, PullRequestEditedEvent } from '@octokit/webhooks-types';

export async function run() {
try {
Expand All @@ -27,6 +28,35 @@
const time = (new Date()).toTimeString();
console.log(`Setting output time: ${time}`);
core.setOutput('time', time);

let eventType: string = '';

const eventName = github.context.eventName;

// TODO: Must mock this part of the code
if ('action' in github.context.payload) {
eventType = github.context.payload.action ?? '';
}

console.log(`Event name: ${eventName}`);
console.log(`Event type: ${eventType}`);

let labels: string[] = [];

switch (eventName) {
case 'pull_request': {
const payload = github.context.payload as PullRequestOpenedEvent | PullRequestReopenedEvent | PullRequestClosedEvent | PullRequestLabeledEvent | PullRequestUnlabeledEvent | PullRequestEditedEvent;
labels = payload.pull_request.labels.map(label => label.name);

Check warning on line 49 in src/action.ts

View check run for this annotation

Codecov / codecov/patch

src/action.ts#L48-L49

Added lines #L48 - L49 were not covered by tests

break;

Check warning on line 51 in src/action.ts

View check run for this annotation

Codecov / codecov/patch

src/action.ts#L51

Added line #L51 was not covered by tests
}

// Add other event cases if needed
default:
console.log(`Unhandled event: ${eventName}`);
}

console.log(`Collected labels: ${labels.join(', ')}`);
} catch (error) {
if (error instanceof Error) {
if (!error.message.startsWith('Mock')) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictNullChecks": true, /* Needed for types in @octokit/webhooks-types, see https://github.com/octokit/webhooks/issues/395. When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
Expand Down
Loading