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

Add support for Bicep Linter #1898

Merged
merged 32 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b527ed5
Add Bicep to ARM descriptor
omusavi Sep 20, 2022
975fb0e
Add test cases
omusavi Sep 20, 2022
827f15d
Update changelog
omusavi Sep 20, 2022
666eb0d
Add version regex
omusavi Sep 20, 2022
44e7a1c
Remove unnecessary class
omusavi Sep 21, 2022
b5a7974
Fix test extension
omusavi Sep 21, 2022
91020a8
generated changes
omusavi Sep 21, 2022
75ed178
Fix missing '\' in Dockerfile
omusavi Sep 22, 2022
8959404
Generated fixes
omusavi Sep 22, 2022
6a1cbc5
Merge branch 'main' into bicep-linter
omusavi Sep 22, 2022
9aa6b92
Simplify bicep tests
omusavi Sep 22, 2022
c83f946
Remove sudo from Dockerfile
omusavi Sep 22, 2022
f93c820
Add bicepconfig file to cspell ignore
omusavi Sep 22, 2022
bb6a0bc
Download file to bicep filename
omusavi Sep 22, 2022
c7b3aa1
Merge branch 'main' into bicep-linter
omusavi Sep 22, 2022
4d11c00
Update Bicep CLI command
omusavi Sep 22, 2022
3dd61cb
Quote version regex to avoid escapes and set extra args to array
omusavi Sep 22, 2022
76f455d
missing escape
omusavi Sep 22, 2022
0966221
Set file extensions at linter level
omusavi Sep 22, 2022
04470cc
Merge branch 'main' into bicep-linter
omusavi Sep 27, 2022
53f0e44
Move file_container_regex to ArmLinter block
omusavi Sep 27, 2022
2b340f6
Extract bicep linter into its own descriptor
omusavi Sep 28, 2022
63d941f
Generated files
omusavi Sep 28, 2022
c191ef2
Move test files and create a bicepconfig file
omusavi Sep 28, 2022
717da99
Merge branch 'main' into bicep-linter
omusavi Sep 28, 2022
f158435
Remove old test file and set cli_executable
omusavi Sep 28, 2022
0779704
Remove config_file_name as there is no configurable flag in the bicep…
omusavi Sep 28, 2022
d71593c
Add additional metadata
omusavi Sep 29, 2022
718dfdf
Missing regex field
omusavi Sep 29, 2022
45e4799
Simplify regex
omusavi Sep 29, 2022
331c5cd
Merge branch 'main' into bicep-linter
nvuillam Oct 2, 2022
60d3a62
Add more errors to bicep-bad
nvuillam Oct 2, 2022
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
10 changes: 10 additions & 0 deletions .automation/generated/linter-links-previews.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"image": null,
"title": "The GNU Bourne-Again Shell"
},
"bicep": {
"description": "Learn how to use Bicep linter.",
"image": "https://learn.microsoft.com/en-us/media/logos/logo-ms-social.png",
"title": "Use Bicep linter - Azure Resource Manager"
},
"bicep_linter": {
"description": "Learn how to use Bicep linter.",
"image": "https://learn.microsoft.com/en-us/media/logos/logo-ms-social.png",
"title": "Use Bicep linter - Azure Resource Manager"
},
"black": {
"description": "The uncompromising Python code formatter. Contribute to psf/black development by creating an account on GitHub.",
"image": "https://repository-images.githubusercontent.com/125266328/48aef880-6cce-11e9-9e3c-3ca0dd3ac138",
Expand Down
99 changes: 99 additions & 0 deletions .automation/test/bicep/bicep_bad_1.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
@description('Web app name.')
@minLength(2)
param webAppName string = 'webApp-${uniqueString(resourceGroup().id)}'

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Describes plan\'s pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/')
@allowed([
'F1'
'D1'
'B1'
'B2'
'B3'
'S1'
'S2'
'S3'
'P1'
'P2'
'P3'
'P4'
])
param sku string = 'F1'

@description('The language stack of the app.')
@allowed([
'.net'
'php'
'node'
'html'
])
param language string = '.net'

@description('Optional Git Repo URL, if empty a \'hello world\' app will be deploy from the Azure-Samples repo')
param repoUrl string = ''

var unusedVarThatShouldError = ''
var appServicePlanName = 'AppServicePlan-${webAppName}'
var gitRepoReference = {
'.net': 'https://github.com/Azure-Samples/app-service-web-dotnet-get-started'
node: 'https://github.com/Azure-Samples/nodejs-docs-hello-world'
php: 'https://github.com/Azure-Samples/php-docs-hello-world'
html: 'https://github.com/Azure-Samples/html-docs-hello-world'
}
var gitRepoUrl = (empty(repoUrl) ? gitRepoReference[language] : repoUrl)
var configReference = {
'.net': {
comments: '.Net app. No additional configuration needed.'
}
html: {
comments: 'HTML app. No additional configuration needed.'
}
php: {
phpVersion: '7.4'
}
node: {
appSettings: [
{
name: 'WEBSITE_NODE_DEFAULT_VERSION'
value: '12.15.0'
}
]
}
}

