Skip to content

Better error handling #45

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

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .github/linters/super-linter.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
VALIDATE_ALL_CODEBASE=false
VALIDATE_DOCKERFILE_HADOLINT=true
VALIDATE_GITHUB_ACTIONS=true
VALIDATE_MARKDOWN=true
MARKDOWN_CONFIG_FILE=.markdownlint.json
VALIDATE_PYTHON_BLACK=true
19 changes: 12 additions & 7 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ jobs:
# Set the agent to run on
runs-on: ubuntu-latest

############################################
# Grant status permission for MULTI_STATUS #
############################################
permissions:
contents: read
packages: read
statuses: write

##################
# Load all steps #
##################
Expand All @@ -43,17 +51,14 @@ jobs:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0

- name: Setup env
run: cat .github/linters/super-linter.env >> "$GITHUB_ENV"

################################
# Run Linter against code base #
################################
- name: Lint Code Base
uses: super-linter/super-linter/slim@v5
env:
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_DOCKERFILE_HADOLINT: true
VALIDATE_GITHUB_ACTIONS: true
VALIDATE_MARKDOWN: true
MARKDOWN_CONFIG_FILE: .markdownlint.json
VALIDATE_PYTHON_BLACK: true
DEFAULT_BRANCH: main
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"GITHUB_PAT": "${input:gh_pat}",
"SCOPE_NAME": "ctcampbell/webgoat"
}
}
],
"inputs": [
{
"id": "gh_pat",
"type": "promptString",
"description": "Enter your GitHub PAT",
"password": true
}
]
}
17 changes: 17 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "super-linter",
"command": "docker",
"args": [
"run",
"--rm",
"-e", "RUN_LOCAL=true",
"--env-file", ".github/linters/super-linter.env",
"-v", "${workspaceFolder}:/tmp/lint",
"github/super-linter:slim-v5"
]
}
]
}
14 changes: 14 additions & 0 deletions src/code_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def list_repo_code_scanning_alerts(api_endpoint, github_pat, repo_name):
return "need permission to access,{}".format(repo_name) # don't have permission
if response.status_code == 403:
return "need to enable GHAS,{}".format(repo_name) # no GHAS
if not response.ok:
raise Exception(
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(response.links["next"]["url"], headers=headers)
Expand Down Expand Up @@ -132,6 +136,10 @@ def list_org_code_scanning_alerts(api_endpoint, github_pat, org_name):
"Accept": "application/vnd.github.v3+json",
}
response = requests.get(url, headers=headers)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(org_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(response.links["next"]["url"], headers=headers)
Expand Down Expand Up @@ -359,6 +367,12 @@ def list_enterprise_cloud_code_scanning_alerts(
"Accept": "application/vnd.github.v3+json",
}
response = requests.get(url, headers=headers)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(
enterprise_slug, response.status_code, response.text
)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(response.links["next"]["url"], headers=headers)
Expand Down
14 changes: 14 additions & 0 deletions src/dependabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def list_repo_dependabot_alerts(api_endpoint, github_pat, repo_name):
"Accept": "application/vnd.github+json",
},
)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
Expand Down Expand Up @@ -133,6 +137,10 @@ def list_org_dependabot_alerts(api_endpoint, github_pat, org_name):
"Accept": "application/vnd.github+json",
},
)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(org_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
Expand Down Expand Up @@ -172,6 +180,12 @@ def list_enterprise_dependabot_alerts(api_endpoint, github_pat, enterprise_slug)
"Accept": "application/vnd.github+json",
},
)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(
enterprise_slug, response.status_code, response.text
)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
Expand Down
10 changes: 10 additions & 0 deletions src/enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def get_enterprise_version(api_endpoint):
if api_endpoint != "https://api.github.com":
url = "{}/meta".format(api_endpoint)
response = requests.get(url)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(
api_endpoint, response.status_code, response.text
)
)
if "installed_version" in response.json():
return response.json()["installed_version"]
else:
Expand All @@ -36,6 +42,10 @@ def get_repo_report(url, github_pat):
}
url = "{}/stafftools/reports/all_repositories.csv".format(url)
response = requests.get(url, headers=headers)
if not response.ok:
raise Exception(
"API error,{},{},{}".format(url, response.status_code, response.text)
)
if response.status_code == 202: # report needs to be generated
while response.status_code == 202:
print("Waiting a minute for the report to be generated ...")
Expand Down
20 changes: 17 additions & 3 deletions src/secret_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ def get_repo_secret_scanning_alerts(api_endpoint, github_pat, repo_name):
"Accept": "application/vnd.github.v3+json",
},
)
response_json = response.json()
# The secret scanning API returns a code of 404 if there are no alerts,
# secret scanning is disabled, or the repository is public.
if response.status_code == 404:
return ["not found"]
if not response.ok:
raise Exception(
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
response.links["next"]["url"],
Expand Down Expand Up @@ -132,11 +136,15 @@ def get_org_secret_scanning_alerts(api_endpoint, github_pat, org_name):
"Accept": "application/vnd.github.v3+json",
},
)
response_json = response.json()
# The secret scanning API returns a code of 404 if there are no alerts,
# secret scanning is disabled, or the repository is public.
if response.status_code == 404:
return ["not found"]
if not response.ok:
raise Exception(
"API error,{},{},{}".format(org_name, response.status_code, response.text)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
response.links["next"]["url"],
Expand Down Expand Up @@ -250,11 +258,17 @@ def get_enterprise_secret_scanning_alerts(api_endpoint, github_pat, enterprise_s
"Accept": "application/vnd.github.v3+json",
},
)
response_json = response.json()
# The secret scanning API returns a code of 404 if there are no alerts,
# secret scanning is disabled, or the repository is public.
if response.status_code == 404:
return ["not found"]
if not response.ok:
raise Exception(
"API error,{},{},{}".format(
enterprise_slug, response.status_code, response.text
)
)
response_json = response.json()
while "next" in response.links.keys():
response = requests.get(
response.links["next"]["url"],
Expand Down