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

feat: support adding issue labels #29

Merged
merged 1 commit into from
Oct 8, 2023
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: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[Requirements](#requirements) | [Configuration](#configuration) | [Publishing new release](#publishing-new-release)

`eslint-remote-tester-run-action` is a pre-configured Github workflow action for running [`eslint-remote-tester`](https://github.com/AriPerkkio/eslint-remote-tester).
It runs `eslint-remote-tester` and posts results in Github issue. Results are commented on existing open issue if present.
It runs `eslint-remote-tester` and posts results in Github issue.
Results are commented on existing **open** issue if present. Existing issues are searched based on `issue-label` if present. Otherwise `issue-title` will be used.

Check out the use case description from eslint-remote-tester's documentation: [Plugin maintainer making sure all existing rules do not crash](https://github.com/AriPerkkio/eslint-remote-tester#plugin-maintainer-making-sure-all-existing-rules-do-not-crash).

Expand Down Expand Up @@ -50,6 +51,7 @@ jobs:
- uses: AriPerkkio/eslint-remote-tester-run-action@v2
with:
issue-title: 'Results of weekly scheduled smoke test'
issue-label: 'smoke-test'
max-result-count: 100
eslint-remote-tester-config: test/smoke/eslint-remote-tester.config.js
```
Expand All @@ -60,6 +62,7 @@ jobs:
| :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: | :--------------------------------------------: | :----------------------------------------- |
| `github-token` | Token for Github Authentication. See [About the `GITHUB_TOKEN` secret](https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret). | :x: | `${{github.token}}` | `${{secrets.SOME_CUSTOM_TOKEN}}` |
| `issue-title` | Title of issue created for reporting results | :x: | `'Results of eslint-remote-tester-run-action'` | `'Results of weekly scheduled smoke test'` |
| `issue-label` | Label used on the created issue | :x: | :x: | `'smoke-test'` |
| `eslint-remote-tester-config` | Path to project's `eslint-remote-tester.config.js` | :x: | `'eslint-remote-tester.config.js'` | `./path/to/custom.config.js` |
| `max-result-count` | Maximum result count to be posted in result comment. | :x: | `50` | `100` |
| `working-directory` | The working directory where action is run | :x: | :x: | `./ci` |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
description: 'Title of issue created for reporting results'
reqired: false
default: 'Results of eslint-remote-tester-run-action'
issue-label:
description: 'Label used on the created issue'
reqired: false
eslint-remote-tester-config:
description: 'Path to eslint-remote-tester.config.js'
reqired: false
Expand Down
10 changes: 7 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5763,9 +5763,11 @@ var core = __toModule(require_core());
var import_github = __toModule(require_github());
var githubToken;
var issueTitle;
var issueLabel;
try {
githubToken = core.getInput("github-token");
issueTitle = core.getInput("issue-title", {required: true});
issueLabel = core.getInput("issue-label");
} catch (error2) {
core.setFailed(error2.message);
}
Expand Down Expand Up @@ -5801,26 +5803,28 @@ var GithubClient = class {
}));
}
async getExistingIssue() {
const query = issueLabel ? `label:"${issueLabel}"` : `${issueTitle} in:title`;
const response = await this.requestAndRetry(() => this.octokit.search.issuesAndPullRequests({
sort: "created",
order: "desc",
q: [
`${issueTitle} in:title`,
query,
"is:issue",
"is:open",
`repo:${import_github.context.repo.owner}/${import_github.context.repo.repo}`
].join(" ")
}));
const {items} = response.data;
core.info(`Found ${items.length} open issues with title ${issueTitle}`);
const issue = items.find((a) => a.title === issueTitle);
core.info(`Found ${items.length} open issues matcing query (${query})`);
const issue = items[0];
return issue ? issue.number : void 0;
}
async createIssue(body) {
await this.requestAndRetry(() => this.octokit.issues.create({
owner: import_github.context.repo.owner,
repo: import_github.context.repo.repo,
title: issueTitle,
labels: issueLabel ? [issueLabel] : void 0,
body
}));
}
Expand Down
14 changes: 11 additions & 3 deletions src/github-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { context, getOctokit } from '@actions/github';

let githubToken: string;
let issueTitle: string;
let issueLabel: string | undefined;

try {
githubToken = core.getInput('github-token');
issueTitle = core.getInput('issue-title', { required: true });
issueLabel = core.getInput('issue-label');
} catch (error) {
core.setFailed(error.message);
}
Expand Down Expand Up @@ -68,12 +70,17 @@ class GithubClient {
}

private async getExistingIssue(): Promise<number | undefined> {
// Look for existing issues based on issue label if present. Otherwise use issue title
const query = issueLabel
? `label:"${issueLabel}"`
: `${issueTitle} in:title`;

const response = await this.requestAndRetry(() =>
this.octokit.search.issuesAndPullRequests({
sort: 'created',
order: 'desc',
q: [
`${issueTitle} in:title`,
query,
'is:issue',
'is:open',
`repo:${context.repo.owner}/${context.repo.repo}`,
Expand All @@ -82,10 +89,10 @@ class GithubClient {
);

const { items } = response.data;
core.info(`Found ${items.length} open issues with title ${issueTitle}`);
core.info(`Found ${items.length} open issues matcing query (${query})`);

// In case of many matches use the latest issue
const issue = items.find(a => a.title === issueTitle);
const issue = items[0];
return issue ? issue.number : undefined;
}

Expand All @@ -95,6 +102,7 @@ class GithubClient {
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
labels: issueLabel ? [issueLabel] : undefined,
body,
})
);
Expand Down
2 changes: 1 addition & 1 deletion test/__mocks__/GithubAPI.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default setupServer(
items: mockNoExistingIssues()
? []
: [
{ title: 'Should not match', number: 1 },
{ title, number: expectedIssueNumber },
{ title: 'Should not match', number: 1 },
],
})
);
Expand Down
6 changes: 5 additions & 1 deletion test/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import GithubAPI from './__mocks__/GithubAPI.mock';

jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation(key => `mock-${key}`),
getInput: jest
.fn()
.mockImplementation(key =>
key === 'issue-label' ? undefined : `mock-${key}`
),
info: jest.fn(),
}));

Expand Down
Loading