Skip to content

Commit

Permalink
Merge pull request #11 from Shoreasg/v5.0.0
Browse files Browse the repository at this point in the history
V5.0.0
  • Loading branch information
Shoreasg authored Aug 20, 2024
2 parents bd44ebc + 0e79b31 commit 3a2631e
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 25 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Check if you have install correctly by running:

1. GET all organizations in a group
2. GET all integrations for all organizations in a group.
3. GET all issue count for all organizations in a group.

## DELETE API

Expand Down Expand Up @@ -83,4 +84,12 @@ Get all organizations in a group and check their snyk code settings, set all to
## Required flags

1. `--group_id`: Your group id
2. `--sast_enabled`: Enable / Disable Snyk Code
2. `--sast_enabled`: Enable / Disable Snyk Code

# `snyk-rest-cli --get_all_org_issues`

Get all issues counts for all organizations in a group for all severity.

## Required flags

1. `--group_id`: Your group id
112 changes: 100 additions & 12 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
paginationForDeleteEmptyTargetsInOrg,
paginationForGetAllIntegrationsInOrg,
paginationForGetAllOrgsGroup,
paginationForGetIssuesCount,
} from "./pagination.js";
import { echo, chalk } from "zx";
import { echo, chalk, spinner } from "zx";
let orgIds = [];

