Skip to content

Commit dcc5110

Browse files
committed
first pass at creating example
1 parent 12753c5 commit dcc5110

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed

.github/dependabot/go.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This is the input to Dependabot CLI.
2+
# For more examples of what you can do, see the smoke tests: https://github.com/dependabot/smoke-tests/tree/main/tests
3+
4+
job:
5+
# this is the directory defined in dependabot-core
6+
package-manager: go_modules
7+
allowed-updates:
8+
- dependency-type: direct
9+
update-type: all
10+
experiments:
11+
# unlike with hosted Dependabot, you can control this variable directly
12+
goprivate: ""
13+
source:
14+
provider: github
15+
repo: dependabot/example-cli-usage
16+
# looks absolute, but this is relative to the root of the repo
17+
directory: /example
18+
branch: main

.github/workflows/example.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Run Dependabot
2+
on:
3+
workflow_dispatch:
4+
5+
jobs:
6+
run-dependabot:
7+
permissions:
8+
# Important not to give Dependabot write access in case it runs arbitrary
9+
# code as some ecosystems do.
10+
contents: read
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Download CLI
17+
run: |
18+
gh release download --repo dependabot/cli -p "*linux-amd64.tar.gz"
19+
tar xzvf *.tar.gz >/dev/null 2>&1
20+
./dependabot --version
21+
22+
- name: Run Dependabot
23+
env:
24+
LOCAL_GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25+
run: |
26+
./dependabot update -f .github/dependabot/go.yml --timeout 20m > result.jsonl
27+
28+
- name: Upload result
29+
uses: actions/upload-artifact@v4
30+
with:
31+
name: dependabot-result
32+
path: result.jsonl
33+
34+
create-prs:
35+
permissions:
36+
# This job creates PRs, so it needs write access.
37+
contents: write
38+
pull-requests: write
39+
runs-on: ubuntu-latest
40+
needs: run-dependabot
41+
steps:
42+
- name: Checkout code
43+
uses: actions/checkout@v4
44+
45+
- name: Download result
46+
uses: actions/download-artifact@v4
47+
with:
48+
name: dependabot-result
49+
50+
- name: Create PRs
51+
run: bash create.sh result.jsonl

create.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# example
2+
3+
This is an example Go project that will be updated using the example Dependabot CLI workflow.

example/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/dependabot/example-cli-usage/example
2+
3+
go 1.24.2
4+
5+
require (
6+
golang.org/x/net v0.32.0
7+
golang.org/x/text v0.21.0
8+
)

example/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
2+
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
3+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
4+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=

example/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package example
2+
3+
import (
4+
_ "golang.org/x/net/html"
5+
_ "golang.org/x/text"
6+
)
7+
8+
func main() {}

0 commit comments

Comments
 (0)