-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
155 lines (141 loc) · 3.9 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type {
App as A,
AppContext as AC,
AppMiddlewareContext as AMC,
ManifestOf,
} from "@deco/deco";
import type { Secret } from "apps/website/loaders/secret.ts";
import workflow from "apps/workflows/mod.ts";
import { type DrizzleContext, Octokit } from "./deps/deps.ts";
import { Bot, createBot } from "./deps/discordeno.ts";
import manifest, { type Manifest } from "./manifest.gen.ts";
import { GithubClient } from "./sdk/github/client.ts";
import type { ProjectUser } from "./types.ts";
export type App = ReturnType<typeof GithubBot>;
export type AppContext = AC<App> & DrizzleContext;
export type AppMiddlewareContext = AMC<App>;
export type AppManifest = ManifestOf<App>;
interface GithubProps {
/**
* @title Webhook Secret
* @description Secret create for the git hub webhook under https://github.com/{{organization}}/{{repo}}/settings/hooks
*/
webhook_secret?: Secret | null;
/**
* @title Organization Name
*/
org_name: string;
/**
* @title Repository Name
*/
repo_name: string;
}
interface DiscordProps {
/**
* @title Pull Request Channel ID
* @description Discord channel where the bot will send the recurrent messages about pull requests
*/
pr_channel_id: string;
/**
* @title Summary Channel ID
* @description Discord channel where the bot will send the recurrent messages about pull requests summary
*/
summary_channel_id: string;
}
interface DiscordApplicationsProps {
/**
* @title Public Key
* @description Public key provided by discord when you create your bot: https://discord.com/developers/applications/{{your_bot_id}}/information
*/
public_key: string;
/**
* @title App ID
* @description App ID provided by discord when you create your bot: https://discord.com/developers/applications/{{your_bot_id}}/information
*/
app_id: string;
/**
* @title Token
* @description Token provided by discord to identify your bot when this app is communicating with discord APIs: https://discord.com/developers/applications/{{your_bot_id}}/bot
*/
token: Secret;
}
/**
* @title {{github.org_name}}/{{github.repo_name}}
*/
export interface Project {
github: GithubProps;
discord: DiscordProps;
/**
* @description Users that are working on this project
*/
users: ProjectUser[];
/**
* @title Active
* @description If the project is active or not
* @default true
*/
active: boolean;
}
interface Props {
projects: Project[];
discord: DiscordApplicationsProps;
/**
* @title Github Token
* @description Octokit token necessary to retrieve github information from your repositories
*/
githubToken: Secret;
/**
* @title Cron Job Secret
* @description Secret to validate requests from cronjobs
*/
cronJobSecret: Secret;
/**
* @description Enable this to create a workflow to make people confirm they will review the pull request
* @default false
*/
confirmPullRequestReview?: boolean;
}
/**
* @title Github Automation
* @description App to send notifications about pull requests and issues to Discord
* @category Automation
*/
export default function GithubBot(props: Props) {
const { discord, projects, githubToken } = props;
if (!discord.token?.get() || !projects.length || !githubToken?.get()) {
return {
state: {
...props,
githubClient: {} as GithubClient,
discord: {
...discord,
bot: {} as Bot,
},
},
manifest,
};
}
const discordBot = createBot({
token: discord.token.get()!,
});
const githubClient = new GithubClient(
new Octokit({
auth: githubToken.get(),
}),
);
const state = {
...props,
githubClient,
discord: {
...discord,
bot: discordBot,
},
};
const app: A<Manifest, typeof state, [ReturnType<typeof workflow>]> = {
state,
manifest,
dependencies: [workflow({})],
};
return app;
}
export { Preview } from "./preview/Preview.tsx";