Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement workspace internal dependency updater #262

Merged
merged 4 commits into from
Sep 12, 2023
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
20 changes: 20 additions & 0 deletions .github/actions/update-workspace-dependencies/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Update Workspace dependencies
description: Updates internal dependencies in a Go Workspace

inputs:
module:
description: The go module to update the dependencies of
required: true

runs:
using: "composite"
steps:
- name: Install dependencies
shell: bash
run: |
sudo DEBIAN_FRONTEND=noninteractive apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y jq
- name: Update workspace dependencies
shell: bash
run: ${{ github.action_path }}/update-workspace-dependencies.sh "${{ github.repository }}" "wsl-pro-service"

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
set -eu

github_repo="${1:-""}"
target_module="${2:-""}"

if [[ -z "${github_repo}" || -z "${target_module}" ]] ; then
echo "Update the out-of-date workspace dependencies of the target modules"
echo "This script must be run from the workspace root."
echo "Usage:"
echo " $(basename $0) github_repo target_module"
echo ""
echo "github_repo:"
echo " The 'user/repository' of the workspace"
echo ""
echo "target_module:"
echo " The path of the module relative to the workspace root"
exit 2
fi

cd ${target_module}
current_commit=$(git rev-parse HEAD~0)

# Find internal dependencies
repo="github.com/${github_repo}"
url="${repo}/${target_module}"
regex="s#${url} \(${repo}/[^@]\+@v.*\)#\1#p"
dependencies=`go mod graph | sed -n "${regex}"`

for dependency in ${dependencies}; do
pattern="^${repo}/\(.*\)@v\([^-]\+\)-\([^-]\+\)-\(.*\)$"
# Capture groups: (https://go.dev/ref/mod#versions)
# \1 Dependency path
# \2 Base version prefix
# \3 Timestamp
# \4 Revision identifier (12-character prefix of the commit hash)

dep_path=`echo $dependency | sed "s#${pattern}#\1#"`
dep_commit=`echo $dependency | sed "s#${pattern}#\4#"`

diff_files="$(git diff --name-only ${dep_commit} -- "../${dep_path}")"
if [ -z "${diff_files}" ] ; then
continue
fi

echo "::group::Updating dependency ${dep_path}::"

echo "Files changed since commit ${dep_commit}:"
echo "${diff_files}"

go get "${repo}/${dep_path}@${current_commit}"

echo "::endgroup::"
done
45 changes: 45 additions & 0 deletions .github/workflows/auto-updates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,48 @@ jobs:
if: steps.check-readme.outputs.modified == 'true'
run: |
git push origin auto-updates/readme-cli-ref:main

update-internal-dependencies:
name: Update internal dependencies
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
ref: main
- name: Set up git
uses: ./.github/actions/setup-git
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up go
uses: actions/setup-go@v4
with:
go-version-file: go.work
- name: Update submodules
uses: ./.github/actions/update-workspace-dependencies
with:
module: wsl-pro-service
- name: Detect modifications
shell: bash
id: check-diff
run: |
hasModif="false"
MODIFIED=$(git status --porcelain --untracked-files=no)
if [ -n "$MODIFIED" ]; then
hasModif="true"
fi
echo "modified=${hasModif}" >> $GITHUB_OUTPUT
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
if: steps.check-diff.outputs.modified == 'true'
with:
commit-message: Auto update internal dependencies
title: Auto update WSL-Pro-Service dependencies
labels: automated pr
body: "[Auto-generated pull request](https://github.com/canonical/ubuntu-pro-for-windows/actions/workflows/auto-updates.yaml) by GitHub Action"
branch: auto-updates/update-internal-dependencies
token: ${{ secrets.GITHUB_TOKEN }}
- name: Push branch
if: steps.check-diff.outputs.modified == 'true'
run: |
git push origin auto-updates/update-internal-dependencies:main
Loading