resource asp 'Microsoft.Web/serverfarms@2021-03-01' = {
name: appServicePlanName
location: location
sku: {
name: sku
}
}

resource webApp 'Microsoft.Web/sites@2021-03-01' = {
name: webAppName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
siteConfig: union(configReference[language],{
minTlsVersion: '1.2'
scmMinTlsVersion: '1.2'
ftpsState: 'FtpsOnly'
})
serverFarmId: asp.id
httpsOnly: true
}
}

resource gitsource 'Microsoft.Web/sites/sourcecontrols@2021-03-01' = {
parent: webApp
name: 'web'
properties: {
repoUrl: gitRepoUrl
branch: 'master'
isManualIntegration: true
}
}
98 changes: 98 additions & 0 deletions .automation/test/bicep/bicep_good_1.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
@description('Web app name.')
@minLength(2)
param webAppName string = 'webApp-${uniqueString(resourceGroup().id)}'

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Describes plan\'s pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/')
@allowed([
'F1'
'D1'
'B1'
'B2'
'B3'
'S1'
'S2'
'S3'
'P1'
'P2'
'P3'
'P4'
])
param sku string = 'F1'

@description('The language stack of the app.')
@allowed([
'.net'
'php'
'node'
'html'
])
param language string = '.net'

@description('Optional Git Repo URL, if empty a \'hello world\' app will be deploy from the Azure-Samples repo')
param repoUrl string = ''

var appServicePlanName = 'AppServicePlan-${webAppName}'
var gitRepoReference = {
'.net': 'https://github.com/Azure-Samples/app-service-web-dotnet-get-started'
node: 'https://github.com/Azure-Samples/nodejs-docs-hello-world'
php: 'https://github.com/Azure-Samples/php-docs-hello-world'
html: 'https://github.com/Azure-Samples/html-docs-hello-world'
}
var gitRepoUrl = (empty(repoUrl) ? gitRepoReference[language] : repoUrl)
var configReference = {
'.net': {
comments: '.Net app. No additional configuration needed.'
}
html: {
comments: 'HTML app. No additional configuration needed.'
}
php: {
phpVersion: '7.4'
}
node: {
appSettings: [
{
name: 'WEBSITE_NODE_DEFAULT_VERSION'
value: '12.15.0'
}
]
}
}

resource asp 'Microsoft.Web/serverfarms@2021-03-01' = {
name: appServicePlanName
location: location
sku: {
name: sku
}
}

resource webApp 'Microsoft.Web/sites@2021-03-01' = {
name: webAppName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
siteConfig: union(configReference[language],{
minTlsVersion: '1.2'
scmMinTlsVersion: '1.2'
ftpsState: 'FtpsOnly'
})
serverFarmId: asp.id
httpsOnly: true
}
}

