Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci-pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Pre-commit

on:
workflow_dispatch:
pull_request:

jobs:
pre-commit:
uses: ./.github/workflows/reusable-pre-commit.yml
with:
ros_distro: humble
75 changes: 75 additions & 0 deletions .github/workflows/reusable-pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Reusable pre-commit
# The pre-commit configuration is in .pre-commit-config.yaml
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
# UoE editor: Alejandro Bordallo <[email protected]>

on:
workflow_call:
inputs:
ros_distro:
description: 'ROS2 distribution name'
required: true
type: string

jobs:
pre-commit:
runs-on: ubuntu-latest
container: ros:${{ inputs.ros_distro }}
env:
# this will be src/{repo-owner}/{repo-name}
path: src/${{ github.repository }}
steps:
- name: "Determine prerequisites"
id: prereq
run: |
command -v sudo >/dev/null 2>&1 || (apt update && apt install -y sudo)
sudo apt update
echo "need_node=$(command -v node >/dev/null 2>&1 && echo 0 || echo 1)" >> $GITHUB_OUTPUT
echo "need_ros2=$(if [ -d "/opt/ros/${{ inputs.ros_distro }}" ]; then echo 0; else echo 1; fi)" \
>> $GITHUB_OUTPUT

# needed for github actions, and only if a bare ubuntu image is used
- uses: actions/setup-node@v4
if: ${{ steps.prereq.outputs.need_node == '1' && !env.ACT }}
- name: Install node
# Consider switching to https://github.com/actions/setup-node when it works
# https://github.com/nektos/act/issues/973
if: ${{ steps.prereq.outputs.need_node == '1' && env.ACT }}
run: |
sudo apt install -y curl
curl -sS https://webi.sh/node | sh
echo ~/.local/opt/node/bin >> $GITHUB_PATH

# needed only if a non-ros image is used
- uses: ros-tooling/[email protected]
if: ${{ steps.prereq.outputs.need_ros2 == '1' }}
with:
use-ros2-testing: true

- uses: actions/checkout@v4
with:
fetch-depth: 0
path: ${{ env.path }}
- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ inputs.ros_distro }}|${{ hashFiles( format('{0}/.pre-commit-config.yaml', env.path) ) }}
- name: Install pre-commit and system hooks
shell: bash
run: |
sudo apt-get install -qq \
ros-${{ inputs.ros_distro }}-ament-cppcheck \
ros-${{ inputs.ros_distro }}-ament-cpplint \
ros-${{ inputs.ros_distro }}-ament-lint-cmake \
ros-${{ inputs.ros_distro }}-ament-copyright \
python3-venv
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install pre-commit
- name: Run pre-commit
shell: bash
run: |
source .venv/bin/activate
source /opt/ros/${{ inputs.ros_distro }}/setup.bash
cd ${{ env.path }}
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual
111 changes: 111 additions & 0 deletions .github/workflows/reusable-update-pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Reusable Update pre-commit
# Update pre-commit config and create PR if changes are detected
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
# UoE editor: Alejandro Bordallo <[email protected]>

on:
workflow_call:
inputs:
ref_for_scheduled_build:
description: |
'Reference on which the repo should be checkout for scheduled build.
Usually is this name of a branch or a tag.'
default: ''
required: false
type: string
secrets:
precommit-pr-token:
description: 'PAT from GreatAlexander for PR auto-approval'
required: true

jobs:
auto_update_and_create_pr:
runs-on: ubuntu-latest
env:
# this will be src/{repo-owner}/{repo-name}
path: src/${{ github.repository }}

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
path: ${{ env.path }}
ref: ${{ github.event.inputs.ref_for_scheduled_build }}

- name: Install pre-commit
run: |
sudo apt-get install -qq python3-venv
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install pre-commit

- name: Auto-update with pre-commit
run: |
source .venv/bin/activate
cd ${{ env.path }}
pre-commit autoupdate || true # Ignoring errors

- name: Check for changes
id: git_status
run: |
cd ${{ env.path }}
git diff --quiet && echo "changed=false" >> $GITHUB_OUTPUT || echo "changed=true" >> $GITHUB_OUTPUT

- name: There are changes
id: git_diff
if: steps.git_status.outputs.changed == 'true'
run: |
cd ${{ env.path }}
{
echo 'PR_COMMIT_DIFF<<EOF'
git diff --unified=1
echo EOF
} >> "$GITHUB_ENV"
git diff --exit-code || true

- name: No changes!
if: steps.git_status.outputs.changed == 'false'
run: |
echo "No changes detected"

- name: Create Pull Request
id: cpr
if: steps.git_status.outputs.changed == 'true'
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: auto-update-${{ github.event.inputs.ref_for_scheduled_build }}
base: main
commit-message: |
Bump version of pre-commit hooks

```diff
${{ env.PR_COMMIT_DIFF }}
```
title: Bump version of pre-commit hooks
body: |
This pull request contains auto-updated files of the pre-commit config.

```diff
${{ env.PR_COMMIT_DIFF }}
```
delete-branch: true
draft: false
path: ${{ env.path }}

- name: Enable Pull Request Automerge
if: steps.cpr.outputs.pull-request-operation == 'created'
run: |
cd ${{ env.path }}
gh pr merge --squash --auto "${{ steps.cpr.outputs.pull-request-number }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Auto approve
if: steps.cpr.outputs.pull-request-operation == 'created'
run: |
cd ${{ env.path }}
gh pr review --approve "${{ steps.cpr.outputs.pull-request-number }}"
env:
GH_TOKEN: ${{ secrets.precommit-pr-token }}
15 changes: 15 additions & 0 deletions .github/workflows/update-pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Auto Update pre-commit
# Update pre-commit config and create PR if changes are detected
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
# UoE editor: Alejandro Bordallo <[email protected]>

on:
workflow_dispatch:
schedule:
- cron: '0 0 2 * *' # Runs at 00:00, on day 2 of the month

jobs:
auto_update_and_create_pr:
uses: ./.github/workflows/reusable-update-pre-commit.yml
secrets:
precommit-pr-token: ${{ secrets.PRECOMMIT_AUTOUPDATE_PR_TOKEN }}
18 changes: 13 additions & 5 deletions .mergify.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
pull_request_rules:
- name: automatic merge for pre-commit ci updates
- name: assign PR to its author
conditions:
- author=pre-commit-ci[bot]
- title=[pre-commit.ci] pre-commit autoupdate
- base = main
actions:
merge:
method: squash
assign:
add_users:
- "{{author}}"
- name: review request for all PRs
conditions:
- base = main
actions:
request_reviews:
users:
- GreatAlexander
- hect95