|
| 1 | +import { Annotations } from '@/annotations' |
| 2 | +import { ChecksCreateResponse, ChecksUpdateParamsOutputAnnotations } from '@octokit/rest' |
| 3 | +import { Context } from '@actions/github/lib/context' |
| 4 | +import github from '@actions/github' |
| 5 | + |
| 6 | +export class Github { |
| 7 | + private readonly client: github.GitHub |
| 8 | + private readonly context: Context |
| 9 | + private readonly owner: string |
| 10 | + private readonly repo: string |
| 11 | + private readonly sha: string |
| 12 | + private check: ChecksCreateResponse | null = null |
| 13 | + |
| 14 | + public constructor(token: string) { |
| 15 | + this.client = new github.GitHub({ |
| 16 | + auth: token, |
| 17 | + }) |
| 18 | + this.context = github.context |
| 19 | + this.owner = this.context.repo.owner |
| 20 | + this.repo = this.context.repo.repo |
| 21 | + this.sha = this.context.payload.pull_request?.head.sha ?? this.context.sha |
| 22 | + } |
| 23 | + |
| 24 | + private static githubAnnotationFromAnnotation( |
| 25 | + annotations: Annotations, |
| 26 | + ): ChecksUpdateParamsOutputAnnotations[] { |
| 27 | + return annotations.map(annotation => { |
| 28 | + const { level, message, path, column = {}, line = {} } = annotation |
| 29 | + |
| 30 | + const startLine = line?.start ?? 0 |
| 31 | + const endLine = line?.end ?? 0 |
| 32 | + |
| 33 | + const isSameLinePosition = startLine === endLine |
| 34 | + const startColumn = isSameLinePosition ? column?.start ?? line.end : undefined |
| 35 | + const endColumn = isSameLinePosition ? column?.end ?? line.start : undefined |
| 36 | + |
| 37 | + return { |
| 38 | + annotation_level: level, |
| 39 | + message, |
| 40 | + path, |
| 41 | + start_column: startColumn, |
| 42 | + end_column: endColumn, |
| 43 | + start_line: startLine, |
| 44 | + end_line: endLine, |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + public async createCheck(name: string): Promise<this> { |
| 50 | + const { owner, repo, sha } = this |
| 51 | + |
| 52 | + const response = await this.client.checks.create({ |
| 53 | + owner, |
| 54 | + repo, |
| 55 | + name, |
| 56 | + head_sha: sha, |
| 57 | + status: 'in_progress', |
| 58 | + }) |
| 59 | + |
| 60 | + this.check = response.data |
| 61 | + |
| 62 | + return this |
| 63 | + } |
| 64 | + |
| 65 | + public async updateCheckWithAnnotations(annotations: Annotations): Promise<void> { |
| 66 | + if (this.check === null) return |
| 67 | + |
| 68 | + const { owner, repo } = this |
| 69 | + const { name, id } = this.check |
| 70 | + |
| 71 | + await this.client.checks.update({ |
| 72 | + owner, |
| 73 | + repo, |
| 74 | + check_run_id: id, |
| 75 | + name, |
| 76 | + status: 'completed', |
| 77 | + conclusion: annotations.conclusion, |
| 78 | + output: { |
| 79 | + title: `${name}: output`, |
| 80 | + summary: `${annotations.length} annotations written`, |
| 81 | + text: ` |
| 82 | + - Failures: ${annotations.failures.length} |
| 83 | + - Warnings: ${annotations.warnings.length} |
| 84 | + - Notices: ${annotations.notices.length} |
| 85 | + `, |
| 86 | + annotations: Github.githubAnnotationFromAnnotation(annotations), |
| 87 | + }, |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments