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

Implement codeowner commands to add/remove labels #215

Merged
merged 1 commit into from
Oct 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CodeOwnersEntry, matchFile } from 'codeowners-utils';
import { ISSUE_COMMENT_COMMANDS } from './issue_comment_commands/handler';

const generateCodeOwnersMentionComment = (
context: WebhookContext<IssuesLabeledEvent | PullRequestLabeledEvent>,
integration: string,
codeOwners: string[],
triggerLabel: string,
Expand All @@ -34,7 +35,7 @@ ${ISSUE_COMMENT_COMMANDS.filter((command) => command.invokerType === 'code_owner
]
.filter((item) => item !== undefined)
.join(' ')
.trim()}\` ${command.description?.replace('<type>', triggerLabel)}`,
.trim()}\` ${command.description(context)}`,
)
.join('\n')}

Expand Down Expand Up @@ -119,6 +120,7 @@ export class CodeOwnersMention extends BaseWebhookHandler {
context.scheduleIssueComment({
handler: 'CodeOwnersMention',
comment: generateCodeOwnersMentionComment(
context,
integrationName,
mentions,
triggerLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import { IssueCommentCommandContext } from '../const';

export class IssueCommentCommandBase {
export abstract class IssueCommentCommandBase {
command: string;
description: string;
invokerType?: string;
requireAdditional?: boolean;
exampleAdditional?: string;

async handle(
abstract description(context: WebhookContext<any>): string;

abstract handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
) {}
): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import { invokerIsCodeOwner, IssueCommentCommandContext } from '../const';
import { invokerIsCodeOwner, IssueCommentCommandContext, triggerType } from '../const';
import { IssueCommentCommandBase } from './base';

export class CloseIssueCommentCommand extends IssueCommentCommandBase {
command = 'close';
description = 'Closes the <type>.';
invokerType = 'code_owner';

description(context: WebhookContext<any>) {
return `Closes the ${triggerType(context)}.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import {
invokerIsCodeOwner,
IssueCommentCommandContext,
ManageableLabels,
triggerType,
} from '../const';
import { IssueCommentCommandBase } from './base';

export class LabelAddCommentCommand implements IssueCommentCommandBase {
command = 'add-label';
exampleAdditional = 'needs-more-information';
invokerType = 'code_owner';
requireAdditional = true;

description(context: WebhookContext<any>) {
const validLabels = Array.from(ManageableLabels[context.repository]);
return `Add a label (${validLabels.join(', ')}) to the ${triggerType(context)}.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
) {
if (!invokerIsCodeOwner(command)) {
throw new Error('Only the code owner can add labels.');
}

if (!ManageableLabels[context.repository].has(command.additional)) {
throw new Error(
`The requested label ${command.additional} is not valid for ${context.repository}`,
);
}
await context.github.issues.addLabels(context.issue({ labels: [command.additional] }));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import {
invokerIsCodeOwner,
IssueCommentCommandContext,
ManageableLabels,
triggerType,
} from '../const';
import { IssueCommentCommandBase } from './base';

export class LabelRemoveCommentCommand implements IssueCommentCommandBase {
command = 'remove-label';
exampleAdditional = 'needs-more-information';
invokerType = 'code_owner';
requireAdditional = true;

description(context: WebhookContext<any>) {
const validLabels = Array.from(ManageableLabels[context.repository]);
return `Remove a label (${validLabels.join(', ')}) on the ${triggerType(context)}.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
) {
if (!invokerIsCodeOwner(command)) {
throw new Error('Only the code owner can remove labels.');
}

if (!ManageableLabels[context.repository].has(command.additional)) {
throw new Error(
`The requested label ${command.additional} is not valid for ${context.repository}`,
);
}

if (!command.currentLabels.includes(command.additional)) {
throw new Error(`The requested label ${command.additional} is not active on the issue.`);
}

await context.github.issues.removeLabel(context.issue({ name: command.additional }));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import { invokerIsCodeOwner, IssueCommentCommandContext } from '../const';
import { invokerIsCodeOwner, IssueCommentCommandContext, triggerType } from '../const';
import { IssueCommentCommandBase } from './base';

export class RenameIssueCommentCommand implements IssueCommentCommandBase {
command = 'rename';
description = 'Renames the <type>.';
exampleAdditional = 'Awesome new title';
invokerType = 'code_owner';
requireAdditional = true;

description(context: WebhookContext<any>) {
return `Renames the ${triggerType(context)}.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import { invokerIsCodeOwner, IssueCommentCommandContext } from '../const';
import { invokerIsCodeOwner, IssueCommentCommandContext, triggerType } from '../const';
import { IssueCommentCommandBase } from './base';

export class ReopenIssueCommentCommand implements IssueCommentCommandBase {
command = 'reopen';
description = 'Reopen the <type>.';
invokerType = 'code_owner';

description(context: WebhookContext<any>) {
return `Reopen the ${triggerType(context)}.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../../github-webhook.model';
import { invokerIsCodeOwner, IssueCommentCommandContext } from '../const';
import { invokerIsCodeOwner, IssueCommentCommandContext, triggerType } from '../const';
import { IssueCommentCommandBase } from './base';

export class UnassignIssueCommentCommand implements IssueCommentCommandBase {
command = 'unassign';
description =
'Removes the current integration label and assignees on the <type>, add the integration domain after the command.';
exampleAdditional = '<domain>';
invokerType = 'code_owner';
requireAdditional = true;

description(context: WebhookContext<any>) {
return `Removes the current integration label and assignees on the ${triggerType(
context,
)}, add the integration domain after the command.`;
}

async handle(
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { IssueCommentCreatedEvent } from '@octokit/webhooks-types';
import { WebhookContext } from '../../github-webhook.model';
import { IntegrationManifest } from '../../utils/integration';
import { HomeAssistantRepository } from '../../github-webhook.const';

export const ManageableLabels = {
[HomeAssistantRepository.CORE]: new Set([
'needs-more-information',
'problem in dependency',
'problem in custom component',
]),
[HomeAssistantRepository.HOME_ASSISTANT_IO]: new Set(['needs-more-information']),
};

export const triggerType = (context: WebhookContext<any>) =>
context.repository === HomeAssistantRepository.CORE
? context.eventType.startsWith('issues')
? 'issue'
: 'pull request'
: 'feedback';

export interface IssueCommentCommandContext {
invoker: string;
Expand All @@ -9,17 +26,6 @@ export interface IssueCommentCommandContext {
integrationManifests: { [domain: string]: IntegrationManifest };
}

export interface IssueCommentCommand {
description: string;
exampleAdditional?: string;
invokerType?: 'code_owner';
requireAdditional?: boolean;
handler: (
context: WebhookContext<IssueCommentCreatedEvent>,
command: IssueCommentCommandContext,
) => Promise<void>;
}

export const invokerIsCodeOwner = (
command: IssueCommentCommandContext,
manifest?: IntegrationManifest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import { CloseIssueCommentCommand } from './commands/close';
import { RenameIssueCommentCommand } from './commands/rename';
import { ReopenIssueCommentCommand } from './commands/reopen';
import { UnassignIssueCommentCommand } from './commands/unassign';
import { LabelAddCommentCommand } from './commands/label-add';
import { LabelRemoveCommentCommand } from './commands/label-remove';

const COMMAND_REGEX: RegExp = /^(?<tagged>@home-assistant)\s(?<command>\w*)(\s(?<additional>.*))?$/;
const COMMAND_REGEX: RegExp =
/^(?<tagged>@home-assistant)\s(?<command>[\w|-]*)(\s(?<additional>.*))?$/;

export const ISSUE_COMMENT_COMMANDS: IssueCommentCommandBase[] = [
new CloseIssueCommentCommand(),
new RenameIssueCommentCommand(),
new ReopenIssueCommentCommand(),
new UnassignIssueCommentCommand(),
new LabelAddCommentCommand(),
new LabelRemoveCommentCommand(),
];

export class IssueCommentCommands extends BaseWebhookHandler {
Expand Down
Loading