Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/jobs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Suggestion = {
commit_id: string
filename: string
from_line: number
to_line: number
text: string
}

Expand Down
40 changes: 28 additions & 12 deletions src/suggester/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {suggest} from '../lib/github'
import {get} from '../lib/jobs'
import {Octokit, required} from '../lib'
import {ForbiddenError} from '../lib/api'
import {exists} from 'fs'

const jobID = required('codeball-job-id')
const githubToken = required('GITHUB_TOKEN')
Expand Down Expand Up @@ -86,12 +87,6 @@ const suggestViaGitHub = async ({
})
.then(r => r.data)

// filter out already posted suggestions
suggestions = suggestions.filter(
suggestion =>
!existingComments.find(comment => comment.body === suggestion.text)
)

suggestions.forEach(suggestion => {
const request = {
owner,
Expand All @@ -113,22 +108,43 @@ const suggestViaGitHub = async ({
start_side?: 'LEFT' | 'RIGHT'
}

if (count(suggestion.text, '\n') > 1) {
const isSuggestionMultiline = suggestion.from_line > suggestion.to_line
if (isSuggestionMultiline) {
request.start_line = suggestion.from_line
request.start_side = 'RIGHT'
request.line = suggestion.from_line + count(suggestion.text, '\n')

request.line = suggestion.to_line
request.side = 'RIGHT'
} else {
request.line = suggestion.from_line
request.side = 'RIGHT'
}

const alreadyExists = existingComments.some(comment => {
const isSameBody = comment.body === request.body
const isSameStartLineLine = comment.start_line === request.start_line
const isSameeEndLine = comment.line === request.line
const isSame = isSameBody && isSameStartLineLine && isSameeEndLine
return isSame
})

if (alreadyExists) return

const inReplyTo = existingComments.find(comment => {
if (!comment.line) return false
const isSuggestionMultiline = suggestion.text.indexOf('\n') > -1
const isCommentMultiline = comment.body.indexOf('\n') > -1
if (!isSuggestionMultiline && !isCommentMultiline)
return comment.line === suggestion.from_line

const isSuggestionMultiline = suggestion.from_line > suggestion.to_line
const isGithubCommentMultiline = !!comment.start_line

if (!isSuggestionMultiline && !isGithubCommentMultiline)
return suggestion.from_line === comment.line

if (isGithubCommentMultiline && isGithubCommentMultiline)
return (
suggestion.from_line === comment.start_line &&
suggestion.to_line === comment.line
)

return false
})

Expand Down