From 96daf3b200f1845977212f5e46906521bd3d37ff Mon Sep 17 00:00:00 2001 From: Scott Gibb Date: Wed, 11 Dec 2024 12:34:03 +0000 Subject: [PATCH 1/3] Added updated PR flow --- README.md | 79 ++++++++++---- TEMPLATES/mega-linter.yml | 102 ++++++++---------- .../mega-linter/templates/mega-linter.yml | 65 ++++------- 3 files changed, 121 insertions(+), 125 deletions(-) diff --git a/README.md b/README.md index a0dddd3e6ea..2f0a6198b50 100644 --- a/README.md +++ b/README.md @@ -426,6 +426,7 @@ In your repository you should have a `.github/workflows` folder with **GitHub** --- # MegaLinter GitHub Action configuration file # More info at https://megalinter.io +--- name: MegaLinter on: @@ -438,7 +439,7 @@ env: # Comment env block if you don't want to apply fixes # Apply linter fixes configuration APPLY_FIXES: all # When active, APPLY_FIXES must also be defined as environment variable (in github/workflows/mega-linter.yml or other CI tool) APPLY_FIXES_EVENT: pull_request # Decide which event triggers application of fixes in a commit or a PR (pull_request, push, all) - APPLY_FIXES_MODE: commit # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request) + APPLY_FIXES_MODE: pull_request # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request) concurrency: group: ${{ github.ref }}-${{ github.workflow }} @@ -448,12 +449,16 @@ jobs: megalinter: name: MegaLinter runs-on: ubuntu-latest + + # Give the default GITHUB_TOKEN write permission to commit and push, comment + # issues, and post new Pull Requests; remove the ones you do not need + # Statuses, give permission to write statuses on wether it can be merged or not permissions: - # Give the default GITHUB_TOKEN write permission to commit and push, comment issues & post new PR - # Remove the ones you do not need contents: write issues: write pull-requests: write + statuses: write + steps: # Git Checkout - name: Checkout Code @@ -486,34 +491,62 @@ jobs: megalinter-reports mega-linter.log - # Create pull request if applicable (for now works only on PR from same repository, not from forks) + # Create pull request if applicable + # (for now works only on PR from same repository, not from forks) - name: Create Pull Request with applied fixes - id: cpr - if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') uses: peter-evans/create-pull-request@v6 + id: cpr + if: >- + steps.ml.outputs.has_updated_sources == 1 && + ( + env.APPLY_FIXES_EVENT == 'all' || + env.APPLY_FIXES_EVENT == github.event_name + ) && + env.APPLY_FIXES_MODE == 'pull_request' && + ( + github.event_name == 'push' || + github.event.pull_request.head.repo.full_name == github.repository + ) && + !contains(github.event.head_commit.message, 'skip fix') with: token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }} commit-message: "[MegaLinter] Apply linters automatic fixes" title: "[MegaLinter] Apply linters automatic fixes" labels: bot - - name: Create PR output - if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') - run: | - echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}" - echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" - - # Push new commit if applicable (for now works only on PR from same repository, not from forks) - - name: Prepare commit - if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') - run: sudo chown -Rc $UID .git/ - - name: Commit and push applied linter fixes - if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') - uses: stefanzweifel/git-auto-commit-action@v4 + branch: megalinter-fixes-${{ github.head_ref }} + body: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + base: ${{ github.head_ref }} + + - name: Post PR Comment + if: >- + steps.ml.outputs.has_updated_sources == 1 && + ( + env.APPLY_FIXES_EVENT == 'all' || + env.APPLY_FIXES_EVENT == github.event_name + ) && + env.APPLY_FIXES_MODE == 'pull_request' && + ( + github.event_name == 'push' || + github.event.pull_request.head.repo.full_name == github.repository + ) && + !contains(github.event.head_commit.message, 'skip fix') + uses: marocchino/sticky-pull-request-comment@v2 with: - branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }} - commit_message: "[MegaLinter] Apply linters fixes" - commit_user_name: megalinter-bot - commit_user_email: nicolas.vuillamy@ox.security + message: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + PR: ${{ steps.cpr.outputs.pull-request-url }} + hide_and_recreate: true + + - name: Fail if PR Created + run: | + if [ "${{ steps.cpr.outputs.pull-request-url }}" != "" ]; then + echo "A PR with formatting fixes has been created. Please merge it before proceeding." + exit 1 + fi + ``` diff --git a/TEMPLATES/mega-linter.yml b/TEMPLATES/mega-linter.yml index 3be4a906cf0..3b6783f33d1 100644 --- a/TEMPLATES/mega-linter.yml +++ b/TEMPLATES/mega-linter.yml @@ -29,25 +29,24 @@ env: # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) # or posted in a PR (pull_request) - APPLY_FIXES_MODE: commit + APPLY_FIXES_MODE: pull_request concurrency: group: ${{ github.ref }}-${{ github.workflow }} cancel-in-progress: true -permissions: {} - jobs: megalinter: name: MegaLinter runs-on: ubuntu-latest # Give the default GITHUB_TOKEN write permission to commit and push, comment - # issues & post new PR; remove the ones you do not need + # issues, and post new Pull Requests; remove the ones you do not need permissions: contents: write issues: write pull-requests: write + statuses: write steps: # Git Checkout @@ -108,68 +107,59 @@ jobs: megalinter-reports mega-linter.log - # Set APPLY_FIXES_IF var for use in future steps - - name: Set APPLY_FIXES_IF var - run: | - printf 'APPLY_FIXES_IF=%s\n' "${{ - steps.ml.outputs.has_updated_sources == 1 && - ( - env.APPLY_FIXES_EVENT == 'all' || - env.APPLY_FIXES_EVENT == github.event_name - ) && - ( - github.event_name == 'push' || - github.event.pull_request.head.repo.full_name == github.repository - ) - }}" >> "${GITHUB_ENV}" - - # Set APPLY_FIXES_IF_* vars for use in future steps - - name: Set APPLY_FIXES_IF_* vars - run: | - printf 'APPLY_FIXES_IF_PR=%s\n' "${{ - env.APPLY_FIXES_IF == 'true' && - env.APPLY_FIXES_MODE == 'pull_request' - }}" >> "${GITHUB_ENV}" - printf 'APPLY_FIXES_IF_COMMIT=%s\n' "${{ - env.APPLY_FIXES_IF == 'true' && - env.APPLY_FIXES_MODE == 'commit' && - (!contains(fromJSON('["refs/heads/main", "refs/heads/master"]'), github.ref)) - }}" >> "${GITHUB_ENV}" - # Create pull request if applicable # (for now works only on PR from same repository, not from forks) - name: Create Pull Request with applied fixes uses: peter-evans/create-pull-request@v6 id: cpr - if: env.APPLY_FIXES_IF_PR == 'true' + if: >- + steps.ml.outputs.has_updated_sources == 1 && + ( + env.APPLY_FIXES_EVENT == 'all' || + env.APPLY_FIXES_EVENT == github.event_name + ) && + env.APPLY_FIXES_MODE == 'pull_request' && + ( + github.event_name == 'push' || + github.event.pull_request.head.repo.full_name == github.repository + ) && + !contains(github.event.head_commit.message, 'skip fix') with: token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }} commit-message: "[MegaLinter] Apply linters automatic fixes" title: "[MegaLinter] Apply linters automatic fixes" labels: bot + branch: megalinter-fixes-${{ github.head_ref }} + body: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + base: ${{ github.head_ref }} + + - name: Post PR Comment + if: >- + steps.ml.outputs.has_updated_sources == 1 && + ( + env.APPLY_FIXES_EVENT == 'all' || + env.APPLY_FIXES_EVENT == github.event_name + ) && + env.APPLY_FIXES_MODE == 'pull_request' && + ( + github.event_name == 'push' || + github.event.pull_request.head.repo.full_name == github.repository + ) && + !contains(github.event.head_commit.message, 'skip fix') + uses: marocchino/sticky-pull-request-comment@v2 + with: + message: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + PR: ${{ steps.cpr.outputs.pull-request-url }} + hide_and_recreate: true - - name: Create PR output - if: env.APPLY_FIXES_IF_PR == 'true' + - name: Fail if PR Created run: | - echo "PR Number - ${{ steps.cpr.outputs.pull-request-number }}" - echo "PR URL - ${{ steps.cpr.outputs.pull-request-url }}" + if [ "${{ steps.cpr.outputs.pull-request-url }}" != "" ]; then + echo "A PR with formatting fixes has been created. Please merge it before proceeding." + exit 1 + fi - # Push new commit if applicable - # (for now works only on PR from same repository, not from forks) - - name: Prepare commit - if: env.APPLY_FIXES_IF_COMMIT == 'true' - run: sudo chown -Rc $UID .git/ - - - name: Commit and push applied linter fixes - uses: stefanzweifel/git-auto-commit-action@v4 - if: env.APPLY_FIXES_IF_COMMIT == 'true' - with: - branch: >- - ${{ - github.event.pull_request.head.ref || - github.head_ref || - github.ref - }} - commit_message: "[MegaLinter] Apply linters fixes" - commit_user_name: megalinter-bot - commit_user_email: nicolas.vuillamy@ox.security diff --git a/mega-linter-runner/generators/mega-linter/templates/mega-linter.yml b/mega-linter-runner/generators/mega-linter/templates/mega-linter.yml index 301bccc8db5..2818959e573 100644 --- a/mega-linter-runner/generators/mega-linter/templates/mega-linter.yml +++ b/mega-linter-runner/generators/mega-linter/templates/mega-linter.yml @@ -131,8 +131,13 @@ jobs: commit-message: "[MegaLinter] Apply linters automatic fixes" title: "[MegaLinter] Apply linters automatic fixes" labels: bot + branch: megalinter-fixes-${{ github.head_ref }} + body: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + base: ${{ github.head_ref }} - - name: Create PR output + - name: Post PR Comment if: >- steps.ml.outputs.has_updated_sources == 1 && ( @@ -145,50 +150,18 @@ jobs: github.event.pull_request.head.repo.full_name == github.repository ) && !contains(github.event.head_commit.message, 'skip fix') - run: | - echo "PR Number - ${{ steps.cpr.outputs.pull-request-number }}" - echo "PR URL - ${{ steps.cpr.outputs.pull-request-url }}" + uses: marocchino/sticky-pull-request-comment@v2 + with: + message: | + MegaLinter has automatically applied linters fixes on this PR. + Please review the changes and merge if they are correct. + PR: ${{ steps.cpr.outputs.pull-request-url }} + hide_and_recreate: true - # Push new commit if applicable - # (for now works only on PR from same repository, not from forks) - - name: Prepare commit - if: >- - steps.ml.outputs.has_updated_sources == 1 && - ( - env.APPLY_FIXES_EVENT == 'all' || - env.APPLY_FIXES_EVENT == github.event_name - ) && - env.APPLY_FIXES_MODE == 'commit' && - github.ref != 'refs/heads/main' && - ( - github.event_name == 'push' || - github.event.pull_request.head.repo.full_name == github.repository - ) && - !contains(github.event.head_commit.message, 'skip fix') - run: sudo chown -Rc $UID .git/ + - name: Fail if PR Created + run: | + if [ "${{ steps.cpr.outputs.pull-request-url }}" != "" ]; then + echo "A PR with formatting fixes has been created. Please merge it before proceeding." + exit 1 + fi - - name: Commit and push applied linter fixes - uses: stefanzweifel/git-auto-commit-action@v5 - if: >- - steps.ml.outputs.has_updated_sources == 1 && - ( - env.APPLY_FIXES_EVENT == 'all' || - env.APPLY_FIXES_EVENT == github.event_name - ) && - env.APPLY_FIXES_MODE == 'commit' && - github.ref != 'refs/heads/main' && - ( - github.event_name == 'push' || - github.event.pull_request.head.repo.full_name == github.repository - ) && - !contains(github.event.head_commit.message, 'skip fix') - with: - branch: >- - ${{ - github.event.pull_request.head.ref || - github.head_ref || - github.ref - }} - commit_message: "[MegaLinter] Apply linters fixes" - commit_user_name: megalinter-bot - commit_user_email: nicolas.vuillamy@ox.security From 86a1a2ef091d6c5646c3bed5865c91b9e5459c0c Mon Sep 17 00:00:00 2001 From: Scott Gibb Date: Wed, 11 Dec 2024 12:40:35 +0000 Subject: [PATCH 2/3] removed doc line --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 2f0a6198b50..4ba41cf9f5e 100644 --- a/README.md +++ b/README.md @@ -423,7 +423,6 @@ In your repository you should have a `.github/workflows` folder with **GitHub** This file should have this code ```yml ---- # MegaLinter GitHub Action configuration file # More info at https://megalinter.io --- From 70c4ac7af1857bacd8dc7bc7342405fa033baa7d Mon Sep 17 00:00:00 2001 From: Scott Gibb Date: Wed, 11 Dec 2024 12:43:26 +0000 Subject: [PATCH 3/3] fixing doc line --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ba41cf9f5e..1e075d5a307 100644 --- a/README.md +++ b/README.md @@ -423,9 +423,9 @@ In your repository you should have a `.github/workflows` folder with **GitHub** This file should have this code ```yml +--- # MegaLinter GitHub Action configuration file # More info at https://megalinter.io ---- name: MegaLinter on: