From 0ffd26ff220e8d6dac5262103692e98695392797 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sat, 15 Jun 2024 00:36:08 +0200 Subject: [PATCH] github: annotate build warnings Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/test.yml | 2 +- __tests__/fixtures/lint.Dockerfile | 23 ++++++++++++++++++++++ __tests__/github.test.itg.ts | 31 ++++++++++++++++++++++++++++++ src/github.ts | 11 +++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/lint.Dockerfile diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c17796af..83cf21bf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ on: env: NODE_VERSION: "20" - BUILDX_VERSION: "v0.15.0" + BUILDX_VERSION: "https://github.com/crazy-max/buildx.git#metadata-warnings" jobs: test: diff --git a/__tests__/fixtures/lint.Dockerfile b/__tests__/fixtures/lint.Dockerfile new file mode 100644 index 00000000..40271b0f --- /dev/null +++ b/__tests__/fixtures/lint.Dockerfile @@ -0,0 +1,23 @@ +# syntax=docker/dockerfile-upstream:master + +# Copyright 2024 actions-toolkit authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +frOM busybox as base +cOpy lint.Dockerfile . + +from scratch +COPy --from=base \ + /lint.Dockerfile \ + / diff --git a/__tests__/github.test.itg.ts b/__tests__/github.test.itg.ts index 852e0a67..e9ea620b 100644 --- a/__tests__/github.test.itg.ts +++ b/__tests__/github.test.itg.ts @@ -194,3 +194,34 @@ maybe('writeBuildSummary', () => { }); }); }); + +maybe('annotateBuildWarnings', () => { + it('annoate lint issues', async () => { + const buildx = new Buildx(); + const build = new Build({buildx: buildx}); + + fs.mkdirSync(tmpDir, {recursive: true}); + await expect( + (async () => { + // prettier-ignore + const buildCmd = await buildx.getCommand([ + '--builder', process.env.CTN_BUILDER_NAME ?? 'default', + 'build', + '-f', path.join(fixturesDir, 'lint.Dockerfile'), + fixturesDir, + '--metadata-file', build.getMetadataFilePath() + ]); + await Exec.exec(buildCmd.command, buildCmd.args); + })() + ).resolves.not.toThrow(); + + const metadata = build.resolveMetadata(); + expect(metadata).toBeDefined(); + const buildRef = build.resolveRef(metadata); + expect(buildRef).toBeDefined(); + const buildStatus = build.resolveStatus(metadata); + expect(buildStatus).toBeDefined(); + + await GitHub.annotateBuildWarnings(path.join(fixturesDir, 'lint.Dockerfile'), buildStatus ?? {}); + }); +}); diff --git a/src/github.ts b/src/github.ts index 39bfdcfc..62b4a72f 100644 --- a/src/github.ts +++ b/src/github.ts @@ -35,6 +35,7 @@ import {jwtDecode, JwtPayload} from 'jwt-decode'; import {Util} from './util'; +import {SolveStatus} from './types/buildkit/client'; import {BuildSummaryOpts, GitHubActionsRuntimeToken, GitHubActionsRuntimeTokenAC, GitHubRepo, UploadArtifactOpts, UploadArtifactResponse} from './types/github'; export interface GitHubOpts { @@ -281,4 +282,14 @@ export class GitHub { core.info(`Writing summary`); await sum.addSeparator().write(); } + + public static async annotateBuildWarnings(source: string, st: SolveStatus): Promise { + st.warnings?.forEach(warning => { + core.warning(warning.detail?.map(encoded => atob(encoded)).join(' ') || 'n/a', { + title: atob(warning.short) || 'Build warning', + file: source, + startLine: warning.range && warning.range.length > 0 ? warning.range[0]?.start.line : undefined + }); + }); + } }