Skip to content

Commit 13aa8b5

Browse files
authored
TEMPLATES/mega-linter.yml: DRY the ifs (#2957)
* TEMPLATES/mega-linter.yml: DRY the ifs * Update .github/workflows/mega-linter.yml Per #2957 (comment) * Update .github/workflows/mega-linter-for-runner.yml Per #2957 (comment) * TEMPLATES/mega-linter.yml: Bump actions/checkout to v4 * Update mega-linter.yml * Update mega-linter-for-runner.yml
1 parent 0b906e8 commit 13aa8b5

File tree

3 files changed

+272
-112
lines changed

3 files changed

+272
-112
lines changed
Lines changed: 120 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,180 @@
1-
---
21
# MegaLinter GitHub Action configuration file
32
# More info at https://megalinter.io
4-
name: MegaLinter for mega-linter-runner
3+
---
4+
name: MegaLinter for mega-linter-runner # ml workflow change
55

6-
# Start lint on any push and on pull requests
6+
# Trigger mega-linter at every push. Action will also be visible from Pull
7+
# Requests to main
78
on:
8-
# Trigger mega-linter at every push. Action will also be visible from Pull Requests to master
9-
push: # Comment this line to trigger action only on pull-requests (not recommended if you don't pay for GH Actions)
9+
# Comment this line to trigger action only on pull-requests
10+
# (not recommended if you don't pay for GH Actions)
11+
push:
12+
1013
pull_request:
11-
branches: [master, main, alpha]
14+
branches:
15+
- main
16+
- master
17+
- alpha # ml workflow addition
1218

13-
env: # Comment env block if you do not want to apply fixes
19+
# Comment env block if you do not want to apply fixes
20+
env:
1421
# Apply linter fixes configuration
15-
APPLY_FIXES: all # When active, APPLY_FIXES must also be defined as environment variable (in github/workflows/mega-linter.yml or other CI tool)
16-
APPLY_FIXES_EVENT: pull_request # Decide which event triggers application of fixes in a commit or a PR (pull_request, push, all)
17-
APPLY_FIXES_MODE: commit # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request)
22+
#
23+
# When active, APPLY_FIXES must also be defined as environment variable
24+
# (in github/workflows/mega-linter.yml or other CI tool)
25+
APPLY_FIXES: all
26+
27+
# Decide which event triggers application of fixes in a commit or a PR
28+
# (pull_request, push, all)
29+
APPLY_FIXES_EVENT: pull_request
30+
31+
# If APPLY_FIXES is used, defines if the fixes are directly committed (commit)
32+
# or posted in a PR (pull_request)
33+
APPLY_FIXES_MODE: commit
1834

1935
concurrency:
2036
group: ${{ github.ref }}-${{ github.workflow }}
2137
cancel-in-progress: true
2238

2339
jobs:
24-
build:
40+
megalinter:
2541
name: MegaLinter
2642
runs-on: ubuntu-latest
43+
44+
# Give the default GITHUB_TOKEN write permission to commit and push, comment
45+
# issues & post new PR; remove the ones you do not need
2746
permissions:
28-
# Give the default GITHUB_TOKEN write permission to commit and push, comment issues & post new PR
29-
# Remove the ones you do not need
3047
contents: write
3148
issues: write
3249
pull-requests: write
33-
environment:
34-
name: dev
50+
environment: # ml workflow addition
51+
name: dev # ml workflow addition
52+
3553
steps:
54+
55+
# Git Checkout
3656
- name: Checkout Code
3757
uses: actions/checkout@v4
3858
with:
3959
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
60+
61+
# If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to
62+
# improve performance
4063
fetch-depth: 0
4164

4265
# MegaLinter
4366
- name: MegaLinter
44-
id: ml
67+
4568
# You can override MegaLinter flavor used to have faster performances
4669
# More info at https://megalinter.io/flavors/
47-
uses: oxsecurity/megalinter/flavors/javascript@beta
70+
uses: oxsecurity/megalinter/flavors/javascript@beta # ml workflow change
71+
72+
id: ml
73+
74+
# All available variables are described in documentation
75+
# https://megalinter.io/configuration/
4876
env:
49-
# All available variables are described in documentation
50-
# https://megalinter.io/configuration/#shared-variables
51-
# ADD YOUR CUSTOM ENV VARIABLES HERE OR DEFINE THEM IN A FILE .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
52-
DEFAULT_WORKSPACE: mega-linter-runner
53-
VALIDATE_ALL_CODEBASE: true # ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} # Validates all source when push on main, else just the git diff with main. Override with true if you always want to lint all sources
77+
DEFAULT_WORKSPACE: mega-linter-runner # ml workflow addition
78+
79+
# Validates all source when push on main, else just the git diff with
80+
# main. Override with true if you always want to lint all sources
81+
#
82+
# To validate the entire codebase, set to:
83+
# VALIDATE_ALL_CODEBASE: true
84+
#
85+
# To validate only diff with main, set to:
86+
# VALIDATE_ALL_CODEBASE: >-
87+
# ${{
88+
# github.event_name == 'push' &&
89+
# contains(fromJSON('["refs/heads/main", "refs/heads/master"]'), github.ref)
90+
# }}
91+
VALIDATE_ALL_CODEBASE: true # ml workflow change
92+
# >-
93+
# ${{
94+
# github.event_name == 'push' &&
95+
# contains(fromJSON('["refs/heads/main", "refs/heads/master"]'), github.ref)
96+
# }}
97+
5498
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5599

100+
# ADD YOUR CUSTOM ENV VARIABLES HERE OR DEFINE THEM IN A FILE
101+
# .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
102+
103+
# Uncomment to disable copy-paste and spell checks
104+
# DISABLE: COPYPASTE,SPELL
105+
56106
# Upload MegaLinter artifacts
57107
- name: Archive production artifacts
58-
if: ${{ success() }} || ${{ failure() }}
59108
uses: actions/upload-artifact@v3
109+
if: ${{ success() }} || ${{ failure() }}
60110
with:
61111
name: MegaLinter reports
62112
path: |
63113
megalinter-reports
64114
mega-linter.log
65115
66-
# Create pull request if applicable (for now works only on PR from same repository, not from forks)
116+
# Set APPLY_FIXES_IF var for use in future steps
117+
- name: Set APPLY_FIXES_IF var
118+
run: |
119+
printf 'APPLY_FIXES_IF=%s\n' "${{
120+
steps.ml.outputs.has_updated_sources == 1 &&
121+
(
122+
env.APPLY_FIXES_EVENT == 'all' ||
123+
env.APPLY_FIXES_EVENT == github.event_name
124+
) &&
125+
(
126+
github.event_name == 'push' ||
127+
github.event.pull_request.head.repo.full_name == github.repository
128+
)
129+
}}" >> "${GITHUB_ENV}"
130+
131+
# Set APPLY_FIXES_IF_* vars for use in future steps
132+
- name: Set APPLY_FIXES_IF_* vars
133+
run: |
134+
printf 'APPLY_FIXES_IF_PR=%s\n' "${{
135+
env.APPLY_FIXES_IF == 'true' &&
136+
env.APPLY_FIXES_MODE == 'pull_request'
137+
}}" >> "${GITHUB_ENV}"
138+
printf 'APPLY_FIXES_IF_COMMIT=%s\n' "${{
139+
env.APPLY_FIXES_IF == 'true' &&
140+
env.APPLY_FIXES_MODE == 'commit' &&
141+
(!contains(fromJSON('["refs/heads/main", "refs/heads/master"]'), github.ref))
142+
}}" >> "${GITHUB_ENV}"
143+
144+
# Create pull request if applicable
145+
# (for now works only on PR from same repository, not from forks)
67146
- name: Create Pull Request with applied fixes
68-
id: cpr
69-
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')
70147
uses: peter-evans/create-pull-request@v5
148+
id: cpr
149+
if: env.APPLY_FIXES_IF_PR == 'true'
71150
with:
72151
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
73152
commit-message: "[MegaLinter] Apply linters automatic fixes"
74153
title: "[MegaLinter] Apply linters automatic fixes"
75154
labels: bot
155+
76156
- name: Create PR output
77-
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')
157+
if: env.APPLY_FIXES_IF_PR == 'true'
78158
run: |
79-
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
80-
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
159+
echo "PR Number - ${{ steps.cpr.outputs.pull-request-number }}"
160+
echo "PR URL - ${{ steps.cpr.outputs.pull-request-url }}"
81161
82-
# Push new commit if applicable (for now works only on PR from same repository, not from forks)
162+
# Push new commit if applicable
163+
# (for now works only on PR from same repository, not from forks)
83164
- name: Prepare commit
84-
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')
165+
if: env.APPLY_FIXES_IF_COMMIT == 'true'
85166
run: sudo chown -Rc $UID .git/
167+
86168
- name: Commit and push applied linter fixes
87-
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')
88169
uses: stefanzweifel/git-auto-commit-action@v5
170+
if: env.APPLY_FIXES_IF_COMMIT == 'true'
89171
with:
90-
branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
172+
branch: >-
173+
${{
174+
github.event.pull_request.head.ref ||
175+
github.head_ref ||
176+
github.ref
177+
}}
91178
commit_message: "[MegaLinter] Apply linters fixes"
92179
commit_user_name: megalinter-bot
93180
commit_user_email: [email protected]

0 commit comments

Comments
 (0)