Skip to content

Commit

Permalink
Add delete command and delete test issues (upstream) (#10)
Browse files Browse the repository at this point in the history
* Delete test issues after integ tests

* Inline issueId for delete mutation

* Add command to delete an issue

* Fix typo

---------

Co-authored-by: arkon <arkon@users.noreply.github.com>
  • Loading branch information
stevenyomi and arkon authored Mar 2, 2024
1 parent a017be8 commit 454c6a6
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ permissions:
jobs:
build:
runs-on: ubuntu-latest
outputs:
self_mutation_happened: ${{ steps.self_mutation.outputs.self_mutation_happened }}

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/generate-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,4 @@ jobs:
branch: github-actions/generate-js/${{ github.ref_name }}
title: Generate JavaScript for branch `${{ github.ref_name }}`
body: |
This pull pequest is opened automatically and contains generated JavaScript code based on commit ${{ github.sha }}. The changes are the following (context excluded):
```
${{ steps.diff.outputs.GIT_DIFF }}
```
This pull request is opened automatically and contains generated JavaScript code based on commit ${{ github.sha }}.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ If `member-token` is not provided, `repo-token` will be used to check user's mem

### Commands

| Name | Description | Default value |
| -------------------- | --------------------------------------- | --------------- |
| `blurb-command` | Optional blurb command text. | Blurb |
| `duplicate-command` | Optional duplicate command text. | Duplicate of # |
| `edit-title-command` | Optional edit issue title command text. | Edit title to |
| `lock-command` | Optional lock command text. | Lock this issue |
| Name | Description | Default value |
| -------------------- | --------------------------------------- | ----------------- |
| `blurb-command` | Optional blurb command text. | Blurb |
| `delete-command` | Optional delete command text. | Delete this issue |
| `duplicate-command` | Optional duplicate command text. | Duplicate of # |
| `edit-title-command` | Optional edit issue title command text. | Edit title to |
| `lock-command` | Optional lock command text. | Lock this issue |

---

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
required: false
default: Blurb
description: Command to close an issue with a blurb posted as a comment
delete-command:
required: false
default: Delete this issue
description: Command to delete an issue
edit-title-command:
required: false
default: Edit title to
Expand Down
5 changes: 5 additions & 0 deletions integ/duplicate-url-check.integ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Octokit } from '@octokit/action';
import { beforeAll, describe, expect, test } from 'vitest';

import { baseIssueMetadata, waitForClosedIssue } from './util';
import { deleteIssue } from '../src/util/issues';

const octokit = new Octokit();

Expand All @@ -23,6 +24,8 @@ describe('Duplicate URL check', () => {
issue_number: createdIssue.data.number,
state: 'closed',
});

await deleteIssue(octokit, createdIssue.data.node_id);
};
});

Expand All @@ -39,5 +42,7 @@ describe('Duplicate URL check', () => {
expect(issue.data.state).toStrictEqual('closed');
expect(issue.data.state_reason).toStrictEqual('not_planned');
expect(issue.data.labels.map((l: any) => l.name)).toContain('duplicate');

await deleteIssue(octokit, issue.data.node_id);
});
});
3 changes: 3 additions & 0 deletions integ/existing-source-check.integ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Octokit } from '@octokit/action';
import { describe, expect, test } from 'vitest';

import { baseIssueMetadata, waitForClosedIssue } from './util';
import { deleteIssue } from '../src/util/issues';

const octokit = new Octokit();

Expand All @@ -18,5 +19,7 @@ describe('Existing source check', () => {

expect(issue.data.state).toStrictEqual('closed');
expect(issue.data.state_reason).toStrictEqual('not_planned');

await deleteIssue(octokit, issue.data.node_id);
});
});
2 changes: 2 additions & 0 deletions integ/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Octokit } from '@octokit/action';

import { GitHubClient } from '../src/types';

export const baseIssueMetadata = {
owner: 'keiyoushi',
repo: 'issue-moderator-action',
Expand Down
26 changes: 26 additions & 0 deletions src/feature/commands/delete-issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as core from '@actions/core';
import * as github from '@actions/github';

import { GitHubClient } from '../../types';
import { deleteIssue as deleteIssueUtil } from '../../util/issues';

import { BOT_REGEX } from '.';

export async function deleteIssue(client: GitHubClient, commentBody: string) {
// If the comment was a question, don't execute the command.
if (!commentBody.match(BOT_REGEX) && commentBody.match(/#\d{3,4}\?/)) {
core.info('Issue not closed because the comment contains a question');
return;
}

const { issue } = github.context;

const issueMetadata = {
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
};
const issueData = await client.rest.issues.get(issueMetadata);

await deleteIssueUtil(client, issueData.data.node_id);
}
7 changes: 6 additions & 1 deletion src/feature/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { IssueCommentEvent } from '@octokit/webhooks-types/schema';
import { GitHubClient } from '../../types';
import { minimizeComment } from '../../util/comments';

import { handleBlurb } from './blurbs';
import { closeDuplicateIssue } from './close-duplicate-issue';
import { deleteIssue } from './delete-issue';
import { editIssueTitle } from './edit-issue-title';
import { lockIssue } from './lock-issue';
import { handleBlurb } from './blurbs';

type CommandFn = (client: GitHubClient, commentBody: string) => Promise<void>;
interface Command {
Expand All @@ -24,6 +25,10 @@ const COMMANDS: Record<string, Command> = {
minimizeComment: true,
fn: handleBlurb,
},
delete: {
minimizeComment: false,
fn: deleteIssue,
},
duplicate: {
minimizeComment: false,
fn: closeDuplicateIssue,
Expand Down
16 changes: 16 additions & 0 deletions src/util/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,19 @@ export async function addLabels(
});
core.info(`Added labels: ${labels}`);
}

export async function deleteIssue(client: GitHubClient, issueId: string) {
try {
await client.graphql(
`
mutation {
deleteIssue(input: {issueId: "${issueId}", clientMutationId: "Delete test issue"}) {
clientMutationId
}
}
`,
);
} catch (error: any) {
core.warning(`Failed to delete issue: ${error.message}`);
}
}

0 comments on commit 454c6a6

Please sign in to comment.