#!/bin/bash -eu
# Copyright 2024 The Kubernetes 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.

if [ -z "$REPOSITORY_OWNER" ]; then
      echo "REPOSITORY_OWNER envvar must be set"
      exit 1
fi

./cve-feed-osv

if [[ -n $(git status --porcelain) ]]; then
  # List changed files
  CHANGED_FILES=$(git ls-files . --exclude-standard --others --modified | grep "vulns/CVE")
  REPO="$REPOSITORY_OWNER/cve-feed-osv"
  BASE_BRANCH="main"
  # Loop through changed files and create PRs
  for FILE in $CHANGED_FILES; do

    # Check if a PR with the same branch name already exists
    OPEN_PR_COUNT=$(gh pr list --state open --base $BASE_BRANCH --repo "$REPO" --limit 50 | grep "$FILE" | wc -l | xargs)

    if [ "$OPEN_PR_COUNT" != 0 ]; then
      echo "PR for $FILE already exists, skipping."
      continue
    fi

    BRANCH_NAME=$(echo "$FILE" | tr / -)
    PR_TITLE="Update $FILE"
    PR_BODY="This PR updates $FILE"

    # Create a new branch and push it
    git checkout -b "$BRANCH_NAME"
    echo "$FILE"
    git add "$FILE"
    git commit -m "Update $FILE"

    git push origin "$BRANCH_NAME" --force
    # Create a new pull request using gh
    gh pr create --base "$BASE_BRANCH" --head "$BRANCH_NAME" --title "$PR_TITLE" --body "$PR_BODY" --repo "$REPO"

    git checkout $BASE_BRANCH

    sleep 30
  done
fi
