Skip to content

Commit

Permalink
suggester: treat null and undefined as the same in eq check
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Aug 18, 2022
1 parent a0d8f9e commit 54ebcd9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/lib/eq/eq.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {eq} from './eq'

describe('EQ', () => {
test('EQ', () => {
expect(eq(undefined, undefined)).toBeTruthy()
expect(eq(undefined, null)).toBeTruthy()
expect(eq(null, undefined)).toBeTruthy()
expect(eq(null, null)).toBeTruthy()
expect(eq(1, 1)).toBeTruthy()
expect(eq(1, 2)).toBeFalsy()
expect(eq(2, 1)).toBeFalsy()
expect(eq(1, '1')).toBeFalsy()
})
})
13 changes: 13 additions & 0 deletions src/lib/eq/eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eq is an equality check, but treats null and undefined as equal
export const eq = (
a: number | undefined | null | string,
b: number | undefined | null | string
): boolean => {
if (a === null) {
a = undefined
}
if (b === null) {
b = undefined
}
return a === b
}
1 change: 1 addition & 0 deletions src/lib/eq/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './eq'
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './octokit'
export * from './track'
export * from './github'
export * from './features'
export * from './eq'
13 changes: 6 additions & 7 deletions src/suggester/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {RequestError} from '@octokit/request-error'
import {track} from '../lib/track'
import {suggest} from '../lib/github'
import {get} from '../lib/jobs'
import {Octokit, required} from '../lib'
import {Octokit, required, eq} from '../lib'
import {ForbiddenError} from '../lib/api'

const jobID = required('codeball-job-id')
Expand Down Expand Up @@ -94,7 +94,7 @@ const suggestViaGitHub = async ({
pull_number: number
}) =>
get(jobID).then(async job => {
let suggestions = job?.comment?.suggestions
const suggestions = job?.comment?.suggestions
if (!suggestions) return
if (suggestions.length === 0) return

Expand Down Expand Up @@ -143,11 +143,10 @@ const suggestViaGitHub = async ({
}

const alreadyExists = existingComments.some(comment => {
const isSameBody = comment.body === request.body
// NOTE: == is intentional, we want to treat undefined and null as equal
const isSameStartLine = comment.start_line == request.start_line
const isSameEndLine = comment.line === request.line
const isSame = isSameBody && isSameStartLine && isSameEndLine
const isSameBody = eq(comment.body, request.body)
const isSameStartLineLine = eq(comment.start_line, request.start_line)
const isSameEndLine = eq(comment.line, request.line)
const isSame = isSameBody && isSameStartLineLine && isSameEndLine
return isSame
})

Expand Down

0 comments on commit 54ebcd9

Please sign in to comment.