Skip to content

Instantly share code, notes, and snippets.

@YSaxon
Forked from snyk-omar/add-sast-tags.py
Created June 7, 2022 20:36
Show Gist options
  • Save YSaxon/b623e8f2a76a0f70fc290eed9ae7d1a5 to your computer and use it in GitHub Desktop.
Save YSaxon/b623e8f2a76a0f70fc290eed9ae7d1a5 to your computer and use it in GitHub Desktop.
Add tags to SAST projects in Snyk.
#! /usr/bin/env python3
"""
Need to install httpx and python-dotenv to run this script.
```
pip install httpx python-dotenv
```
Additionally, you will need a .env file with two variables in it:
GROUP_ID = (your group id)
AUTH_TOKEN = (your auth token)
Afterwards, use any Python version above 3.6, and run this script.
It will update the Snyk Code projects in Snyk to have the sast tag.
Once this is run, go into the UI and click on the tags filter in the
projects page (left-hand menu). Select the type tag and sast as the key.
All of your Snyk Code projects will be shown via this filter.
"""
import logging
import os
import httpx
from dotenv import load_dotenv
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
)
def get_org_ids(token: str, group_id: str) -> list:
org_ids = []
with httpx.Client(
base_url="https://snyk.io/api/v1", headers={"Authorization": f"token {token}"}
) as client:
orgs = client.get(f"group/{group_id}/orgs").json()
for org in orgs.get("orgs"):
org_ids.append(org["id"])
return org_ids
def apply_tags_to_sast_projects(token: str, org_ids: list) -> None:
with httpx.Client(
base_url="https://snyk.io/api/v1", headers={"Authorization": f"token {token}"}
) as client:
for org_id in org_ids:
projects = client.post(f"org/{org_id}/projects").json()
for project in projects.get("projects"):
if project["type"] == "sast":
req = client.post(
f"org/{org_id}/project/{project['id']}/tags",
data={"key": "type", "value": "sast"},
)
logging.info(req.status_code, req.json())
def main():
# Load variables from configuration file
load_dotenv()
group_id = os.getenv("GROUP_ID")
token = os.getenv("AUTH_TOKEN")
logging.info(
"This script will add the sast tag to every Snyk Code project in Snyk for easy filtering via the UI"
)
org_ids = get_org_ids(token, group_id)
apply_tags_to_sast_projects(token, org_ids)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment