Skip to content

Commit 30a412c

Browse files
committed
feat: support multi org #27
1 parent f1cad93 commit 30a412c

File tree

15 files changed

+454
-316
lines changed

15 files changed

+454
-316
lines changed

action.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ inputs:
2727
- chat:write.customize (to allow the bot to customize the name and avatar)
2828
channels:
2929
required: true
30-
description: Comma separated list of Slack channel_ids to post to.
30+
description: Comma-separated list of Slack channel IDs to post to.
3131
with-test-data:
3232
description: Append some test data to the Slack post.
3333
required: false
@@ -52,7 +52,7 @@ inputs:
5252
default: true
5353
repository-filter:
5454
required: false
55-
description: Comma separated list of repositories to scan.
55+
description: Comma-separated list of repositories to scan.
5656
base-url:
5757
description: Point to different github instance such as https://api.github.com
5858
required: false
@@ -62,4 +62,4 @@ outputs:
6262

6363
runs:
6464
using: node20
65-
main: dist/index.js
65+
main: dist/app.js

dist/index.js

Lines changed: 367 additions & 246 deletions
Large diffs are not rendered by default.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@krauters/github-notifier",
33
"description": "GitHub Notifier by Krauters – Post Open Pull Requests to Slack",
4-
"version": "0.14.1",
4+
"version": "0.15.0",
55
"author": "Colten Krauter <coltenkrauter>",
66
"type": "module",
77
"homepage": "https://buymeacoffee.com/coltenkrauter",
@@ -17,15 +17,15 @@
1717
"typescript"
1818
],
1919
"exports": {
20-
".": "./dist/index.js"
20+
".": "./dist/app.js"
2121
},
2222
"engines": {
2323
"node": ">=20"
2424
},
2525
"scripts": {
2626
"all": "npm run test && npm run package",
2727
"bundle:watch": "npm run bundle -- --watch",
28-
"bundle": "npx ncc build src/index.ts -o dist --source-map",
28+
"bundle": "npx ncc build src/app.ts -o dist --source-map",
2929
"fix": "npm run lint -- --fix",
3030
"lint": "npx eslint src/**",
3131
"prepare": "husky || true",

src/app.ts

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,75 @@
33
import type { KnownBlock } from '@slack/web-api'
44

55
import { context } from '@actions/github'
6+
import { debug } from '@actions/core'
67
import { formatStringList, plural, snapDate, SnapType } from '@krauters/utils'
78

8-
import type { RunProps } from './structures.js'
9+
import type { InputProps } from './structures.js'
910

1011
import pkg from '../package.json' with { type: 'json' }
11-
import { workflowLogsUrl, workflowUrl } from './defaults.js'
12+
import { workflowLogsUrl, workflowUrl } from './constants.js'
1213
import { GitHubClient } from './utils/github/github-client.js'
13-
import { PullState, RepositoryType } from './utils/github/structures.js'
14+
import { Pull, PullState, RepositoryType } from './utils/github/structures.js'
1415
import { getFirstBlocks, getLastBlocks, getPullBlocks } from './utils/slack/blocks.js'
1516
import { SlackClient } from './utils/slack/slack-client.js'
1617
import { getApprovedPullRequest } from './utils/test-data.js'
18+
import { parseInputs } from './input-parser.js'
1719

1820
const { homepage, name, version } = pkg
1921

22+
/**
23+
* The main function that gets executed when the action is run.
24+
*/
25+
async function main(): Promise<void> {
26+
try {
27+
debug('Starting main...')
28+
await run(parseInputs())
29+
} catch (err) {
30+
console.error('Fatal error:', err)
31+
process.exit(1)
32+
}
33+
}
34+
2035
/**
2136
* Runs the GitHub Notifier to query GitHub for open pull requests and then post messages to Slack channels.
2237
*
2338
* @param props Configurable properties of the GitHub Notifier.
2439
*/
25-
export async function run({
26-
githubProps,
40+
async function run({
41+
githubConfig,
2742
repositoryFilter,
28-
slackProps,
43+
slackConfig,
2944
withArchived,
3045
withDrafts,
3146
withPublic,
3247
withPullReport,
3348
withTestData,
3449
withUserMentions,
35-
}: RunProps): Promise<void> {
36-
const slack = new SlackClient(slackProps)
37-
const gh = new GitHubClient(githubProps)
50+
}: InputProps): Promise<void> {
51+
const slack = new SlackClient(slackConfig)
52+
const results = await Promise.all(githubConfig.tokens.map(async (token) => {
53+
const client = new GitHubClient({
54+
options: githubConfig.options,
55+
token,
56+
})
57+
58+
const repositories = await client.getRepositories({
59+
repositoryFilter,
60+
type: RepositoryType.All,
61+
withArchived,
62+
withPublic,
63+
})
64+
65+
const openPulls = await client.getPulls({ repositories, state: PullState.Open, withDrafts })
66+
67+
return { client, org: client.getOrg(), openPulls}
68+
}))
69+
70+
console.log(results, 'results')
3871

3972
await slack.enforceAppNamePattern(/.*github[\s-_]?notifier$/i)
4073

41-
const repositories = await gh.getRepositories({
42-
repositoryFilter,
43-
type: RepositoryType.All,
44-
withArchived,
45-
withPublic,
46-
})
47-
const openPulls = await gh.getPulls({ repositories, state: PullState.Open, withDrafts })
74+
const openPulls: Pull[] = []
4875
let blocks: KnownBlock[] = []
4976
for (const openPull of openPulls) {
5077
console.log(`Building Slack blocks from pull request [${openPull.number}]`)
@@ -74,7 +101,7 @@ export async function run({
74101
text = `_<${workflowUrl}|Repository filter>: ${formatStringList(repositoryFilter)}_`
75102
}
76103

77-
blocks = [...getFirstBlocks(gh.cacheOrganization.name, header, text), ...blocks]
104+
blocks = [...getFirstBlocks('gh.cacheOrganization.name', header, text), ...blocks]
78105

79106
blocks = [
80107
...blocks,
@@ -87,18 +114,7 @@ export async function run({
87114
]
88115

89116
await slack.postMessage(header, blocks)
90-
91-
if (withPullReport) {
92-
const pulls = await gh.getPulls({
93-
oldest: snapDate(new Date(), { months: -12, snap: SnapType.Month }),
94-
onlyGhReviews: true,
95-
repositories,
96-
state: PullState.All,
97-
withCommits: false,
98-
withDrafts: false,
99-
withFilesAndChanges: false,
100-
withUser: false,
101-
})
102-
console.log(gh.getPullReport(pulls).reportString)
103-
}
104117
}
118+
119+
// This is required for the GitHub Action to execute main() when it invokes app.ts as specified in action.yaml
120+
await main()
File renamed without changes.

src/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { debug, getBooleanInput, getInput } from '@actions/core'
22
import { stringToArray } from '@krauters/utils'
33

4-
import { run } from './app.js'
4+
import type { InputProps } from './structures.js'
55

66
/**
7-
* The main function that gets executed when the action is run.
7+
* Parses and validates all inputs required for the GitHub Notifier.
8+
*
9+
* @returns The parsed and validated inputs for the GitHub Notifier.
810
*/
9-
export async function main(): Promise<void> {
10-
debug('Starting main...')
11+
export function parseInputs(): InputProps {
1112
debug('Parsing inputs...')
12-
const ghToken = getInput('github-token', { required: true })
13+
const githubTokens = stringToArray(getInput('github-tokens', { required: true }))
1314
const channels = stringToArray(getInput('channels', { required: true }))
1415
const slackToken = getInput('slack-token', { required: true })
1516
const withTestData = getBooleanInput('with-test-data')
@@ -23,15 +24,15 @@ export async function main(): Promise<void> {
2324
// https://github.com/actions/github-script/issues/436
2425
const baseUrl = getInput('base-url') || process.env.GITHUB_API_URL
2526

26-
await run({
27-
githubProps: {
27+
return {
28+
githubConfig: {
2829
options: {
2930
baseUrl,
3031
},
31-
token: ghToken,
32+
tokens: githubTokens,
3233
},
3334
repositoryFilter,
34-
slackProps: {
35+
slackConfig: {
3536
channels,
3637
token: slackToken,
3738
},
@@ -41,5 +42,5 @@ export async function main(): Promise<void> {
4142
withPullReport,
4243
withTestData,
4344
withUserMentions,
44-
})
45-
}
45+
}
46+
}

src/structures.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type {
22
ConfigurableGetPullsProps,
33
ConfigurableGetRepositoriesProps,
4-
GitHubClientProps,
4+
GitHubConfig,
55
} from './utils/github/structures.js'
6-
import type { SlackClientProps } from './utils/slack/structures.js'
6+
import type { SlackConfig } from './utils/slack/structures.js'
77

8-
export interface RunProps extends ConfigurableGetPullsProps, ConfigurableGetRepositoriesProps {
9-
githubProps: GitHubClientProps
10-
slackProps: SlackClientProps
8+
export interface InputProps extends ConfigurableGetPullsProps, ConfigurableGetRepositoriesProps {
9+
githubConfig: GitHubConfig
10+
slackConfig: SlackConfig
1111
withPullReport: boolean
1212
withTestData: boolean
1313
withUserMentions: boolean

0 commit comments

Comments
 (0)