|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import * as github from '@actions/github'; |
| 3 | +import type { WebhookPayload } from '@actions/github/lib/interfaces'; |
| 4 | +import type { components } from '@octokit/openapi-types'; |
| 5 | +import { |
| 6 | + type Comment, |
| 7 | + type GitBranch, |
| 8 | + type Options, |
| 9 | + type ProviderAPIClient, |
| 10 | + type SourceFileIssue, |
| 11 | + runInCI, |
| 12 | +} from '@code-pushup/ci'; |
| 13 | +import { DEFAULT_PERSIST_CONFIG } from '@code-pushup/models'; |
| 14 | +import { |
| 15 | + CODE_PUSHUP_UNICODE_LOGO, |
| 16 | + logger, |
| 17 | + stringifyError, |
| 18 | +} from '@code-pushup/utils'; |
| 19 | + |
| 20 | +type GitHubRefs = { |
| 21 | + head: GitBranch; |
| 22 | + base?: GitBranch; |
| 23 | +}; |
| 24 | + |
| 25 | +type PullRequestPayload = NonNullable<WebhookPayload['pull_request']> & |
| 26 | + components['schemas']['pull-request-minimal']; |
| 27 | + |
| 28 | +const LOG_PREFIX = '[Code PushUp GitHub action]'; |
| 29 | + |
| 30 | +const MAX_COMMENT_CHARS = 65_536; |
| 31 | + |
| 32 | +function convertComment( |
| 33 | + comment: Pick<components['schemas']['issue-comment'], 'id' | 'body' | 'url'>, |
| 34 | +): Comment { |
| 35 | + const { id, body = '', url } = comment; |
| 36 | + return { id, body, url }; |
| 37 | +} |
| 38 | + |
| 39 | +function isPullRequest( |
| 40 | + payload: WebhookPayload['pull_request'], |
| 41 | +): payload is PullRequestPayload { |
| 42 | + return payload != null; |
| 43 | +} |
| 44 | + |
| 45 | +function parseBranchRef({ ref, sha }: GitBranch): GitBranch { |
| 46 | + return { |
| 47 | + ref: ref.split('/').at(-1) ?? ref, |
| 48 | + sha, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function parseGitRefs(): GitHubRefs { |
| 53 | + if (isPullRequest(github.context.payload.pull_request)) { |
| 54 | + const { head, base } = github.context.payload.pull_request; |
| 55 | + return { head: parseBranchRef(head), base: parseBranchRef(base) }; |
| 56 | + } |
| 57 | + return { head: parseBranchRef(github.context) }; |
| 58 | +} |
| 59 | + |
| 60 | +function createAnnotationsFromIssues(issues: SourceFileIssue[]): void { |
| 61 | + if (issues.length > 0) { |
| 62 | + core.info(`Creating annotations for ${issues.length} issues:`); |
| 63 | + } |
| 64 | + // eslint-disable-next-line functional/no-loop-statements |
| 65 | + for (const issue of issues) { |
| 66 | + const message = issue.message; |
| 67 | + const properties: core.AnnotationProperties = { |
| 68 | + title: `${CODE_PUSHUP_UNICODE_LOGO} ${issue.plugin.title} | ${issue.audit.title}`, |
| 69 | + file: issue.source.file, |
| 70 | + startLine: issue.source.position?.startLine, |
| 71 | + startColumn: issue.source.position?.startColumn, |
| 72 | + endLine: issue.source.position?.endLine, |
| 73 | + endColumn: issue.source.position?.endColumn, |
| 74 | + }; |
| 75 | + switch (issue.severity) { |
| 76 | + case 'error': |
| 77 | + core.error(message, properties); |
| 78 | + break; |
| 79 | + case 'warning': |
| 80 | + core.warning(message, properties); |
| 81 | + break; |
| 82 | + case 'info': |
| 83 | + core.notice(message, properties); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function createGitHubApiClient(): ProviderAPIClient { |
| 90 | + const token = process.env['GH_TOKEN']; |
| 91 | + |
| 92 | + if (!token) { |
| 93 | + throw new Error('No GitHub token found'); |
| 94 | + } |
| 95 | + |
| 96 | + const octokit = github.getOctokit(token); |
| 97 | + |
| 98 | + return { |
| 99 | + maxCommentChars: MAX_COMMENT_CHARS, |
| 100 | + |
| 101 | + listComments: async (): Promise<Comment[]> => { |
| 102 | + const comments = await octokit.paginate( |
| 103 | + octokit.rest.issues.listComments, |
| 104 | + { |
| 105 | + ...github.context.repo, |
| 106 | + issue_number: github.context.issue.number, |
| 107 | + }, |
| 108 | + ); |
| 109 | + return comments.map(convertComment); |
| 110 | + }, |
| 111 | + |
| 112 | + createComment: async (body: string): Promise<Comment> => { |
| 113 | + const { data } = await octokit.rest.issues.createComment({ |
| 114 | + ...github.context.repo, |
| 115 | + issue_number: github.context.issue.number, |
| 116 | + body, |
| 117 | + }); |
| 118 | + return convertComment(data); |
| 119 | + }, |
| 120 | + |
| 121 | + updateComment: async (id: number, body: string): Promise<Comment> => { |
| 122 | + const { data } = await octokit.rest.issues.updateComment({ |
| 123 | + ...github.context.repo, |
| 124 | + comment_id: id, |
| 125 | + body, |
| 126 | + }); |
| 127 | + return convertComment(data); |
| 128 | + }, |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +async function run(): Promise<void> { |
| 133 | + try { |
| 134 | + if (core.isDebug()) { |
| 135 | + logger.setVerbose(true); |
| 136 | + } |
| 137 | + |
| 138 | + const isMonorepo = process.env['MODE'] === 'monorepo'; |
| 139 | + |
| 140 | + const options: Options = isMonorepo |
| 141 | + ? { |
| 142 | + jobId: 'monorepo-mode', |
| 143 | + monorepo: 'nx', |
| 144 | + nxProjectsFilter: '--with-target=code-pushup --exclude=workspace', |
| 145 | + configPatterns: { |
| 146 | + persist: { |
| 147 | + ...DEFAULT_PERSIST_CONFIG, |
| 148 | + outputDir: '.code-pushup/{projectName}', |
| 149 | + }, |
| 150 | + ...(process.env['CP_API_KEY'] && { |
| 151 | + upload: { |
| 152 | + server: 'https://api.staging.code-pushup.dev/graphql', |
| 153 | + apiKey: process.env['CP_API_KEY'], |
| 154 | + organization: 'code-pushup', |
| 155 | + project: 'cli-{projectName}', |
| 156 | + }, |
| 157 | + }), |
| 158 | + }, |
| 159 | + } |
| 160 | + : { |
| 161 | + jobId: 'standalone-mode', |
| 162 | + bin: 'npx nx code-pushup --', |
| 163 | + }; |
| 164 | + |
| 165 | + const gitRefs = parseGitRefs(); |
| 166 | + |
| 167 | + const apiClient = createGitHubApiClient(); |
| 168 | + |
| 169 | + const result = await runInCI(gitRefs, apiClient, options); |
| 170 | + |
| 171 | + const issues = |
| 172 | + result.mode === 'standalone' |
| 173 | + ? (result.newIssues ?? []) |
| 174 | + : result.projects.flatMap(project => project.newIssues ?? []); |
| 175 | + |
| 176 | + if (issues.length > 0) { |
| 177 | + core.info( |
| 178 | + `Found ${issues.length} new issues, creating GitHub annotations`, |
| 179 | + ); |
| 180 | + createAnnotationsFromIssues(issues); |
| 181 | + } |
| 182 | + |
| 183 | + core.info(`${LOG_PREFIX} Finished running successfully`); |
| 184 | + } catch (error) { |
| 185 | + const message = stringifyError(error); |
| 186 | + core.error(`${LOG_PREFIX} Failed: ${message}`); |
| 187 | + core.setFailed(message); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +await run(); |
0 commit comments