# Copyright 2024 Google LLC
#
# 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.


# Stage 1: Build the Go linter binary
FROM golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 AS go_builder

RUN apk add --no-cache git
WORKDIR /src

# Using osv-schema v1.9.0
RUN git clone https://github.com/ossf/osv-schema.git osv-schema && cd osv-schema && git checkout 6410c13304f9bde44b522350b244f1f51e1c18a6

WORKDIR /src/osv-schema/tools/osv-linter

RUN go mod download

RUN go generate ./...

RUN CGO_ENABLED=0 GOOS=linux go build -o /usr/local/bin/osv-linter ./cmd/osv

# Build the worker binary
COPY --from=bindings . /src/osv.dev/bindings/
WORKDIR /src/osv.dev/go
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /usr/local/bin/osv-linter-worker ./cmd/osv-linter-worker

# Stage 2: Run worker
FROM gcr.io/oss-vdb/worker

# Copy the built Go binaries from the 'go_builder' stage
COPY --from=go_builder /usr/local/bin/osv-linter /usr/local/bin/osv-linter
COPY --from=go_builder /usr/local/bin/osv-linter-worker /usr/local/bin/osv-linter-worker

WORKDIR /linter

ENTRYPOINT ["osv-linter-worker"]
