-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (159 loc) · 5.41 KB
/
main.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
import path from 'node:path'
import chalk from 'chalk'
import { Command } from 'commander'
import { subDays, isAfter, formatDistance, max } from 'date-fns'
import { Octokit } from 'octokit'
import terminalLink from 'terminal-link'
const log = console.log
const program = new Command()
// Octokit is using the experimental node:fetch api so in order to not have
// warnings in the console we set this env variable
const { emit: originalEmit } = process
function suppresser(event, error) {
const isExperimentalWarning = event === 'warning' && error.name === 'ExperimentalWarning'
return !isExperimentalWarning && originalEmit.apply(process, arguments)
}
process.emit = suppresser
program
.name('github-pulse-overview')
.description('CLI to easily get an overview of the pulse of one or multiple repositories on GitHub')
.version('0.0.4')
.option('-t, --token <string>', 'The GitHub token to use for authentication, only needed for private repositories')
.option(
'-f, --file <string>',
'The file containing the repositories to get an overview of, must be JSON formatted, cannot be combined with -r',
)
.option(
'-r, --repos <items>',
'Comma separated list of repositories to get an overview of, e.g. westh/telemaster,octokit/octokit.js, cannot be combined with -f',
)
function validateOptions(options) {
const { file, repos } = options
const isBothFileAndRepositoriesSpecified = file && repos
const isNeitherFileNorRepositoriesSpecified = !file && !repos
const isFileAndReposiotiresSpecifiedJustRight =
!isBothFileAndRepositoriesSpecified && !isNeitherFileNorRepositoriesSpecified
if (isFileAndReposiotiresSpecifiedJustRight) return
if (isBothFileAndRepositoriesSpecified) {
log('Cannot specify both -f and -r, see --help')
process.exit(1)
}
if (isNeitherFileNorRepositoriesSpecified) {
log('Some repositories need to be specified, see --help')
process.exit(1)
}
}
function formatResposne(pullRequests) {
const nowish = new Date()
const oneWeekAgo = subDays(nowish, 7)
const pullRequestsThatAreOpen = []
const pullRequestsThatWereMerged = []
const pullRequestsThatWereClosed = []
pullRequests.forEach(
({
title,
created_at: createdAtString,
updated_at: updatedAtString,
merged_at: mergedAtString,
closed_at: closedAtString,
html_url: url,
number,
}) => {
const createdAt = new Date(createdAtString ?? nowish)
const updatedAt = new Date(updatedAtString ?? nowish)
const closedAt = new Date(closedAtString)
const mergedAt = new Date(mergedAtString)
const isCreatedOrUpdatedWithinTimeframe = isAfter(createdAt, oneWeekAgo) || isAfter(updatedAt, oneWeekAgo)
const isMergedWithinTimeframe = mergedAtString && isAfter(mergedAt, oneWeekAgo)
const isClosedWithinTimeframe = closedAtString && isAfter(closedAt, oneWeekAgo)
const isOpenedOrUpdatedWithinTimeframe =
isCreatedOrUpdatedWithinTimeframe && !isClosedWithinTimeframe && !isMergedWithinTimeframe
if (isOpenedOrUpdatedWithinTimeframe)
pullRequestsThatAreOpen.push({
title,
url,
number,
date: max([createdAt, updatedAt]),
})
if (isMergedWithinTimeframe) {
pullRequestsThatWereMerged.push({ title, url, number, date: mergedAt })
}
if (isClosedWithinTimeframe && !isMergedWithinTimeframe) {
pullRequestsThatWereClosed.push({ title, url, number, date: closedAt })
}
},
)
return [
{
wording: 'merged',
icon: '\ue727',
data: pullRequestsThatWereMerged,
},
{
wording: 'opened or updated',
icon: '\uf407',
data: pullRequestsThatAreOpen,
},
{
wording: 'closed',
icon: '\uf48e',
data: pullRequestsThatWereClosed,
},
]
}
async function main() {
program.parse()
const options = program.opts()
validateOptions(options)
const { file, repos, token } = options
const octokit = new Octokit({ auth: token })
const reposToCheck = file
? (
await import(path.resolve(process.cwd(), file), {
assert: {
type: 'json',
},
})
).default
: repos.split(',')
const reposData = await Promise.all(
reposToCheck.map(async repo => {
const [owner, name] = repo.split('/')
const { data } = await octokit.request('GET /repos/{owner}/{repo}/pulls', {
owner,
repo: name,
state: 'all',
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
})
return {
repo,
data,
}
}),
)
for (const { repo, data } of reposData) {
const foramttedResponse = formatResposne(data)
log(chalk.underline(repo))
if (foramttedResponse.every(({ data }) => !data.length)) {
log(` No changes proposed or made within the last week`)
const isLastRepo = repo === reposToCheck.at(-1)
if (!isLastRepo) log('')
continue
}
for (const { wording, icon, data } of foramttedResponse) {
if (data.length) {
log(` ${icon} ${chalk.bold(data.length)} Pull requests ${wording}`)
for (const { title, url, number, date } of data) {
const link = terminalLink(title, url)
log(` - ${link} ${chalk.dim(`(#${number} ${wording} ${formatDistance(date, new Date())} ago)`)}`)
}
}
}
const isLastRepo = repo === reposToCheck.at(-1)
if (!isLastRepo) log('')
}
}
main()