From a59eadccdca49476a3681b2fa63d2ff19e77a535 Mon Sep 17 00:00:00 2001 From: Atomist Bot Date: Sun, 26 Jul 2020 13:33:00 +0000 Subject: [PATCH] ESLint fixes [atomist:generated] [atomist-skill:atomist/eslint-skill] --- lib/configuration.ts | 6 +- lib/events/PushToUnmappedRepo.ts | 200 +++++++++++++++++-------------- skill.ts | 89 +++++++------- 3 files changed, 163 insertions(+), 132 deletions(-) diff --git a/lib/configuration.ts b/lib/configuration.ts index e1eaf28..9ad35ae 100644 --- a/lib/configuration.ts +++ b/lib/configuration.ts @@ -15,7 +15,7 @@ */ export interface PushToUnmappedRepoConfiguration { - prefix?: string; - invite: boolean; - ignore?: string[]; + prefix?: string; + invite: boolean; + ignore?: string[]; } diff --git a/lib/events/PushToUnmappedRepo.ts b/lib/events/PushToUnmappedRepo.ts index bba7f8f..bf695e7 100644 --- a/lib/events/PushToUnmappedRepo.ts +++ b/lib/events/PushToUnmappedRepo.ts @@ -18,102 +18,124 @@ import { EventHandler } from "@atomist/skill"; import * as _ from "lodash"; import { PushToUnmappedRepoConfiguration } from "../configuration"; import { - AddBotToChannelMutation, - AddBotToChannelMutationVariables, - CreateChannelMutation, - CreateChannelMutationVariables, - InviteUserToChannelMutation, - InviteUserToChannelMutationVariables, - LinkChannelToRepoMutation, - LinkChannelToRepoMutationVariables, - PushToUnmappedRepoSubscription, + AddBotToChannelMutation, + AddBotToChannelMutationVariables, + CreateChannelMutation, + CreateChannelMutationVariables, + InviteUserToChannelMutation, + InviteUserToChannelMutationVariables, + LinkChannelToRepoMutation, + LinkChannelToRepoMutationVariables, + PushToUnmappedRepoSubscription, } from "../typings/types"; /** * Event handler to automatically create and map chat channels to repositories */ -export const handler: EventHandler = async ctx => { - const push = ctx.data.Push[0]; - const repo = push.repo; +export const handler: EventHandler< + PushToUnmappedRepoSubscription, + PushToUnmappedRepoConfiguration +> = async ctx => { + const push = ctx.data.Push[0]; + const repo = push.repo; - const teamId = repo?.org?.chatTeam?.id; - if (!teamId) { - return { - code: 0, - reason: `No ChatTeam linked to this workspace`, - }; - } + const teamId = repo?.org?.chatTeam?.id; + if (!teamId) { + return { + code: 0, + reason: `No ChatTeam linked to this workspace`, + }; + } - const channelIds: Array<{ id: string; name: string }> = []; - if (repo.channels?.length === 0) { - const name = repoChannelName( - ctx.configuration?.[0]?.parameters?.prefix - ? `${!!ctx.configuration?.[0]?.parameters?.prefix}-${repo.name}` - : repo.name, - ); + const channelIds: Array<{ id: string; name: string }> = []; + if (repo.channels?.length === 0) { + const name = repoChannelName( + ctx.configuration?.[0]?.parameters?.prefix + ? `${!!ctx.configuration?.[0]?.parameters?.prefix}-${repo.name}` + : repo.name, + ); - const channel = await ctx.graphql.mutate( - "createChannel.graphql", - { teamId, name }, - ); - await ctx.audit.log(`Created or updated channel '${name}'`); - channelIds.push({ id: channel?.createSlackChannel?.id, name }); - await ctx.graphql.mutate("addBotToChannel.graphql", { - teamId, - channelId: channelIds[0].id, - }); - await ctx.audit.log(`Invite @atomist bot to channel '${name}'`); - // Link repo to channel - await ctx.graphql.mutate( - "linkChannelToRepo.graphql", - { - teamId, - channelId: channelIds[0].id, - channelName: name, - repo: repo.name, - owner: repo.owner, - providerId: repo.org.provider.providerId, - }, - ); - await ctx.audit.log(`Linked repository '${repo.owner}/${repo.name}' to channel '${name}'`); - } else { - channelIds.push(...repo.channels.map(c => ({ id: c.channelId, name: c.name }))); - } + const channel = await ctx.graphql.mutate< + CreateChannelMutation, + CreateChannelMutationVariables + >("createChannel.graphql", { teamId, name }); + await ctx.audit.log(`Created or updated channel '${name}'`); + channelIds.push({ id: channel?.createSlackChannel?.id, name }); + await ctx.graphql.mutate< + AddBotToChannelMutation, + AddBotToChannelMutationVariables + >("addBotToChannel.graphql", { + teamId, + channelId: channelIds[0].id, + }); + await ctx.audit.log(`Invite @atomist bot to channel '${name}'`); + // Link repo to channel + await ctx.graphql.mutate< + LinkChannelToRepoMutation, + LinkChannelToRepoMutationVariables + >("linkChannelToRepo.graphql", { + teamId, + channelId: channelIds[0].id, + channelName: name, + repo: repo.name, + owner: repo.owner, + providerId: repo.org.provider.providerId, + }); + await ctx.audit.log( + `Linked repository '${repo.owner}/${repo.name}' to channel '${name}'`, + ); + } else { + channelIds.push( + ...repo.channels.map(c => ({ id: c.channelId, name: c.name })), + ); + } - for (const channelId of channelIds) { - // Invite committers - if (ctx.configuration?.[0]?.parameters?.invite) { - const ignore = ctx.configuration?.[0]?.parameters?.ignore || []; - const commits = push.commits.filter(c => { - if (!!c.committer?.login && ignore.includes(c.committer.login)) { - return false; - } - if ( - !!c.committer?.person?.chatId?.screenName && - ignore.includes(c.committer?.person?.chatId?.screenName) - ) { - return false; - } - return true; - }); - const userIds: Array<{ id: string; name: string }> = _.uniq( - commits - .filter(c => !!c.committer?.person?.chatId?.userId) - .map(c => ({ id: c.committer?.person?.chatId?.userId, name: c.committer?.login })), - ); - for (const userId of userIds) { - await ctx.graphql.mutate( - "inviteUserToChannel.graphql", - { teamId, channelId: channelId.id, userId: userId.id }, - ); - await ctx.audit.log(`Invited user '${userId.name}' to channel '${channelId.name}'`); - } - } - } - return { - code: 0, - reason: `Linked repository [${repo.owner}/${repo.name}](${repo.url}) to channel and invited committers`, - }; + for (const channelId of channelIds) { + // Invite committers + if (ctx.configuration?.[0]?.parameters?.invite) { + const ignore = ctx.configuration?.[0]?.parameters?.ignore || []; + const commits = push.commits.filter(c => { + if ( + !!c.committer?.login && + ignore.includes(c.committer.login) + ) { + return false; + } + if ( + !!c.committer?.person?.chatId?.screenName && + ignore.includes(c.committer?.person?.chatId?.screenName) + ) { + return false; + } + return true; + }); + const userIds: Array<{ id: string; name: string }> = _.uniq( + commits + .filter(c => !!c.committer?.person?.chatId?.userId) + .map(c => ({ + id: c.committer?.person?.chatId?.userId, + name: c.committer?.login, + })), + ); + for (const userId of userIds) { + await ctx.graphql.mutate< + InviteUserToChannelMutation, + InviteUserToChannelMutationVariables + >("inviteUserToChannel.graphql", { + teamId, + channelId: channelId.id, + userId: userId.id, + }); + await ctx.audit.log( + `Invited user '${userId.name}' to channel '${channelId.name}'`, + ); + } + } + } + return { + code: 0, + reason: `Linked repository [${repo.owner}/${repo.name}](${repo.url}) to channel and invited committers`, + }; }; /** @@ -123,5 +145,7 @@ export const handler: EventHandler({ - name: "auto-link-channel-skill", - namespace: "atomist", - displayName: "Auto-Link Chat Channels", - author: "Atomist", - categories: [Category.Chat], - license: "Apache-2.0", - homepageUrl: "https://github.com/atomist-skills/auto-link-channel-skill", - repositoryUrl: "https://github.com/atomist-skills/auto-link-channel-skill.git", - iconUrl: "file://docs/images/icon.svg", + name: "auto-link-channel-skill", + namespace: "atomist", + displayName: "Auto-Link Chat Channels", + author: "Atomist", + categories: [Category.Chat], + license: "Apache-2.0", + homepageUrl: "https://github.com/atomist-skills/auto-link-channel-skill", + repositoryUrl: + "https://github.com/atomist-skills/auto-link-channel-skill.git", + iconUrl: "file://docs/images/icon.svg", - runtime: { - memory: 512, - timeout: 60, - }, + runtime: { + memory: 512, + timeout: 60, + }, - resourceProviders: { - github: resourceProvider.gitHub({ minRequired: 1 }), - chat: resourceProvider.chat({ minRequired: 1 }), - }, + resourceProviders: { + github: resourceProvider.gitHub({ minRequired: 1 }), + chat: resourceProvider.chat({ minRequired: 1 }), + }, - parameters: { - invite: { - type: ParameterType.Boolean, - displayName: "Invite committers", - description: "Invite committers to linked channel", - required: true, - }, - ignore: { - type: ParameterType.StringArray, - displayName: "Ignore committers", - description: - "List committers who should not get invited to linked channels (can be chat user names or GitHub logins)", - required: false, - }, - prefix: { - type: ParameterType.String, - displayName: "Channel prefix", - description: "Prefix for newly created linked channels", - required: false, - }, - repos: parameter.repoFilter({ required: false }), - }, + parameters: { + invite: { + type: ParameterType.Boolean, + displayName: "Invite committers", + description: "Invite committers to linked channel", + required: true, + }, + ignore: { + type: ParameterType.StringArray, + displayName: "Ignore committers", + description: + "List committers who should not get invited to linked channels (can be chat user names or GitHub logins)", + required: false, + }, + prefix: { + type: ParameterType.String, + displayName: "Channel prefix", + description: "Prefix for newly created linked channels", + required: false, + }, + repos: parameter.repoFilter({ required: false }), + }, - subscriptions: ["file://graphql/subscription/*.graphql"], + subscriptions: ["file://graphql/subscription/*.graphql"], });