Skip to content

Commit

Permalink
refactor: изменен алгоритм обновления замечаний
Browse files Browse the repository at this point in the history
  • Loading branch information
alkoleft committed Jun 4, 2024
1 parent 05b3867 commit 198e93d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 76 deletions.
71 changes: 37 additions & 34 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/github/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class GithubReview {
}

async createReviewComments(
body: string,
summary: string,
params: GitReviewParam[]
): Promise<Review | null> {
core.debug(`createReviewComments`)
Expand Down Expand Up @@ -82,18 +82,18 @@ export class GithubReview {
const response = await this.octokit.rest.pulls.createReview({
...commandParams,
event: 'COMMENT',
body: body,
body: summary,
comments: comments
})
return response.data
}

async updateReview(review_id: number, body: string) {
async updateReview(review_id: number, summary: string) {
await this.octokit.rest.pulls.updateReview({
...this.repo,
review_id: review_id,
pull_number: this.pull_number,
body: body,
body: summary,
commit_id: headSha()
})
}
Expand Down
6 changes: 2 additions & 4 deletions src/model/entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GitReviewParam } from 'src/github'
import * as entity from 'src/sonar/entity'

export interface Repo {
Expand All @@ -7,8 +8,5 @@ export interface Repo {

export interface Publisher {
generateReport(): Promise<boolean>
publishIssues(
quality: entity.Qualitygate,
issues: entity.Issue[]
): Promise<boolean>
publishIssues(summary: string, comments: GitReviewParam[]): Promise<boolean>
}
75 changes: 42 additions & 33 deletions src/publisher/review.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { summary } from '@actions/core'
import { GitReviewParam, GithubReview, Review, ReviewComment } from 'src/github'
import { Publisher } from 'src/model/entity'
import { Sonar } from 'src/sonar'
Expand All @@ -12,35 +13,47 @@ export class ReviewPublisher implements Publisher {
}

async generateReport(): Promise<boolean> {
const quality = await this.sonar.getQualityStatus()
if (!quality) {
return false
}
const reportData = await this.getReportData()

const issues = await this.sonar.allIssues()
if (!issues) {
if (reportData === undefined) {
return false
}

await this.publishIssues(quality, issues.issues)

return true
return await this.publishIssues(reportData.summary, reportData.comments)
}

async publishIssues(
quality: entity.Qualitygate,
issues: entity.Issue[]
summary: string,
comments: GitReviewParam[]
): Promise<boolean> {
const existsReview = await this.getExistsReview()

if (existsReview === undefined) {
await this.createNewReview(summary, comments)
} else {
await this.updateReview(existsReview, summary, comments)
}
return true
}

private async getReportData() {
const quality = await this.sonar.getQualityStatus()
if (!quality) {
return undefined
}

const issues = await this.sonar.allIssues()
if (!issues) {
return undefined
}
const comments: GitReviewParam[] = []
const stat = {
bug: 0,
vul: 0,
smell: 0
}
for (const i in issues) {
const issue = issues[i]
for (const i in issues.issues) {
const issue = issues.issues[i]
if (issue.type == 'BUG') {
stat.bug++
} else if (issue.type == 'VULNERABILITY') {
Expand All @@ -55,30 +68,31 @@ export class ReviewPublisher implements Publisher {
line: issue.line || issue.textRange.startLine
})
}
const comment = this.sonar.qualityGate.report(
const summary = this.sonar.qualityGate.report(
quality.projectStatus,
stat.bug,
stat.vul,
stat.smell
)

if (existsReview === undefined) {
await this.createNewReview(comment, comments)
} else {
await this.updateReview(comment, comments)
return {
summary,
comments
}
return true
}

private async createNewReview(comment: string, comments: GitReviewParam[]) {
await this.github.createReviewComments(comment, comments)
private async createNewReview(summary: string, comments: GitReviewParam[]) {
await this.github.createReviewComments(summary, comments)
}

private async updateReview(title: string, comments: GitReviewParam[]) {
private async updateReview(
review: Review,
summary: string,
comments: GitReviewParam[]
) {
const reviewComments = await this.github.getReviewComments()

const needDelete: number[] = []
const needUpdate: { comment_id: number; comment: GitReviewParam }[] = []
const needCreate: GitReviewParam[] = []

const commentsHash = new Map(comments.map(c => [c.key, c]))
Expand All @@ -97,13 +111,13 @@ export class ReviewPublisher implements Publisher {
if (comment === undefined) {
needDelete.push(reviewComment.id)
} else if (comment.comment != reviewComment.body) {
needUpdate.push({
comment_id: reviewComment.id,
comment: comment
})
needDelete.push(reviewComment.id)
needCreate.push(comment)
}
}

await this.github.updateReview(review.id, summary)

for (const i in comments) {
const comment = comments[i]
const reviewComment = reviewCommentsHash.get(comment.key)
Expand All @@ -113,12 +127,7 @@ export class ReviewPublisher implements Publisher {
}

if (needCreate && needCreate.length) {
await this.github.createReviewComments(title, needCreate)
}

for (const i in needUpdate) {
const record = needUpdate[i]
await this.github.updateReviewComment(record.comment_id, record.comment)
await this.github.createReviewComments('', needCreate)
}

for (const i in needDelete) {
Expand Down

0 comments on commit 198e93d

Please sign in to comment.