Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from atomist-skills/atomist/eslint-master
Browse files Browse the repository at this point in the history
ESLint fixes
  • Loading branch information
cdupuis authored Jul 26, 2020
2 parents 052019c + a59eadc commit b4c4a58
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 132 deletions.
6 changes: 3 additions & 3 deletions lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

export interface PushToUnmappedRepoConfiguration {
prefix?: string;
invite: boolean;
ignore?: string[];
prefix?: string;
invite: boolean;
ignore?: string[];
}
200 changes: 112 additions & 88 deletions lib/events/PushToUnmappedRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PushToUnmappedRepoSubscription, PushToUnmappedRepoConfiguration> = 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<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 })));
}
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<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`,
};
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`,
};
};

/**
Expand All @@ -123,5 +145,7 @@ export const handler: EventHandler<PushToUnmappedRepoSubscription, PushToUnmappe
* @return valid Slack channel name based on repository name
*/
export function repoChannelName(repoName: string): string {
return repoName ? repoName.substring(0, 80).replace(/\./g, "_").toLowerCase() : repoName;
return repoName
? repoName.substring(0, 80).replace(/\./g, "_").toLowerCase()
: repoName;
}
89 changes: 48 additions & 41 deletions skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,59 @@
* limitations under the License.
*/

import { Category, parameter, ParameterType, resourceProvider, skill } from "@atomist/skill";
import {
Category,
parameter,
ParameterType,
resourceProvider,
skill,
} from "@atomist/skill";
import { PushToUnmappedRepoConfiguration } from "./lib/configuration";

export const Skill = skill<PushToUnmappedRepoConfiguration & { repos: any }>({
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"],
});

0 comments on commit b4c4a58

Please sign in to comment.