Skip to content

feat: externalize packages for i18nify-go #143

feat: externalize packages for i18nify-go

feat: externalize packages for i18nify-go #143

name: Auto-Generate Go Packages
on:
pull_request:
types: [opened, synchronize]
branches:
- master
paths:
- 'i18nify-data/country/subdivisions/**'
- 'i18nify-data/currency/**'
- 'i18nify-data/bankcodes/**'
- 'i18nify-data/country/metadata/**'
- '!i18nify-data/go/**'
push:
branches:
- master
paths:
- 'i18nify-data/country/subdivisions/**'
- 'i18nify-data/currency/**'
- 'i18nify-data/bankcodes/**'
- 'i18nify-data/country/metadata/**'
- '!i18nify-data/go/**'
# Manual trigger via PR comment (only works from default branch)
issue_comment:
types: [created]
# Manual trigger via GitHub Actions UI
workflow_dispatch:
inputs:
package:
description: 'Package to generate'
required: false
type: choice
default: 'all'
options:
- 'all'
- 'country/subdivisions'
- 'currency'
- 'bankcodes'
- 'country/metadata'
branch:
description: 'Branch to run on'
required: true
default: 'pt/externalize-packages'
concurrency:
group: auto-generate-go-${{ github.event_name }}-${{ github.event.issue.number || github.ref }}
cancel-in-progress: false
jobs:
# ============================================
# STEP 1: Determine what packages to generate
# ============================================
# For PR/push: detect which packages have data changes
detect-changes:
name: Detect Data Changes
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' || github.event_name == 'push'
outputs:
packages: ${{ steps.filter.outputs.changes }}
branch: ${{ github.event.pull_request.head.ref || github.ref_name }}
pr_number: ${{ github.event.pull_request.number }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: dorny/paths-filter@v3
id: filter
with:
base: ${{ github.event.pull_request.base.ref || github.event.before || 'HEAD~1' }}
filters: |
country/subdivisions:
- 'i18nify-data/country/subdivisions/**'
- '!i18nify-data/country/subdivisions/README.md'
currency:
- 'i18nify-data/currency/**'
- '!i18nify-data/currency/README.md'
bankcodes:
- 'i18nify-data/bankcodes/**'
- '!i18nify-data/bankcodes/README.md'
country/metadata:
- 'i18nify-data/country/metadata/**'
- '!i18nify-data/country/metadata/README.md'
# For comment trigger: parse comment and get packages
check-comment:
name: Parse Comment Trigger
runs-on: ubuntu-latest
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request != null &&
contains(github.event.comment.body, 'generate-package-go')
outputs:
packages: ${{ steps.parse.outputs.packages }}
branch: ${{ steps.pr-info.outputs.branch }}
pr_number: ${{ github.event.issue.number }}
steps:
- name: Parse comment
id: parse
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
PACKAGE=$(echo "$COMMENT_BODY" | grep -oP 'generate-package-go\s+\K[^\s]+' || echo "")
if [ -n "$PACKAGE" ]; then
echo "packages=[\"$PACKAGE\"]" >> $GITHUB_OUTPUT
else
echo 'packages=["country/subdivisions","currency","bankcodes","country/metadata"]' >> $GITHUB_OUTPUT
fi
- name: Get PR branch
id: pr-info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} --jq '.head.ref')
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
- name: React to comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions -f content='+1'
# For workflow_dispatch: get inputs
check-dispatch:
name: Parse Manual Dispatch
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
outputs:
packages: ${{ steps.parse.outputs.packages }}
branch: ${{ github.event.inputs.branch }}
steps:
- name: Parse inputs
id: parse
env:
PACKAGE: ${{ github.event.inputs.package }}
run: |
if [ "$PACKAGE" != "all" ] && [ -n "$PACKAGE" ]; then
echo "packages=[\"$PACKAGE\"]" >> $GITHUB_OUTPUT
else
echo 'packages=["country/subdivisions","currency","bankcodes","country/metadata"]' >> $GITHUB_OUTPUT
fi
# ============================================
# STEP 2: Generate packages (only if needed)
# ============================================
generate:
name: Generate ${{ matrix.package }}
runs-on: ubuntu-latest
needs: [detect-changes, check-comment, check-dispatch]
if: |
always() && (
(needs.detect-changes.result == 'success' && needs.detect-changes.outputs.packages != '[]' && needs.detect-changes.outputs.packages != '') ||
(needs.check-comment.result == 'success') ||
(needs.check-dispatch.result == 'success')
)
strategy:
matrix:
package: ${{ fromJson(needs.check-dispatch.outputs.packages || needs.check-comment.outputs.packages || needs.detect-changes.outputs.packages || '[]') }}
fail-fast: false
env:
BRANCH: ${{ needs.check-dispatch.outputs.branch || needs.check-comment.outputs.branch || needs.detect-changes.outputs.branch }}
PR_NUMBER: ${{ needs.check-comment.outputs.pr_number || needs.detect-changes.outputs.pr_number || '' }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ env.BRANCH }}
- name: Check if proto exists
id: check-proto
run: |
PROTO_DIR="i18nify-data/${{ matrix.package }}/proto"
if [ -d "$PROTO_DIR" ] && ls "$PROTO_DIR"/*.proto 1> /dev/null 2>&1; then
echo "has_proto=true" >> $GITHUB_OUTPUT
else
echo "has_proto=false" >> $GITHUB_OUTPUT
echo "⚠️ No proto found for ${{ matrix.package }} - skipping"
fi
- name: Setup Go
if: steps.check-proto.outputs.has_proto == 'true'
uses: actions/setup-go@v5
with:
go-version: '1.20'
- name: Install protoc
if: steps.check-proto.outputs.has_proto == 'true'
run: |
sudo apt-get update
sudo apt-get install -y protobuf-compiler jq
go install google.golang.org/protobuf/cmd/[email protected]
echo "$HOME/go/bin" >> $GITHUB_PATH
- name: Generate package
if: steps.check-proto.outputs.has_proto == 'true'
run: |
chmod +x .github/scripts/go/generate-package.sh
.github/scripts/go/generate-package.sh "${{ matrix.package }}"
- name: Commit and push generated package
if: steps.check-proto.outputs.has_proto == 'true'
id: commit
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add "i18nify-data/go/${{ matrix.package }}"
if git diff --cached --quiet; then
echo "No changes to commit"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
git commit -m "chore: auto-regenerate ${{ matrix.package }} Go package"
git pull --rebase origin "$BRANCH" || true
git push origin HEAD:"$BRANCH"
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Generate version and update go.mod
if: steps.check-proto.outputs.has_proto == 'true'
id: version
run: |
# Generate pseudo-version from HEAD
COMMIT_SHA=$(git rev-parse HEAD)
SHORT_SHA=$(echo "$COMMIT_SHA" | cut -c1-12)
TIMESTAMP=$(TZ=UTC git show -s --format=%cd --date=format-local:%Y%m%d%H%M%S "$COMMIT_SHA")
VERSION="v0.0.0-${TIMESTAMP}-${SHORT_SHA}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
# Update go.mod
chmod +x .github/scripts/go/update-dependencies.sh
.github/scripts/go/update-dependencies.sh "${{ matrix.package }}" "$VERSION"
- name: Commit and push go.mod
if: steps.check-proto.outputs.has_proto == 'true'
run: |
git add packages/i18nify-go/go.mod packages/i18nify-go/go.sum
if git diff --cached --quiet; then
echo "No go.mod changes"
exit 0
fi
git commit -m "chore(go): update ${{ matrix.package }} to ${{ steps.version.outputs.version }}"
git pull --rebase origin "$BRANCH" || true
git push origin HEAD:"$BRANCH"
- name: Comment on PR
if: steps.check-proto.outputs.has_proto == 'true' && env.PR_NUMBER != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ steps.version.outputs.version }}"
MODULE="github.com/razorpay/i18nify/i18nify-data/go/${{ matrix.package }}"
gh pr comment "$PR_NUMBER" --body "## 📦 Package Updated
**Package:** \`${{ matrix.package }}\`
**Version:** \`$VERSION\`
\`\`\`bash
go get ${MODULE}@${VERSION}
\`\`\`"