export async function getAllIntegrationsInOrg() {
Expand Down Expand Up @@ -283,7 +284,10 @@ export async function deleteEmptyTargets() {

export async function updateSnykCode() {
try {
if (myCustomArgv.sast_enabled === "true" || myCustomArgv.sast_enabled === "false") {
if (
myCustomArgv.sast_enabled === "true" ||
myCustomArgv.sast_enabled === "false"
) {
await getAllOrgsGroup();

if (orgIds && orgIds.length > 0) {
Expand Down Expand Up @@ -334,7 +338,7 @@ export async function updateSnykCode() {
headers: {
accept: "application/vnd.api+json",
authorization: `TOKEN ${myCustomArgv.snyk_token}`,
'Content-Type': 'application/vnd.api+json'
"Content-Type": "application/vnd.api+json",
},
body: JSON.stringify({
data: {
Expand All @@ -346,7 +350,8 @@ export async function updateSnykCode() {
},
}),
}
).then(async (response) => {
)
.then(async (response) => {
if (response.status == 201) {
echo(
`${chalk.greenBright(
Expand Down Expand Up @@ -374,8 +379,8 @@ export async function updateSnykCode() {
)
);
}
} else{
if(data.data.attributes.sast_enabled === true){
} else {
if (data.data.attributes.sast_enabled === true) {
echo(
chalk.greenBright(
`${orgId} has Snyk Code Enabled. Disabling it...`
Expand All @@ -389,7 +394,7 @@ export async function updateSnykCode() {
headers: {
accept: "application/vnd.api+json",
authorization: `TOKEN ${myCustomArgv.snyk_token}`,
'Content-Type': 'application/vnd.api+json'
"Content-Type": "application/vnd.api+json",
},
body: JSON.stringify({
data: {
Expand All @@ -401,7 +406,8 @@ export async function updateSnykCode() {
},
}),
}
).then(async (response) => {
)
.then(async (response) => {
if (response.status == 201) {
echo(
`${chalk.greenBright(
Expand All @@ -422,14 +428,12 @@ export async function updateSnykCode() {
echo(chalk.red(`Update error: ${error.message}`));
console.error(error);
}
} echo(
}
echo(
chalk.yellowBright(
`Organization ID: ${orgId} has Snyk Code Disabled! Skipping Org!`
)
);



}
}
}
Expand All @@ -441,3 +445,87 @@ export async function updateSnykCode() {
console.error(error);
}
}

export async function getIssuesCount() {
try {
await getAllOrgsGroup();

if (orgIds && orgIds.length > 0) {
await Promise.all(
orgIds.map(async (orgId) => {
const response = await fetch(
`https://api.snyk.io/rest/orgs/${orgId}/issues?version=${myCustomArgv.api_version}&limit=100`,
{
method: "GET",
headers: {
accept: "application/vnd.api",
authorization: `Token ${myCustomArgv.snyk_token}`,
},
}
);

if (!response.ok) {
const errorText = await response.json();
echo(
chalk.red(
`HTTP error! Status: ${response.status}, Response: ${JSON.stringify(errorText)}`
)
);
return;
}

const data = await response.json();
const issues = data.data;

let lowIssueCount = 0;
let mediumIssueCount = 0;
let highIssueCount = 0;
let criticalIssueCount = 0;

await (spinner(chalk.red(`Calculating number of issues for org ${orgId}`),async()=>{
for (const issue of issues) {
switch (issue.attributes.effective_severity_level) {
case "low":
lowIssueCount++;
break;
case "medium":
mediumIssueCount++;
break;
case "high":
highIssueCount++;
break;
case "critical":
criticalIssueCount++;
break;
}
}

if (data.links.next) {
await paginationForGetIssuesCount(
data.links.next,
orgId,
lowIssueCount,
mediumIssueCount,
highIssueCount,
criticalIssueCount
);
} else {
echo(
chalk.green(`Organization ID: ${orgId} has the following issues counts`)
);
echo(chalk.yellow(`Low Issues Counts: ${lowIssueCount}`));
echo(chalk.yellow(`Medium Issues Counts: ${mediumIssueCount}`));
echo(chalk.yellow(`High Issues Counts: ${highIssueCount}`));
echo(chalk.yellow(`Critical Issues Counts: ${criticalIssueCount}`));
}
}))


})
);
}
} catch (error) {
echo(`Fetch error: ${error.message}`);
console.error(error);
}
}
3 changes: 2 additions & 1 deletion helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const myCustomArgv = minimist(process.argv.slice(2), {
"v",
"get_all_integrations_org",
"delete_empty_targets",
"update_snyk_code_orgs"
"update_snyk_code_orgs",
"get_all_orgs_issues"
],
});
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
import { echo } from "zx";
import { myCustomArgv, readHelp } from "./helper.js";
import { deleteEmptyTargets, getAllIntegrationsInOrg, getAllOrgsGroup, updateSnykCode } from "./api.js";
import { deleteEmptyTargets, getAllIntegrationsInOrg, getAllOrgsGroup, getIssuesCount, updateSnykCode } from "./api.js";

const cliVersion = "4.0.3";
const cliVersion = "5.0.0";


(async () => {
Expand All @@ -19,6 +19,8 @@ const cliVersion = "4.0.3";
deleteEmptyTargets();
} else if (myCustomArgv.update_snyk_code_orgs){
updateSnykCode();
} else if (myCustomArgv.get_all_org_issues){
getIssuesCount();
}
else {
echo(`invalid command!`);
Expand Down
80 changes: 71 additions & 9 deletions pagination.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { echo, fetch, chalk } from "zx";
import { echo, fetch, chalk, spinner } from "zx";
import { addIntegrationType, myCustomArgv } from "./helper.js";

export const paginationForGetAllOrgsGroup = async (nextPage, orgIds) => {
Expand Down Expand Up @@ -110,11 +110,7 @@ export const paginationForDeleteEmptyTargetsInOrg = async (nextPage, orgId) => {
const data = await response.json();

if (data.data.length === 0) {
echo(
`${chalk.redBright(
target.id
)} is empty. Deleting it...`
);
echo(`${chalk.redBright(target.id)} is empty. Deleting it...`);
//call delete api here
try {
await fetch(
Expand All @@ -130,9 +126,7 @@ export const paginationForDeleteEmptyTargetsInOrg = async (nextPage, orgId) => {
.then(async (response) => {
if (response.status == 204) {
echo(
`${chalk.greenBright(
target.id
)}sucessfully deleted`
`${chalk.greenBright(target.id)}sucessfully deleted`
);
} else {
echo(
Expand Down Expand Up @@ -176,3 +170,71 @@ export const paginationForDeleteEmptyTargetsInOrg = async (nextPage, orgId) => {
return null;
}
};
export const paginationForGetIssuesCount = async (
nextPage,
orgId,
lowIssueCount,
mediumIssueCount,
highIssueCount,
criticalIssueCount
) => {
try {
while (nextPage) {
const response = await fetch(`https://api.snyk.io${nextPage}`, {
method: "GET",
headers: {
accept: "application/vnd.api+json",
authorization: `Token ${myCustomArgv.snyk_token}`,
},
});

if (!response.ok) {
const errorText = await response.json();
echo(
chalk.red(
`HTTP error! Status: ${response.status}, Response: ${JSON.stringify(
errorText
)}`
)
);
return;
}

const data = await response.json();
const issues = data.data;

await (spinner(chalk.red(`Calculating number of issues for org ${orgId}`),async()=>{
for (const issue of issues) {
switch (issue.attributes.effective_severity_level) {
case "low":
lowIssueCount++;
break;
case "medium":
mediumIssueCount++;
break;
case "high":
highIssueCount++;
break;
case "critical":
criticalIssueCount++;
break;
}
}
}))


nextPage = data.links.next || null;
}

echo(
chalk.green(`Organization ID: ${orgId} has the following issues counts`)
);
echo(chalk.yellow(`Low Issues Counts: ${lowIssueCount}`));
echo(chalk.yellow(`Medium Issues Counts: ${mediumIssueCount}`));
echo(chalk.yellow(`High Issues Counts: ${highIssueCount}`));
echo(chalk.yellow(`Critical Issues Counts: ${criticalIssueCount}`));
} catch (error) {
echo(chalk.red(`Fetch error: ${error.message}`));
console.error(error);
}
};

0 comments on commit 3a2631e

Please sign in to comment.