Skip to content

Commit 81faff9

Browse files
authored
chore(bots/bugbuster): simplified recently linted logic (botpress#14648)
1 parent 1a0f09d commit 81faff9

File tree

8 files changed

+26
-60
lines changed

8 files changed

+26
-60
lines changed

bots/bugbuster/bot.definition.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ import telegram from './bp_modules/telegram'
77

88
export default new sdk.BotDefinition({
99
states: {
10-
recentlyLinted: {
11-
type: 'bot',
12-
schema: sdk.z.object({
13-
issues: sdk.z
14-
.array(
15-
sdk.z.object({
16-
id: sdk.z.string(),
17-
lintedAt: sdk.z.string().datetime(),
18-
})
19-
)
20-
.title('Recently Linted Issues')
21-
.describe('List of recently linted issues'),
22-
}),
23-
},
2410
watchedTeams: {
2511
type: 'bot',
2612
schema: sdk.z.object({

bots/bugbuster/src/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const bootstrap = (props: types.CommonHandlerProps) => {
1010

1111
const linear = utils.linear.LinearApi.create()
1212
const teamsManager = new TeamsManager(linear, client, ctx.botId)
13-
const recentlyLintedManager = new RecentlyLintedManager(client, ctx.botId)
13+
const recentlyLintedManager = new RecentlyLintedManager(linear)
1414
const issueProcessor = new IssueProcessor(logger, linear, teamsManager)
1515

1616
return {

bots/bugbuster/src/handlers/linear-issue-updated.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ export const handleLinearIssueUpdated: bp.EventHandlers['linear:issueUpdated'] =
1717
return
1818
}
1919

20-
const recentlyLinted = await recentlyLintedManager
21-
.getRecentlyLinted()
20+
const isRecentlyLinted = await recentlyLintedManager
21+
.isRecentlyLinted(issue)
2222
.catch(_handleError('trying to get recently linted issues'))
2323

24-
const isRecentlyLinted = recentlyLinted.some(({ id: issueId }) => issue.id === issueId)
25-
2624
await issueProcessor.lintIssue(issue, isRecentlyLinted).catch(_handleError('trying to lint the updated Linear issue'))
27-
await recentlyLintedManager
28-
.setRecentlyLinted([...recentlyLinted, { id: issue.id, lintedAt: new Date().toISOString() }])
29-
.catch(_handleError('trying to update recently linted issues'))
3025
}

bots/bugbuster/src/services/issue-processor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class IssueProcessor {
5656
return { identifier: issue.identifier, result: 'ignored' }
5757
}
5858

59-
const errors = await lintIssue(issue, status)
59+
const errors = lintIssue(issue, status)
6060

6161
if (errors.length === 0) {
6262
this._logger.info(`Issue ${issue.identifier} passed all lint checks.`)

bots/bugbuster/src/services/issue-processor/lint-issue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type IssueLint = {
55
message: string
66
}
77

8-
export const lintIssue = async (issue: lin.Issue, status: lin.StateKey): Promise<IssueLint[]> => {
8+
export const lintIssue = (issue: lin.Issue, status: lin.StateKey): IssueLint[] => {
99
const lints: string[] = []
1010

1111
if (!_hasLabelOfCategory(issue, 'type')) {
@@ -57,7 +57,7 @@ export const lintIssue = async (issue: lin.Issue, status: lin.StateKey): Promise
5757
)
5858
}
5959

60-
const issueProject = await issue.project
60+
const issueProject = issue.project
6161
if (issueProject && issueProject.completedAt) {
6262
lints.push(
6363
`Issue ${issue.identifier} is associated with a completed project (${issueProject.name}). Consider removing the project association.`
Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
import * as bp from '.botpress'
1+
import * as lin from '../utils/linear-utils'
22

33
const RECENT_THRESHOLD: number = 1000 * 60 * 10 // 10 minutes
4-
type IssueLintEntry = bp.states.recentlyLinted.RecentlyLinted['payload']['issues'][number]
54

65
export class RecentlyLintedManager {
7-
public constructor(
8-
private _client: bp.Client,
9-
private _botId: string
10-
) {}
6+
public constructor(private _linear: lin.LinearApi) {}
117

12-
public async getRecentlyLinted(): Promise<bp.states.recentlyLinted.RecentlyLinted['payload']['issues']> {
13-
const {
14-
state: {
15-
payload: { issues },
16-
},
17-
} = await this._client.getOrSetState({
18-
id: this._botId,
19-
type: 'bot',
20-
name: 'recentlyLinted',
21-
payload: { issues: [] },
22-
})
23-
return issues.filter(this._isRecentlyLinted)
24-
}
25-
26-
public async setRecentlyLinted(issues: bp.states.recentlyLinted.RecentlyLinted['payload']['issues']): Promise<void> {
27-
await this._client.setState({
28-
id: this._botId,
29-
type: 'bot',
30-
name: 'recentlyLinted',
31-
payload: {
32-
issues: issues.filter(this._isRecentlyLinted),
33-
},
34-
})
35-
}
36-
37-
private _isRecentlyLinted = (issue: IssueLintEntry): boolean => {
38-
const lintedAt = new Date(issue.lintedAt).getTime()
8+
public async isRecentlyLinted(issue: lin.Issue): Promise<boolean> {
9+
const me = await this._linear.getMe()
10+
const timestamps = issue.comments.nodes
11+
.filter((comment) => comment.user.id === me.id)
12+
.map((comment) => new Date(comment.createdAt).getTime())
3913
const now = new Date().getTime()
40-
return now - lintedAt < RECENT_THRESHOLD
14+
for (const timestamp of timestamps) {
15+
if (now - timestamp < RECENT_THRESHOLD) {
16+
return true
17+
}
18+
}
19+
return false
4120
}
4221
}

bots/bugbuster/src/utils/linear-utils/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export class LinearApi {
4040
}
4141

4242
public async getMe(): Promise<lin.User> {
43+
if (this._viewer) {
44+
return this._viewer
45+
}
4346
const me = await this._client.viewer
4447
if (!me) {
4548
throw new Error('Viewer not found. Please ensure you are authenticated.')

bots/bugbuster/src/utils/linear-utils/graphql-queries.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type Issue = {
4141
nodes: {
4242
id: string
4343
resolvedAt: string | null
44+
createdAt: string
4445
user: {
4546
id: string
4647
}
@@ -95,7 +96,9 @@ export const GRAPHQL_QUERIES = {
9596
user {
9697
id
9798
},
98-
parentId
99+
parentId,
100+
resolvedAt,
101+
createdAt
99102
}
100103
}
101104
}

0 commit comments

Comments
 (0)