Skip to content

Commit

Permalink
ci: add a new workflow to update sync-param-files.yaml automatically (a…
Browse files Browse the repository at this point in the history
…utowarefoundation#75)

* feat add update-sync-param-files github action

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* debug

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* apply pre-commit

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* reflected comments

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* refactored python file

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* simplified python script

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* simplified python script

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* pre-commit applied

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* do not update when the source already exists

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* remove unnecessary import

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* not in

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* checkout@v1 -> checkout@v3

* fix comment

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* simplified python script

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* rename a variable

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* isolate src2dst calculation as a function

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* debugged

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* debugging

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* debugging

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* debugging

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* debugging

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* ci(pre-commit): autofix

* debugged?

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* updated python script with miyake-san's suggestion + minor fix

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* applied pre-commit

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* removed sorted function

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* resolve pre-commit

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

* added pip3 install GitPython action

Signed-off-by: kminoda <koji.m.minoda@gmail.com>

Signed-off-by: kminoda <koji.m.minoda@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
kminoda and pre-commit-ci[bot] authored Aug 23, 2022
1 parent f434f80 commit f945f95
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .github/update-sync-param-files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import argparse
import dataclasses
from pathlib import Path
from typing import List
from typing import Optional

import git

"""
This module updates `sync-param-files.yaml` based on the launch parameter files in `autoware.universe`.
"""

REPO_NAME = "autowarefoundation/autoware.universe"
REPO_URL = f"https://github.com/{REPO_NAME}.git"
CLONE_PATH = Path("/tmp/autoware.universe")


@dataclasses.dataclass
class FileSyncConfig:
source: str
dest: str
replace: Optional[bool] = None
delete_orphaned: Optional[bool] = None
pre_commands: Optional[str] = None
post_commands: Optional[str] = None


def create_tier4_launch_sync_configs(tier4_launch_package_path: Path) -> List[FileSyncConfig]:
launch_package_name = tier4_launch_package_path.name
launch_config_path = tier4_launch_package_path / "config"

sync_configs = []
for param_file_path in tier4_launch_package_path.glob("config/**/*.param.yaml"):
relative_param_file_path = param_file_path.relative_to(launch_config_path)

source = param_file_path.relative_to(CLONE_PATH)
dest = Path("autoware_launch/config") / launch_package_name / relative_param_file_path

sync_configs.append(FileSyncConfig(str(source), str(dest)))

return sync_configs


def dump_sync_config(section_name: str, sync_configs: List[FileSyncConfig]) -> List[str]:
indent = 4 * " "
lines = [f"{indent}# {section_name}\n"]
for sync_config in sync_configs:
lines.append(f"{indent}- source: {sync_config.source}\n")
lines.append(f"{indent} dest: {sync_config.dest}\n")
lines.append("\n")
return lines


def main():
parser = argparse.ArgumentParser()
parser.add_argument("sync_param_files_path", type=Path, help="path to sync-param-files.yaml")
args = parser.parse_args()

# Clone Autoware
if not CLONE_PATH.exists():
git.Repo.clone_from(REPO_URL, CLONE_PATH)

# Create sync config for tier4_*_launch
tier4_launch_package_paths = sorted(
CLONE_PATH.glob("launch/tier4_*_launch"), key=lambda p: p.name
)
tier4_launch_sync_configs_map = {
p.name: create_tier4_launch_sync_configs(p) for p in tier4_launch_package_paths
}

# Create sync-param-files.yaml
with open(args.sync_param_files_path, "w") as f:
f.write(f"- repository: {REPO_NAME}\n")
f.write(" files:\n")
for section_name, sync_config in tier4_launch_sync_configs_map.items():
f.writelines(dump_sync_config(section_name, sync_config))


if __name__ == "__main__":
main()
54 changes: 54 additions & 0 deletions .github/workflows/update-sync-param-files.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: update-sync-param-files

on:
schedule:
- cron: 0 0 * * *
workflow_dispatch:

jobs:
update-sync-param-files:
runs-on: ubuntu-latest
steps:
- name: Generate token
id: generate-token
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}

- name: Check out repository
uses: actions/checkout@v3

- name: Install GitPython
run: |
pip3 install GitPython
shell: bash

- name: Generate sync-param-files.yaml
run: |
python3 .github/update-sync-param-files.py .github/sync-param-files.yaml
- name: Create PR
id: create-pr
uses: peter-evans/create-pull-request@v4
with:
token: ${{ steps.generate-token.outputs.token }}
base: ${{ github.event.repository.default_branch }}
branch: update-sync-param-files
title: "chore: update sync-param-files.yaml"
commit-message: "chore: update sync-param-files.yaml"
body: ${{ steps.create-pr-body.outputs.body }}

- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.create-pr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.create-pr.outputs.pull-request-url }}"
shell: bash

- name: Enable auto-merge
if: ${{ steps.create-pr.outputs.pull-request-operation == 'created' }}
uses: peter-evans/enable-pull-request-automerge@v2
with:
token: ${{ steps.generate-token.outputs.token }}
pull-request-number: ${{ steps.create-pr.outputs.pull-request-number }}
merge-method: squash

0 comments on commit f945f95

Please sign in to comment.