Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrans committed Jan 15, 2025
1 parent 78b7859 commit fa02843
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/circular-deps-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check Circular Dependencies

on:
pull_request:
branches: [ main, develop ]

jobs:
check-circular-deps:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: metamask/github-tools/.github/actions/setup-environment@main

- name: Install madge
run: yarn add -D madge

- name: Check circular dependencies
run: yarn circular:check
35 changes: 35 additions & 0 deletions .github/workflows/circular-deps-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Update Circular Dependencies

on:
issue_comment:
types: created

jobs:
shared-flow:
uses: ./.github/workflows/shared/metamaskbot-pr-update.yml
with:
bot_command: '@metamaskbot update-circular-deps'
commit_message: 'Update circular dependencies'
file_pattern: 'circular-deps.json'
secrets:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

update-circular-deps:
name: Update Circular Dependencies
needs: shared-flow
if: ${{ needs.shared-flow.outputs.is_fork == 'false' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Setup environment
uses: metamask/github-tools/.github/actions/setup-environment@main
- name: Install madge
run: yarn add -D madge
- name: Update circular dependencies
run: yarn circular:update
194 changes: 194 additions & 0 deletions .github/workflows/shared/metamaskbot-pr-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
name: MetaMask Bot PR Update Template

# TODO: Migrate the following workflows to use this shared template:
# - .github/workflows/update-attributions.yml
# - .github/workflows/update-lavamoat-policies.yml
#
# These workflows share similar patterns for:
# - PR fork checking
# - Comment reactions
# - Environment setup
# - Change detection
# - Committing changes
# - Status checking and notifications

on:
workflow_call:
inputs:
bot_command:
required: true
type: string
commit_message:
required: true
type: string
file_pattern:
required: false
type: string
default: '.'
outputs:
is_fork:
description: "Whether the PR is from a fork"
value: ${{ jobs.is-fork-pull-request.outputs.IS_FORK }}
has_changes:
description: "Whether there are changes to commit"
value: ${{ jobs.check-changes.outputs.HAS_CHANGES }}
secrets:
GITHUB_TOKEN:
required: true

jobs:
is-fork-pull-request:
name: Check if PR is from fork
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, inputs.bot_command) }}
outputs:
IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }}
steps:
- uses: actions/checkout@v4
- name: Check fork status
id: is-fork
run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}

react-to-comment:
name: React to comment
needs: is-fork-pull-request
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
runs-on: ubuntu-latest
steps:
- name: Add reaction
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${REPO}/issues/comments/${COMMENT_ID}/reactions" \
-f content='+1'
env:
COMMENT_ID: ${{ github.event.comment.id }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}

prepare:
name: Prepare environment
needs: is-fork-pull-request
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
runs-on: ubuntu-latest
outputs:
COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Setup environment
uses: metamask/github-tools/.github/actions/setup-environment@main
- name: Get commit SHA
id: commit-sha
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

check-changes:
name: Check for changes
needs: [is-fork-pull-request, prepare]
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
runs-on: ubuntu-latest
outputs:
HAS_CHANGES: ${{ steps.changes.outputs.HAS_CHANGES }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Check for changes
id: changes
run: |
if git diff --exit-code ${{ inputs.file_pattern }}
then
echo "HAS_CHANGES=false" >> "$GITHUB_OUTPUT"
else
echo "HAS_CHANGES=true" >> "$GITHUB_OUTPUT"
fi
commit-changes:
name: Commit changes
needs: [is-fork-pull-request, check-changes]
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' && needs.check-changes.outputs.HAS_CHANGES == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Commit changes
run: |
git config --global user.name 'MetaMask Bot'
git config --global user.email '[email protected]'
git add ${{ inputs.file_pattern }}
git commit -m "${{ inputs.commit_message }}"
git push
- name: Post success comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
gh pr comment "${PR_NUMBER}" \
--body "Changes committed: ${{ inputs.commit_message }}"
post-no-changes:
name: Post no changes comment
needs: [is-fork-pull-request, check-changes]
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' && needs.check-changes.outputs.HAS_CHANGES == 'false' }}
runs-on: ubuntu-latest
steps:
- name: Post comment
run: gh pr comment "${PR_NUMBER}" --body "No changes needed"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}

check-status:
name: Check update status
needs: [is-fork-pull-request, commit-changes, post-no-changes]
if: ${{ always() && needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
runs-on: ubuntu-latest
outputs:
PASSED: ${{ steps.set-output.outputs.PASSED }}
steps:
- name: Set status
id: set-output
run: |
if [[ "${{ needs.commit-changes.result }}" == "success" || "${{ needs.post-no-changes.result }}" == "success" ]]; then
echo "PASSED=true" >> "$GITHUB_OUTPUT"
else
echo "PASSED=false" >> "$GITHUB_OUTPUT"
fi
post-failure:
name: Post failure comment
needs: [is-fork-pull-request, check-status]
if: ${{ always() && needs.is-fork-pull-request.outputs.IS_FORK == 'false' && needs.check-status.outputs.PASSED != 'true' }}
runs-on: ubuntu-latest
steps:
- name: Post comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
ACTION_RUN_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
run: |
gh pr comment "${PR_NUMBER}" \
--body "Update failed. You can [review the logs or retry the update here](${ACTION_RUN_URL})"
34 changes: 34 additions & 0 deletions development/circular-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

COMMAND="${1:-}"
if [[ "$COMMAND" != "check" && "$COMMAND" != "update" ]]; then
echo "Usage: $0 [check|update]"
exit 1
fi

# Common madge arguments for circular dependency detection
MADGE_ARGS="--circular --extensions js,jsx,ts,tsx --exclude '(test|stories|storybook|\.test\.|\.spec\.)' --ts-config tsconfig.json --webpack-config webpack.config.js . --json"

if [[ "$COMMAND" == "check" ]]; then
# Generate current circular dependencies
yarn madge $MADGE_ARGS > circular-deps.temp.json

# Compare with existing file
if ! diff circular-deps.temp.json circular-deps.json > /dev/null 2>&1; then
echo "Error: Circular dependencies have changed."
echo "Run '@metamaskbot update-circular-deps' to update the circular dependencies file."
rm circular-deps.temp.json
exit 1
fi

rm circular-deps.temp.json
echo "Circular dependencies check passed."
else
# Generate circular dependencies and update the file
yarn madge $MADGE_ARGS > circular-deps.json
echo "Updated circular-deps.json with current circular dependencies."
fi
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"dist:mmi:debug": "yarn dist --build-type mmi --apply-lavamoat=false --snow=false",
"build": "yarn lavamoat:build",
"build:dev": "node development/build/index.js",
"circular-deps:check": "./development/circular-deps.sh check",
"circular-deps:update": "./development/circular-deps.sh update",
"start:test": "yarn env:e2e build:dev testDev --apply-lavamoat=false",
"start:test:flask": "yarn start:test --build-type flask --apply-lavamoat=false",
"start:test:mv2:flask": "ENABLE_MV3=false yarn start:test:flask --apply-lavamoat=false --snow=false",
Expand Down Expand Up @@ -775,4 +777,4 @@
}
},
"packageManager": "[email protected]"
}
}

0 comments on commit fa02843

Please sign in to comment.