generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add actionlint to workflow linter (#39)
* adding actionlint to default workflow-linter rules * setting fail level to warning at first * adding error messages * catching windows installations * Catching FileNotFound exception * Adding filename for action lint to run against * adding a test for actionlint * adding error message for missing filename * apply black * trying actionlint from source * importing shutil * separated installation into a separate function * added an additional test * removing failure if actionlint fails * Improve the test coverage for run_actionlint rule * catching unknown error * adding package manager to requirements --------- Co-authored-by: Andy Pixley <[email protected]> Co-authored-by: Opeyemi Alao <[email protected]>
- Loading branch information
1 parent
8fc0766
commit bcbdda5
Showing
11 changed files
with
392 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""A Rule to run actionlint on workflows.""" | ||
|
||
from typing import Optional, Tuple | ||
import subprocess | ||
import platform | ||
import urllib.request | ||
import os | ||
|
||
from ..rule import Rule | ||
from ..models.workflow import Workflow | ||
from ..utils import LintLevels, Settings | ||
|
||
|
||
def install_actionlint(platform_system: str) -> Tuple[bool, str]: | ||
"""If actionlint is not installed, detects OS platform | ||
and installs actionlint""" | ||
|
||
error = f"An error occurred when installing Actionlint on {platform_system}" | ||
|
||
if platform_system.startswith("Linux"): | ||
return install_actionlint_source(error) | ||
elif platform_system == "Darwin": | ||
try: | ||
subprocess.run(["brew", "install", "actionlint"], check=True) | ||
return True, "" | ||
except (FileNotFoundError, subprocess.CalledProcessError): | ||
return False, f"{error} : check Brew installation" | ||
elif platform_system.startswith("Win"): | ||
try: | ||
subprocess.run(["choco", "install", "actionlint", "-y"], check=True) | ||
return True, "" | ||
except (FileNotFoundError, subprocess.CalledProcessError): | ||
return False, f"{error} : check Choco installation" | ||
return False, error | ||
|
||
|
||
def install_actionlint_source(error) -> Tuple[bool, str]: | ||
"""Install Actionlint Binary from provided script""" | ||
url = "https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash" | ||
version = "1.6.17" | ||
request = urllib.request.urlopen(url) | ||
with open("download-actionlint.bash", "wb+") as fp: | ||
fp.write(request.read()) | ||
try: | ||
subprocess.run(["bash", "download-actionlint.bash", version], check=True) | ||
return True, os.getcwd() | ||
except (FileNotFoundError, subprocess.CalledProcessError): | ||
return False, error | ||
|
||
|
||
def check_actionlint(platform_system: str) -> Tuple[bool, str]: | ||
"""Check if the actionlint is in the system's PATH.""" | ||
try: | ||
subprocess.run( | ||
["actionlint", "--version"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
check=True, | ||
) | ||
return True, "" | ||
except subprocess.CalledProcessError: | ||
return ( | ||
False, | ||
"Failed to install Actionlint, \ | ||
please check your package installer or manually install it", | ||
) | ||
except FileNotFoundError: | ||
return install_actionlint(platform_system) | ||
|
||
|
||
class RunActionlint(Rule): | ||
"""Rule to run actionlint as part of workflow linter V2.""" | ||
|
||
def __init__(self, settings: Optional[Settings] = None) -> None: | ||
self.message = "Actionlint must pass without errors" | ||
self.on_fail = LintLevels.WARNING | ||
self.compatibility = [Workflow] | ||
self.settings = settings | ||
|
||
def fn(self, obj: Workflow) -> Tuple[bool, str]: | ||
if not obj or not obj.filename: | ||
raise AttributeError( | ||
"Running actionlint without a filename is not currently supported" | ||
) | ||
|
||
installed, location = check_actionlint(platform.system()) | ||
if installed: | ||
if location: | ||
result = subprocess.run( | ||
[location + "/actionlint", obj.filename], | ||
capture_output=True, | ||
text=True, | ||
check=False, | ||
) | ||
else: | ||
result = subprocess.run( | ||
["actionlint", obj.filename], | ||
capture_output=True, | ||
text=True, | ||
check=False, | ||
) | ||
if result.returncode == 1: | ||
return False, result.stdout | ||
if result.returncode > 1: | ||
return False, result.stdout | ||
return True, "" | ||
else: | ||
return False, self.message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: test | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
|
||
jobs: | ||
job-key: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Test | ||
run: echo test | ||
|
||
call-workflow: | ||
uses: bitwarden/server/.github/workflows/workflow-linter.yml@master | ||
|
||
test-normal-action: | ||
name: Download Latest | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b | ||
|
||
- run: | | ||
echo test | ||
test-local-action: | ||
name: Testing a local action call | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: local-action | ||
uses: ./version-bump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: test | ||
on: | ||
push: | ||
branches: | ||
- | ||
path: | ||
- "src/**" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
job-key: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Test | ||
run: echo test | ||
|
||
call-workflow: | ||
uses: bitwarden/server/.github/workflows/workflow-linter.yml@master | ||
|
||
test-normal-action: | ||
name: Download Latest | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b | ||
|
||
- run: | | ||
echo test | ||
test-local-action: | ||
name: Testing a local action call | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: local-action | ||
uses: ./version-bump |
Oops, something went wrong.