resource gitsource 'Microsoft.Web/sites/sourcecontrols@2021-03-01' = {
parent: webApp
name: 'web'
properties: {
repoUrl: gitRepoUrl
branch: 'master'
isManualIntegration: true
}
}
13 changes: 13 additions & 0 deletions .automation/test/bicep/bicepconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"analyzers": {
"core": {
"enabled": true,
"verbose": true,
"rules": {
"no-unused-vars": {
"level": "error"
}
}
}
}
}
1 change: 1 addition & 0 deletions .github/linters/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
"bestpractices",
"bfseries",
"bibitem",
"bicepconfig",
"bigskip",
"bikeshedding",
"bmod",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Add quotes to arm-ttk linter command ([#1879](https://github.com/oxsecurity/megalinter/issues/1879))
- Improve support for devcontainers by using Python base image
- Fixed Python version in devcontainer from 3.9 -> 3.10
- Add [bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter) linter
- Add Makefile linter in [java flavor](https://oxsecurity.github.io/megalinter/latest/flavors/java/)
- Fix build command on linux (thanks a lot to [Edouard Choinière](https://github.com/echoix) for the investigation and solution !)

Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ARG PWSH_DIRECTORY='/opt/microsoft/powershell'
ARG ARM_TTK_NAME='master.zip'
ARG ARM_TTK_URI='https://github.com/Azure/arm-ttk/archive/master.zip'
ARG ARM_TTK_DIRECTORY='/opt/microsoft'
ARG BICEP_EXE='bicep'
ARG BICEP_URI='https://github.com/Azure/bicep/releases/latest/download/bicep-linux-musl-x64'
ARG BICEP_DIR='/usr/local/bin'
ARG DART_VERSION='2.8.4'
ARG GLIBC_VERSION='2.31-r0'
ARG PMD_VERSION=6.48.0
Expand Down Expand Up @@ -398,6 +401,11 @@ RUN curl --retry 5 --retry-delay 5 -sLO "${ARM_TTK_URI}" \
# shfmt installation
# Managed with COPY --from=shfmt /bin/shfmt /usr/bin/

# bicep_linter installation
&& curl --retry 5 --retry-delay 5 -sLo ${BICEP_EXE} "${BICEP_URI}" \
&& chmod +x "${BICEP_EXE}" \
&& mv "${BICEP_EXE}" "${BICEP_DIR}" \

# clj-kondo installation
# Managed with COPY --from=clj-kondo /bin/clj-kondo /usr/bin/

Expand Down
8 changes: 8 additions & 0 deletions flavors/dotnet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ ARG PWSH_DIRECTORY='/opt/microsoft/powershell'
ARG ARM_TTK_NAME='master.zip'
ARG ARM_TTK_URI='https://github.com/Azure/arm-ttk/archive/master.zip'
ARG ARM_TTK_DIRECTORY='/opt/microsoft'
ARG BICEP_EXE='bicep'
ARG BICEP_URI='https://github.com/Azure/bicep/releases/latest/download/bicep-linux-musl-x64'
ARG BICEP_DIR='/usr/local/bin'
ARG PSSA_VERSION='latest'
#ARG__END

Expand Down Expand Up @@ -278,6 +281,11 @@ RUN curl --retry 5 --retry-delay 5 -sLO "${ARM_TTK_URI}" \
# shfmt installation
# Managed with COPY --from=shfmt /bin/shfmt /usr/bin/

# bicep_linter installation
&& curl --retry 5 --retry-delay 5 -sLo ${BICEP_EXE} "${BICEP_URI}" \
&& chmod +x "${BICEP_EXE}" \
&& mv "${BICEP_EXE}" "${BICEP_DIR}" \

# dotnet-format installation
&& /usr/share/dotnet/dotnet tool install -g dotnet-format \

Expand Down
1 change: 1 addition & 0 deletions flavors/dotnet/flavor.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"BASH_EXEC",
"BASH_SHELLCHECK",
"BASH_SHFMT",
"BICEP_BICEP_LINTER",
"C_CPPLINT",
"COPYPASTE_JSCPD",
"CPP_CPPLINT",
Expand Down
1 change: 1 addition & 0 deletions megalinter/descriptors/all_flavors.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"BASH_EXEC",
"BASH_SHELLCHECK",
"BASH_SHFMT",
"BICEP_BICEP_LINTER",
"C_CPPLINT",
"COPYPASTE_JSCPD",
"CPP_CPPLINT",
Expand Down
45 changes: 45 additions & 0 deletions megalinter/descriptors/bicep.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
descriptor_id: BICEP
descriptor_type: tooling_format
descriptor_flavors:
- dotnet
linters:
- cli_help_arg_name: --help
cli_executable: bicep
cli_lint_errors_count: "\\.bicep\\(\\d+,\\d+\\) : Error "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the properties are cli_lint_errors_count and cli_lint_errors_regex :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli_lint_error_count can be

  • regex_sum ( sum all the numbers found by regex in logs)
  • regex_number (find a single number with regex in logs)
  • regex_count (count the number of times the regex is matched in the log)

cli_lint_extra_args:
- "build"
cli_lint_mode: file
cli_version_arg_name: --version
file_extensions:
- ".bicep"
linter_image_url: https://raw.githubusercontent.com/Azure/bicep/main/docs/images/BicepLogoImage.png
linter_name: bicep_linter
linter_text: |
By default, Bicep linter errors are set as warnings. To customize linter settings,
use a `bicepconfig.json` file. For more information, see the [documentation for the Bicep Linter](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-config-linter#customize-linter)
linter_repo: https://github.com/Azure/bicep
linter_rules_configuration_url: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-config
linter_rules_inline_disable_url: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter#silencing-false-positives
linter_rules_url: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter#default-rules
linter_url: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter
version_extract_regex: "Bicep CLI version (\\d+)\\.(\\d+)\\.(\\d+)"
examples:
- |
# Bicep CLI
bicep build infra.bicep;

# Azure CLI
az bicep build -f infra.bicep
install:
dockerfile:
- ARG BICEP_EXE='bicep'
- ARG BICEP_URI='https://github.com/Azure/bicep/releases/latest/download/bicep-linux-musl-x64'
- ARG BICEP_DIR='/usr/local/bin'
- |
RUN curl --retry 5 --retry-delay 5 -sLo ${BICEP_EXE} "${BICEP_URI}" \
&& chmod +x "${BICEP_EXE}" \
&& mv "${BICEP_EXE}" "${BICEP_DIR}"
ide:
vscode:
- name: VSCode Bicep
url: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ANSIBLE",
"ARM",
"BASH",
"BICEP",
"C",
"CLOJURE",
"CLOUDFORMATION",
Expand Down Expand Up @@ -109,6 +110,7 @@
"BASH_EXEC",
"BASH_SHELLCHECK",
"BASH_SHFMT",
"BICEP_BICEP_LINTER",
"C_CPPLINT",
"CLOJURE_CLJ_KONDO",
"CLOUDFORMATION_CFN_LINT",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# !/usr/bin/env python3
"""
Unit tests for BICEP linter bicep_linter
This class has been automatically @generated by .automation/build.py, please do not update it manually
"""

from unittest import TestCase

from megalinter.tests.test_megalinter.LinterTestRoot import LinterTestRoot


class bicep_bicep_linter_test(TestCase, LinterTestRoot):
descriptor_id = "BICEP"
linter_name = "bicep_linter"