Skip to content

Commit c5bf8ec

Browse files
Better error handling (#45)
* Raise exceptions when API response is not ok * Config for local super-linter, fix lint grumbles * Fix action ref * Add error handling to more files * Fix lint grumbles AGAIN --------- Co-authored-by: Natalie Somersall <some-natalie@github.com>
1 parent 3235602 commit c5bf8ec

File tree

8 files changed

+118
-10
lines changed

8 files changed

+118
-10
lines changed

.github/linters/super-linter.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
VALIDATE_ALL_CODEBASE=false
2+
VALIDATE_DOCKERFILE_HADOLINT=true
3+
VALIDATE_GITHUB_ACTIONS=true
4+
VALIDATE_MARKDOWN=true
5+
MARKDOWN_CONFIG_FILE=.markdownlint.json
6+
VALIDATE_PYTHON_BLACK=true

.github/workflows/linter.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ jobs:
3030
# Set the agent to run on
3131
runs-on: ubuntu-latest
3232

33+
############################################
34+
# Grant status permission for MULTI_STATUS #
35+
############################################
36+
permissions:
37+
contents: read
38+
packages: read
39+
statuses: write
40+
3341
##################
3442
# Load all steps #
3543
##################
@@ -43,17 +51,14 @@ jobs:
4351
# Full git history is needed to get a proper list of changed files within `super-linter`
4452
fetch-depth: 0
4553

54+
- name: Setup env
55+
run: cat .github/linters/super-linter.env >> "$GITHUB_ENV"
56+
4657
################################
4758
# Run Linter against code base #
4859
################################
4960
- name: Lint Code Base
5061
uses: super-linter/super-linter/slim@v5
5162
env:
52-
VALIDATE_ALL_CODEBASE: false
53-
DEFAULT_BRANCH: main
5463
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55-
VALIDATE_DOCKERFILE_HADOLINT: true
56-
VALIDATE_GITHUB_ACTIONS: true
57-
VALIDATE_MARKDOWN: true
58-
MARKDOWN_CONFIG_FILE: .markdownlint.json
59-
VALIDATE_PYTHON_BLACK: true
64+
DEFAULT_BRANCH: main

.vscode/launch.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal",
13+
"justMyCode": true,
14+
"env": {
15+
"GITHUB_PAT": "${input:gh_pat}",
16+
"SCOPE_NAME": "ctcampbell/webgoat"
17+
}
18+
}
19+
],
20+
"inputs": [
21+
{
22+
"id": "gh_pat",
23+
"type": "promptString",
24+
"description": "Enter your GitHub PAT",
25+
"password": true
26+
}
27+
]
28+
}

.vscode/tasks.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "super-linter",
6+
"command": "docker",
7+
"args": [
8+
"run",
9+
"--rm",
10+
"-e", "RUN_LOCAL=true",
11+
"--env-file", ".github/linters/super-linter.env",
12+
"-v", "${workspaceFolder}:/tmp/lint",
13+
"github/super-linter:slim-v5"
14+
]
15+
}
16+
]
17+
}

src/code_scanning.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def list_repo_code_scanning_alerts(api_endpoint, github_pat, repo_name):
3131
return "need permission to access,{}".format(repo_name) # don't have permission
3232
if response.status_code == 403:
3333
return "need to enable GHAS,{}".format(repo_name) # no GHAS
34+
if not response.ok:
35+
raise Exception(
36+
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
37+
)
3438
response_json = response.json()
3539
while "next" in response.links.keys():
3640
response = requests.get(response.links["next"]["url"], headers=headers)
@@ -132,6 +136,10 @@ def list_org_code_scanning_alerts(api_endpoint, github_pat, org_name):
132136
"Accept": "application/vnd.github.v3+json",
133137
}
134138
response = requests.get(url, headers=headers)
139+
if not response.ok:
140+
raise Exception(
141+
"API error,{},{},{}".format(org_name, response.status_code, response.text)
142+
)
135143
response_json = response.json()
136144
while "next" in response.links.keys():
137145
response = requests.get(response.links["next"]["url"], headers=headers)
@@ -359,6 +367,12 @@ def list_enterprise_cloud_code_scanning_alerts(
359367
"Accept": "application/vnd.github.v3+json",
360368
}
361369
response = requests.get(url, headers=headers)
370+
if not response.ok:
371+
raise Exception(
372+
"API error,{},{},{}".format(
373+
enterprise_slug, response.status_code, response.text
374+
)
375+
)
362376
response_json = response.json()
363377
while "next" in response.links.keys():
364378
response = requests.get(response.links["next"]["url"], headers=headers)

src/dependabot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def list_repo_dependabot_alerts(api_endpoint, github_pat, repo_name):
2929
"Accept": "application/vnd.github+json",
3030
},
3131
)
32+
if not response.ok:
33+
raise Exception(
34+
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
35+
)
3236
response_json = response.json()
3337
while "next" in response.links.keys():
3438
response = requests.get(
@@ -133,6 +137,10 @@ def list_org_dependabot_alerts(api_endpoint, github_pat, org_name):
133137
"Accept": "application/vnd.github+json",
134138
},
135139
)
140+
if not response.ok:
141+
raise Exception(
142+
"API error,{},{},{}".format(org_name, response.status_code, response.text)
143+
)
136144
response_json = response.json()
137145
while "next" in response.links.keys():
138146
response = requests.get(
@@ -172,6 +180,12 @@ def list_enterprise_dependabot_alerts(api_endpoint, github_pat, enterprise_slug)
172180
"Accept": "application/vnd.github+json",
173181
},
174182
)
183+
if not response.ok:
184+
raise Exception(
185+
"API error,{},{},{}".format(
186+
enterprise_slug, response.status_code, response.text
187+
)
188+
)
175189
response_json = response.json()
176190
while "next" in response.links.keys():
177191
response = requests.get(

src/enterprise.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ def get_enterprise_version(api_endpoint):
1818
if api_endpoint != "https://api.github.com":
1919
url = "{}/meta".format(api_endpoint)
2020
response = requests.get(url)
21+
if not response.ok:
22+
raise Exception(
23+
"API error,{},{},{}".format(
24+
api_endpoint, response.status_code, response.text
25+
)
26+
)
2127
if "installed_version" in response.json():
2228
return response.json()["installed_version"]
2329
else:
@@ -36,6 +42,10 @@ def get_repo_report(url, github_pat):
3642
}
3743
url = "{}/stafftools/reports/all_repositories.csv".format(url)
3844
response = requests.get(url, headers=headers)
45+
if not response.ok:
46+
raise Exception(
47+
"API error,{},{},{}".format(url, response.status_code, response.text)
48+
)
3949
if response.status_code == 202: # report needs to be generated
4050
while response.status_code == 202:
4151
print("Waiting a minute for the report to be generated ...")

src/secret_scanning.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ def get_repo_secret_scanning_alerts(api_endpoint, github_pat, repo_name):
2929
"Accept": "application/vnd.github.v3+json",
3030
},
3131
)
32-
response_json = response.json()
3332
# The secret scanning API returns a code of 404 if there are no alerts,
3433
# secret scanning is disabled, or the repository is public.
3534
if response.status_code == 404:
3635
return ["not found"]
36+
if not response.ok:
37+
raise Exception(
38+
"API error,{},{},{}".format(repo_name, response.status_code, response.text)
39+
)
40+
response_json = response.json()
3741
while "next" in response.links.keys():
3842
response = requests.get(
3943
response.links["next"]["url"],
@@ -132,11 +136,15 @@ def get_org_secret_scanning_alerts(api_endpoint, github_pat, org_name):
132136
"Accept": "application/vnd.github.v3+json",
133137
},
134138
)
135-
response_json = response.json()
136139
# The secret scanning API returns a code of 404 if there are no alerts,
137140
# secret scanning is disabled, or the repository is public.
138141
if response.status_code == 404:
139142
return ["not found"]
143+
if not response.ok:
144+
raise Exception(
145+
"API error,{},{},{}".format(org_name, response.status_code, response.text)
146+
)
147+
response_json = response.json()
140148
while "next" in response.links.keys():
141149
response = requests.get(
142150
response.links["next"]["url"],
@@ -250,11 +258,17 @@ def get_enterprise_secret_scanning_alerts(api_endpoint, github_pat, enterprise_s
250258
"Accept": "application/vnd.github.v3+json",
251259
},
252260
)
253-
response_json = response.json()
254261
# The secret scanning API returns a code of 404 if there are no alerts,
255262
# secret scanning is disabled, or the repository is public.
256263
if response.status_code == 404:
257264
return ["not found"]
265+
if not response.ok:
266+
raise Exception(
267+
"API error,{},{},{}".format(
268+
enterprise_slug, response.status_code, response.text
269+
)
270+
)
271+
response_json = response.json()
258272
while "next" in response.links.keys():
259273
response = requests.get(
260274
response.links["next"]["url"],

0 commit comments

Comments
 (0)