Skip to content

Commit 93a0bc6

Browse files
authored
Add composite action to add vendordeps and make a PR (#143)
1 parent 7ab8008 commit 93a0bc6

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: "Add Vendordep"
2+
description: "Adds a vendordep to the repository and creates a pull request the upstream repo"
3+
4+
inputs:
5+
token:
6+
description: "GitHub API token"
7+
required: true
8+
repo:
9+
description: "Name of the repo to create branches on"
10+
required: true
11+
vendordep_file:
12+
description: "Absolute path to the vendordep file to upload"
13+
required: true
14+
pr_title:
15+
description: "The contents used as the PR title when opening the pull request"
16+
required: true
17+
pr_branch:
18+
description: "The name of the branch to use when creating the pull request"
19+
required: true
20+
21+
runs:
22+
using: "composite"
23+
steps:
24+
- uses: actions/checkout@v4
25+
with:
26+
repository: wpilibsuite/vendor-json-repo
27+
fetch-depth: 0 # Fetch all history so we can use git diff
28+
path: vendor-json-repo
29+
ref: main
30+
31+
- uses: actions/setup-python@v5
32+
with:
33+
python-version: "3.12"
34+
35+
- name: Add vendordep
36+
shell: bash
37+
run: python add_vendordep.py --vendordep_file=${{ inputs.vendordep_file }}
38+
working-directory: vendor-json-repo
39+
40+
- name: Debug
41+
shell: bash
42+
run: git --no-pager diff
43+
working-directory: vendor-json-repo
44+
45+
- name: Create Pull Request
46+
uses: peter-evans/create-pull-request@v6
47+
with:
48+
push-to-fork: ${{ inputs.repo }}
49+
path: vendor-json-repo
50+
base: main
51+
branch: ${{ inputs.pr_branch }}
52+
token: ${{ inputs.token }}
53+
title: ${{ inputs.pr_title }}

BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ py_binary(
2121
],
2222
)
2323

24+
py_binary(
25+
name = "add_vendordep",
26+
srcs = ["add_vendordep.py"],
27+
)
28+
2429
# Change this for local testing only.
2530
cache_directory = None
2631

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,28 @@ To create a new bundle and add it to the CI job to be checked, generated, and pu
9494
* In `.github/workflows/generate_bundles.yml`, add the new bundle name to the arguments for `generate_bundles.py`
9595
* In `.github/workflows/main.yml`, change the `YEAR` environment variable to the name of the new bundle (note: only one bundle is checked by this workflow currently)
9696
* Add a new test configuration to `BUILD.bazel`
97+
98+
## Automatically creating pull requests
99+
If your libraries CI creates a new vendordep.json file, you can use an action contained in this repository to automatically create a pull request to add your changes. In order for the action to work, you must define a secret with write access to be able to create the pull request.
100+
101+
Here is an example workflow:
102+
103+
```yml
104+
jobs:
105+
hello_world_job:
106+
runs-on: ubuntu-latest
107+
name: A job to say hello
108+
steps:
109+
- uses: actions/checkout@v4
110+
111+
# Steps to package your vendordep file. It is recommended that you store the new version number in a variable so that it can be used later when creating your PR's title and branch name
112+
113+
- name: Create Vendor JSON Repo PR
114+
uses: wpilibsuite/vendor-json-repo/.github/actions/add_vendordep@latest
115+
with:
116+
repo: <GH account>/<vendor-json-repo fork name>
117+
token: ${{ secrets.PUBLISH_VENDOR_JSON_TOKEN }}
118+
vendordep_file: <path to vendordep file>
119+
pr_title: "Automatically add <library name> version <version>"
120+
pr_branch: "publish_<library name>_<version>"
121+
```

add_vendordep.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import json
2+
import argparse
3+
from pathlib import Path
4+
import shutil
5+
6+
7+
def add_vendordep(vendordep_filename):
8+
vendordep_contents = json.loads(vendordep_filename.read_bytes())
9+
year = vendordep_contents["frcYear"]
10+
11+
metadata_filename = Path(f"{year}_metadata.json")
12+
metadata_contents = json.loads(metadata_filename.read_bytes())
13+
14+
for metadata_lib in metadata_contents:
15+
if metadata_lib["uuid"] == vendordep_contents["uuid"]:
16+
break
17+
else:
18+
raise Exception(
19+
"This appears to be a new library that does not have metadata associated with it. Can not automatically update"
20+
)
21+
22+
vendordep_destination = Path(f"{year}/{vendordep_filename.name}")
23+
shutil.copy(vendordep_filename, vendordep_destination)
24+
25+
26+
def main():
27+
parser = argparse.ArgumentParser(
28+
"Generates one or more vendordep repository bundles for publication"
29+
)
30+
parser.add_argument("--vendordep_file", type=Path)
31+
args = parser.parse_args()
32+
33+
add_vendordep(args.vendordep_file)
34+
35+
36+
if __name__ == "__main__":
37+
main()

0 commit comments

Comments
 (0)