Skip to content

Commit f5e3f2f

Browse files
committed
Scope issue creation and deletion within provided 'category'
Prevents Snyk scans from removing issues created by other Snyk runs.
1 parent 141fbea commit f5e3f2f

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

entrypoint.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ def create_new_issues(
5050
time.sleep(random.randint(pause_lowerbound_sec, pause_upperbound_sec)) # to prevent rate limiting
5151
title = '{} {}'.format(vulnerability['title'], vulnerability['id'])
5252
issues_found_in_current_run.add(title)
53-
issues_found_in_current_run.add(title)
53+
# When an issue is already open (for a specific category when provided), we don't want to create a new one.
5454
if title in open_issues:
55-
continue
55+
if category is None:
56+
continue
57+
issues_with_category = [issue for issue in open_issues[title] if issue.get("category") == category]
58+
if len(issues_with_category) > 0:
59+
continue
5660
payload = {
5761
'title': title,
5862
'body': vulnerability['description'],
@@ -61,7 +65,7 @@ def create_new_issues(
6165
],
6266
}
6367
if category is not None:
64-
payload['labels'].append(category)
68+
payload['labels'].append(f"Snyk-category:{category}")
6569
r = requests.post(url, data=json.dumps(payload), headers=headers)
6670
if r.ok:
6771
new_issue = r.json()
@@ -72,6 +76,7 @@ def create_new_issues(
7276
'title': title,
7377
'url': new_issue['url'],
7478
'number': new_issue['number'],
79+
'category': category,
7580
'timestamp': timestamp,
7681
}]
7782
else:
@@ -87,15 +92,13 @@ def close_old_issues(
8792
headers: Headers,
8893
):
8994
for title, open_issue_list in open_issues.items():
90-
if title not in found_issues:
91-
for open_issue in open_issue_list:
92-
# Only close issues when the open issue is not of the same meta Snyk run
93-
# i.e. does not have the same Snyk-timestamp label (only if provided).
95+
# Only check issues for the category when defined (ternary operator)
96+
open_issues = open_issue_list if category is None else [issue for issue in open_issue_list if issue.get("category") == category]
97+
for open_issue in open_issues:
98+
if open_issue.get("title") not in found_issues:
9499
time.sleep(random.randint(pause_lowerbound_sec, pause_upperbound_sec)) # to prevent rate limiting
95-
if (timestamp is not None and
96-
timestamp is not open_issue.get('timestamp')):
97-
print(f"Closing issue {open_issue['title']} - #{open_issue['number']}")
98-
close_issue(open_issue, headers)
100+
print(f"Closing issue {open_issue['title']} - #{open_issue['number']}")
101+
close_issue(open_issue, headers)
99102

100103

101104
def fetch_open_issues(url: str, headers: Headers) -> OpenIssues:
@@ -138,14 +141,19 @@ def fetch_open_issues(url: str, headers: Headers) -> OpenIssues:
138141
open_issues[title] = []
139142
# Get the timestamp label if it exists
140143
issue_timestamp = None
144+
issue_category = None
141145
for label in open_issue['labels']:
142146
if label['name'].startswith('Snyk-timestamp:'):
143147
issue_timestamp = label['name'].split(':')[1]
144148
break
149+
if label['name'].startswith('Snyk-category:'):
150+
issue_category = label['name'].split(':')[1]
151+
break
145152
open_issues[title].append({
146153
'title': open_issue['title'],
147154
'url': open_issue['url'],
148155
'number': open_issue['number'],
156+
'category': issue_category,
149157
'timestamp': issue_timestamp,
150158
})
151159
next_link = r.links.get('next')
@@ -160,8 +168,8 @@ def fetch_open_issues(url: str, headers: Headers) -> OpenIssues:
160168
def main():
161169
parser = argparse.ArgumentParser(description="Parse the report file")
162170
parser.add_argument("report_file", type=str, help="a name of the report file")
163-
parser.add_argument("-t", "--timestamp", type=str, help="a timestamp of the run. When passed, will cause combine results of multiple runs when the runs have the same timestamp.", default=datetime.now(), required=False)
164-
parser.add_argument("-c", "--category", type=str, help="a category label to add to the issue.", required=False)
171+
parser.add_argument("-c", "--category", type=str, help="category of the Snyk run. When defined, reported issues will be scoped within the category i.e., will not be closed by scans running on other categories. Also added as label to created issues.", required=False)
172+
parser.add_argument("-t", "--timestamp", type=str, help="a timestamp of the run (added as label to ceated issues)", default=datetime.now(), required=False)
165173
args = parser.parse_args()
166174

167175
global timestamp

0 commit comments

Comments
 (0)