From 22a1e1d7e79dd8fee5d18765a1ff50ebdd41cdf4 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Wed, 18 Dec 2024 19:06:23 -0500 Subject: [PATCH 1/3] Add composite action to add vendordeps and make a PR --- .github/actions/add_vendordep/action.yml | 57 ++++++++++++++++++++++++ BUILD.bazel | 5 +++ add_vendordep.py | 57 ++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 .github/actions/add_vendordep/action.yml create mode 100644 add_vendordep.py diff --git a/.github/actions/add_vendordep/action.yml b/.github/actions/add_vendordep/action.yml new file mode 100644 index 0000000..d7a0fe0 --- /dev/null +++ b/.github/actions/add_vendordep/action.yml @@ -0,0 +1,57 @@ +name: 'Add Vendordep' +description: 'Adds a vendordep to the repository and creates a pull request the upstream repo' + +inputs: + token: + description: 'Build Buddy API token' + required: true + vendordep_file: + description: 'Path to the vendordep file to upload' + required: true + pr_title: + description: 'The contents used as the PR title when opening the pull request' + required: true + pr_branch: + description: 'The name of the branch to use when creating the pull request' + required: true + is_beta: + description: "If true, this will be added to the years beta files" + default: false + required: false + + +runs: + using: "composite" + steps: + - uses: actions/checkout@v4 + with: + repository: 'pjreiniger/vendor-json-repo' # TODO + fetch-depth: 0 # Fetch all history so we can use git diff + path: "vendor-json-repo" + ref: "add_vendordep_action" # TODO + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Add vendordep + shell: bash + run: python add_vendordep.py --vendordep_file=$VENDORDEP_FILE --is_beta=$IS_BETA + working-directory: vendor-json-repo + env: + VENDORDEP_FILE: ${{ inputs.vendordep_file }} + IS_BETA: ${{ inputs.is_beta }} + + - name: Debug + shell: bash + run: git --no-pager diff + working-directory: vendor-json-repo + + - name: Create Gentool Pull Request + uses: peter-evans/create-pull-request@v6 + with: + path: vendor-json-repo + base: main + branch: ${{ inputs.pr_branch }} + token: ${{ inputs.token }} + title: ${{ inputs.pr_title }} \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel index 8387982..39b6871 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -27,6 +27,11 @@ py_binary( visibility = ["//visibility:public"], ) +py_binary( + name = "add_vendordep", + srcs = ["add_vendordep.py"], +) + # Change this for local testing only. cache_directory = None diff --git a/add_vendordep.py b/add_vendordep.py new file mode 100644 index 0000000..8df2366 --- /dev/null +++ b/add_vendordep.py @@ -0,0 +1,57 @@ +import json +import argparse +from pathlib import Path +import shutil + + +def add_vendordep(vendordep_filename, is_beta): + vendordep_contents = json.loads(vendordep_filename.read_bytes()) + year = vendordep_contents['frcYear'] + "beta" if is_beta else "" + + year_filename = Path(f"{year}.json") + year_contents = json.loads(year_filename.read_bytes()) + + metadata_filename = Path(f"{year}_metadata.json") + metadata_contents = json.loads(metadata_filename.read_bytes()) + + for metadata_lib in metadata_contents: + if metadata_lib["uuid"] == vendordep_contents["uuid"]: + break + else: + raise Exception("This appears to be a new library that does not have metadata associated with it. Can not automatically update") + + vendordep_destination = Path(f"{year}/{vendordep_filename.name}") + + year_contents.append(dict( + path=str(vendordep_destination), + name=metadata_lib["name"], + version=vendordep_contents["version"], + uuid=metadata_lib["uuid"], + description=metadata_lib["description"], + website=metadata_lib["website"], + )) + + year_filename.write_text(json.dumps(year_contents, indent=2)) + shutil.copy(vendordep_filename, vendordep_destination) + +def main(): + parser = argparse.ArgumentParser( + "Generates one or more vendordep repository bundles for publication" + ) + parser.add_argument( + "--vendordep_file", + type=Path + ) + parser.add_argument( + "--is_beta", + type=bool + ) + args = parser.parse_args() + + add_vendordep(args.vendordep_file, args.is_beta) + + + + +if __name__ == "__main__": + main() \ No newline at end of file From d2efa09522429f9c40ddc070d43facef6dbf108d Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Wed, 18 Dec 2024 19:11:30 -0500 Subject: [PATCH 2/3] Add docs on action --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 01b4f14..6f041aa 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,28 @@ Pyunit tests are automatically auto generated run using the checker tool against ### Running the tests To run the tests, simply run `bazel test //...`. Alternatively, you can run the `checker.py` tool in a standalone mode by running `bazel run //:checker -- ` + +## Automatically creating pull requests +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. + +Here is an example workflow: + +``` +jobs: + hello_world_job: + runs-on: ubuntu-latest + name: A job to say hello + steps: + - uses: actions/checkout@v4 + + # 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 + + - id: create_pull_request + uses: wpilibsuite/vendor-json-repo/.github/actions/add_vendordep@latest + with: + token: ${{ secrets.PUBLISH_VENDOR_JSON_TOKEN }} + vendordep_file: + pr_title: "Automatically add version " + pr_branch: "publish__" + is_beta: true +``` \ No newline at end of file From a54c8d5b13b30aab68f96ebbf5fe12a642e3120d Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Wed, 18 Dec 2024 19:15:23 -0500 Subject: [PATCH 3/3] Don't use fork --- .github/actions/add_vendordep/action.yml | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/add_vendordep/action.yml b/.github/actions/add_vendordep/action.yml index d7a0fe0..78d114c 100644 --- a/.github/actions/add_vendordep/action.yml +++ b/.github/actions/add_vendordep/action.yml @@ -25,10 +25,10 @@ runs: steps: - uses: actions/checkout@v4 with: - repository: 'pjreiniger/vendor-json-repo' # TODO + repository: 'wpilibsuite/vendor-json-repo' fetch-depth: 0 # Fetch all history so we can use git diff path: "vendor-json-repo" - ref: "add_vendordep_action" # TODO + ref: "main" - uses: actions/setup-python@v5 with: diff --git a/README.md b/README.md index 6f041aa..297aafa 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Pyunit tests are automatically auto generated run using the checker tool against To run the tests, simply run `bazel test //...`. Alternatively, you can run the `checker.py` tool in a standalone mode by running `bazel run //:checker -- ` ## Automatically creating pull requests -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. +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. Here is an example workflow: