Scan GitHub repository via Deepbits Platform
When you have a deepbits account and using app.deepbits.com to scan your GitHub repository, you can follow below instructions to scan GitHub Repository programmatically.
Demo python code to scan public GitHub Repository
import requests
import hashlib
import os
import time
# Created Deepbits API Key
API_KEY = ''
API_BASE = 'https://api.deepbits.com/api/v1'
# GitHub Repo Owner You Want To Scan
owner = 'facebook'
# GitHub Repo Name You Want To Scan
repository = 'react'
# GitHub Repo Branch Name
branchName = 'main'
# Created Deepbits Project Id in Step #2
projectId = ''
default_headers = {"x-api-key": API_KEY}
def get_gh_account_id():
response = requests.get(
f"{API_BASE}/github/account",
headers=default_headers,
)
return response.json()['data'][0]['_id']
def add_github_repo_to_project(accountId, owner, repository, projectId):
url = f"{API_BASE}/github/{accountId}/repos/add",
response = requests.post(
f"{API_BASE}/github/{accountId}/repos/add",
headers=default_headers,
json={
"repoIds": [f"pub:::{owner}/{repository}"],
}
)
res = response.json()['data']
addedRepo = res['added'][0]
existingProjectAssets = requests.get(
f"{API_BASE}/project/{projectId}",
headers=default_headers,
);
updatedProjectAssets = existingProjectAssets.json()['data']['assets']
for asset in updatedProjectAssets:
if asset['assetType'] == 'GitHubRepo':
asset['assetIds'].append(addedRepo)
break
projectAfterUpdate = requests.put(
f"{API_BASE}/project/{projectId}",
headers=default_headers,
json={
"name": "Demo Project",
"assets": updatedProjectAssets
}
)
projectAssets = projectAfterUpdate.json()['data']['createdAssets']
for asset in projectAssets:
if 'gitHubRepoId' in asset and asset['gitHubRepoId'] == addedRepo:
projectAssetId = asset['_id']
break
return {
"repoId": addedRepo,
"projectAssetId": projectAssetId
}
def list_github_branches(accountId, repoId):
response = requests.get(
f"{API_BASE}/github/{accountId}/repos/{repoId}/branch",
headers=default_headers,
)
print(response.json())
return response.json()['data']
def watch_github_branch(projectId, projectAssetId, branchName):
print('Watching branch...', branchName)
response = requests.put(
f"{API_BASE}/project/{projectId}/{projectAssetId}/stream_watch",
headers=default_headers,
json={
"action": "watch",
"identifier": branchName,
}
)
print(response.json())
return response.json()['data']['_id']
def get_stream_scan_detail(projectId, assetId, streamId):
response = requests.get(
f"{API_BASE}/project/{projectId}/{assetId}/{streamId}/scan_result",
headers=default_headers,
)
return response.json()['data']
print(f"ProjectId: {projectId}")
ghAccountId = get_gh_account_id()
print(f"ghAccountId: {ghAccountId}")
addedRepoData = add_github_repo_to_project(ghAccountId, owner, repository, projectId)
print("Added repo data:")
print(addedRepoData)
branches = list_github_branches(ghAccountId, addedRepoData['repoId'])
print(f"\nBranchesData: {branches}")
watchedStreamId = watch_github_branch(projectId, addedRepoData['projectAssetId'], branchName)
print(f"\nWatchedStreamId: {watchedStreamId}")
scanResultDetail = get_stream_scan_detail(projectId, addedRepoData['projectAssetId'], watchedStreamId)
# Do not print scanResult field since it's too big for print
filtered_scan_result_detail = {k: v for k, v in scanResultDetail.items() if k != 'scanResult'}
print(f"\nScanResultDetail: {filtered_scan_result_detail}")
print("\nDemo run successfully without error, get API docs on https://docs.deepbits.com/reference/post_github-repos-add")
Notes
This guide will show you how to generate your API KEY.
Updated about 1 year ago