Add support for starting workflows

This commit is contained in:
Will Hunt 2021-11-29 16:33:57 +00:00
parent 36a3863ba3
commit 8659cb8a32

View File

@ -7,11 +7,12 @@ import { IConnection } from "./IConnection";
import { IssuesOpenedEvent, IssuesReopenedEvent, IssuesEditedEvent, PullRequestOpenedEvent, IssuesClosedEvent, PullRequestClosedEvent, PullRequestReadyForReviewEvent, PullRequestReviewSubmittedEvent, ReleaseCreatedEvent } from "@octokit/webhooks-types";
import { MatrixMessageContent, MatrixEvent, MatrixReactionContent } from "../MatrixEvent";
import { MessageSenderClient } from "../MatrixSender";
import { NotLoggedInError } from "../errors";
import { CommandError, NotLoggedInError } from "../errors";
import { RequestError } from "@octokit/types";
import { Octokit } from "@octokit/rest";
import { ReposGetResponseData } from "../Github/Types";
import { UserTokenStore } from "../UserTokenStore";
import axios from "axios";
import axios, { Axios, AxiosError } from "axios";
import emoji from "node-emoji";
import LogWrapper from "../LogWrapper";
import markdown from "markdown-it";
@ -205,8 +206,7 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
public async onAssign(userId: string, number: string, ...users: string[]) {
const octokit = await this.tokenStore.getOctokitForUser(userId);
if (!octokit) {
await this.as.botIntent.sendText(this.roomId, "You must login to assign an issue", "m.notice");
return;
throw new NotLoggedInError();
}
if (users.length === 1) {
@ -225,8 +225,7 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
public async onClose(userId: string, number: string, comment?: string) {
const octokit = await this.tokenStore.getOctokitForUser(userId);
if (!octokit) {
await this.as.botIntent.sendText(this.roomId, "You must login to close an issue", "m.notice");
return;
throw new NotLoggedInError();
}
if (comment) {
@ -250,8 +249,7 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
public async onWorkflowRun(userId: string, name: string, args?: string, ref?: string) {
const octokit = await this.tokenStore.getOctokitForUser(userId);
if (!octokit) {
await this.as.botIntent.sendText(this.roomId, "You must login to close an issue", "m.notice");
return;
throw new NotLoggedInError();
}
const workflowArgs: Record<string, string> = {};
if (args) {
@ -269,21 +267,32 @@ export class GitHubRepoConnection extends CommandConnection implements IConnecti
await this.as.botIntent.sendText(this.roomId, `Could not find a workflow by the name of "${name}". The workflows on this repository are ${workflowNames}`, "m.notice");
return;
}
if (!ref) {
ref = (await octokit.repos.get({
try {
if (!ref) {
ref = (await octokit.repos.get({
repo: this.state.repo,
owner: this.state.org,
})).data.default_branch;
}
} catch (ex) {
throw new CommandError(ex.message, `Could not determine default ref (maybe pass one in)`);
}
try {
await octokit.actions.createWorkflowDispatch({
repo: this.state.repo,
owner: this.state.org,
})).data.default_branch;
workflow_id: workflow.id,
ref,
inputs: workflowArgs,
});
} catch (ex) {
const httpError = ex as AxiosError;
if (httpError.response?.data) {
throw new CommandError(httpError.response?.data.message, ${httpError.response?.data.message});
}
throw ex;
}
await octokit.actions.createWorkflowDispatch({
repo: this.state.repo,
owner: this.state.org,
workflow_id: workflow.id,
ref,
inputs: workflowArgs,
});
await this.as.botIntent.sendText(this.roomId, `Workflow started`, "m.notice");
}