|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script takes a jsonl file as input which is the stdout of a Dependabot CLI run. |
| 4 | +# It takes the `type: create_pull_request` events and creates a pull request for each of them |
| 5 | +# by using git commands. |
| 6 | + |
| 7 | +# Note at this time there is minimal error handling. |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +if [ $# -ne 1 ]; then |
| 12 | + echo "Usage: $0 <result.jsonl>" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +INPUT="$1" |
| 17 | + |
| 18 | +# Parse each create_pull_request event |
| 19 | +jq -c 'select(.type == "create_pull_request")' "$INPUT" | while read -r event; do |
| 20 | + # Extract fields |
| 21 | + BASE_SHA=$(echo "$event" | jq -r '.expect.data."base-commit-sha"') |
| 22 | + PR_TITLE=$(echo "$event" | jq -r '.expect.data."pr-title"') |
| 23 | + PR_BODY=$(echo "$event" | jq -r '.expect.data."pr-body"') |
| 24 | + COMMIT_MSG=$(echo "$event" | jq -r '.expect.data."commit-message"') |
| 25 | + BRANCH_NAME="dependabot/$(echo "$PR_TITLE" | tr ' /' '__' | tr -cd '[:alnum:]_-')" |
| 26 | + |
| 27 | + echo "Processing PR: $PR_TITLE" |
| 28 | + echo " Base SHA: $BASE_SHA" |
| 29 | + echo " Branch: $BRANCH_NAME" |
| 30 | + |
| 31 | + # Create and checkout new branch from base commit |
| 32 | + git fetch origin |
| 33 | + git checkout "$BASE_SHA" |
| 34 | + git checkout -b "$BRANCH_NAME" |
| 35 | + |
| 36 | + # Apply file changes |
| 37 | + echo "$event" | jq -c '.expect.data."updated-dependency-files"[]' | while read -r file; do |
| 38 | + FILE_PATH=$(echo "$file" | jq -r '.directory + "/" + .name' | sed 's#^/##') |
| 39 | + DELETED=$(echo "$file" | jq -r '.deleted') |
| 40 | + if [ "$DELETED" = "true" ]; then |
| 41 | + git rm -f "$FILE_PATH" || true |
| 42 | + else |
| 43 | + mkdir -p "$(dirname "$FILE_PATH")" |
| 44 | + echo "$file" | jq -r '.content' > "$FILE_PATH" |
| 45 | + git add "$FILE_PATH" |
| 46 | + fi |
| 47 | + done |
| 48 | + |
| 49 | + # Commit and push |
| 50 | + git commit -m "$COMMIT_MSG" |
| 51 | + git push origin "$BRANCH_NAME" |
| 52 | + |
| 53 | + # Create PR using gh CLI |
| 54 | + gh pr create --title "$PR_TITLE" --body "$PR_BODY" --base main --head "$BRANCH_NAME" || true |
| 55 | + |
| 56 | + # Return to main branch for next PR |
| 57 | + git checkout main |
| 58 | +done |
0 commit comments