Skip to content

Commit

Permalink
Add debug messaging to api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Robthreefold committed Jul 27, 2024
1 parent 62ccaf5 commit 83c94a8
Show file tree
Hide file tree
Showing 11 changed files with 1,629 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- run : |
npm install -g tfx-cli
npx tfx extension create --output-path --manifest-globs vss-extension-dev.json
npx tfx extension publish --auth-type pat -t $AZURE_TOKEN --publisher $PUBLISHER_ID --vsix ./SnykProfessionalServices.snyk-artifactory-upload-dev-${{fromJson(steps.set_version.outputs.vssextensiondevjson).version}}.vsix
npx tfx extension publish --auth-type pat -t $AZURE_TOKEN --publisher $PUBLISHER_ID --vsix ./SnykProfessionalServices.snyk-artifactory-upload-dev-${{fromJson(steps.set_version.outputs.vssextensiondevjson).version}}.vsix
66 changes: 59 additions & 7 deletions task/artifactory-api-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const tl = require("azure-pipelines-task-lib/task");
const axios_1 = __importDefault(require("axios"));
const axios_retry_1 = __importDefault(require("axios-retry"));
const Utils = __importStar(require("./helpers"));
const logger_1 = __importDefault(require("./logger"));
(0, axios_retry_1.default)(axios_1.default, {
retries: 10, // Number of retries
retryDelay: axios_retry_1.default.exponentialDelay, // Retry delay strategy
Expand Down Expand Up @@ -73,19 +74,43 @@ function setProperties(properties) {
const queryParams = {
"properties": [prop] + '=' + properties[prop], // Assuming 'prop' and 'properties' are defined elsewhere
};
axios_1.default.put(artifactUrl, null, {
setTimeout(() => axios_1.default.put(artifactUrl, null, {
params: queryParams,
headers: headers,
})
.then(response => {
console.log(`Successfully set property '${prop}' on Artifact ${artifactUrlShort}`);
})
.catch(error => {
//test
console.log('Error while attempting to add property to Artifact:' + error);
// Handle errors here
process.exit(1); // Exiting with a non-zero code indicating an error
});
for (let errorStatus of error.response.data.errors) {
switch (errorStatus.status) {
case 400:
console.log(`Invalid request parameters (headers / body). See headers here: ${headers} '\n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
case 401:
console.log(`Endpoint requires authentication, please specify credentials. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
case 403:
console.log(`Endpoint permission requirements are not met. Message from endpoint is: Message from endpoint is: ${errorStatus.message} ${errorStatus.status} Please check that account has permission to ${prop} property on Artifact ${artifactUrlShort}.`);
logger_1.default.debug(errorStatus);
console.log(`Artifact URL endpoint that failed ${artifactUrl}`);
process.exit(1);
case 500:
console.log(`Server error! Unexpected error during request handling, check distribution logs. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
default:
console.log(`Endpoint failed with the following error: ${error.message} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
}
}
// process.exit(1); // Exiting with a non-zero code indicating an error
}), 1000);
});
}
}
Expand All @@ -94,7 +119,8 @@ function setProperties(properties) {
const buildNumber = tl.getInput('BuildNumber', true);
const projectName = tl.getInput('ProjectKey', true);
const BuildStatus = tl.getInput('BuildStatus', false);
const searchBody = Object.assign({ "buildName": buildName, "buildNumber": buildNumber, "project": projectName }, (BuildStatus !== null && { myProperty: BuildStatus }));
const repos = tl.getInput('ArtifactoryRepositoryName', false);
const searchBody = Object.assign(Object.assign({ "buildName": buildName, "buildNumber": buildNumber, "project": projectName }, (repos !== undefined && { repos: [repos] })), (BuildStatus !== null && { buildStatus: BuildStatus }));
const searchUrl = `${baseUrl}/api/search/buildArtifacts`;
axios_1.default.post(searchUrl, JSON.stringify(searchBody), {
headers: headers,
Expand Down Expand Up @@ -122,15 +148,41 @@ function setProperties(properties) {
// Adding a delay between each API call
})
.catch(error => {
console.log('Error while attempting to add property to Artifact: ' + error);
console.log('Error while attempting to add property to Artifact:' + error);
// Handle errors here
process.exit(1); // Exiting with a non-zero code indicating an error
for (let errorStatus of error.response.data.errors) {
switch (errorStatus.status) {
case 400:
console.log(`Invalid request parameters (headers / body). See headers here: ${headers} '\n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
case 401:
console.log(`Endpoint requires authentication, please specify credentials. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
case 403:
console.log(`Endpoint permission requirements are not met. Message from endpoint is: Message from endpoint is: ${errorStatus.message} ${errorStatus.status} Please check that account has permission to ${prop} property on Artifact ${artifactUrlShort}.`);
console.log(`Here is the artifact URL ${artifactUrl}`);
logger_1.default.debug(errorStatus);
process.exit(1);
case 500:
console.log(`Server error! Unexpected error during request handling, check distribution logs. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
default:
console.log(`Endpoint failed with the following error: ${error.message} \n Throwing failing code of 1`);
logger_1.default.debug(errorStatus);
process.exit(1);
}
}
// process.exit(1); // Exiting with a non-zero code indicating an error
}), 1000);
});
}
})
.catch((error) => {
console.error('Error from Artifactory search builds API:', error.response ? error.response.data : error.message);
console.log(`Artifactory search builds body: \n ${JSON.stringify(searchBody)}`);
process.exit(1);
});
}
Expand Down
78 changes: 67 additions & 11 deletions task/artifactory-api-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import tl = require('azure-pipelines-task-lib/task');
import axios from 'axios';
import axiosRetry from 'axios-retry';
import * as Utils from './helpers'
import logger from './logger';
import { json } from 'stream/consumers';


Expand Down Expand Up @@ -50,6 +51,7 @@ export function setProperties(properties: any): void {
const queryParams = {
"properties": [prop] + '=' + properties[prop], // Assuming 'prop' and 'properties' are defined elsewhere
};
setTimeout(() =>
axios.put(artifactUrl, null, {
params: queryParams,
headers: headers,
Expand All @@ -58,11 +60,36 @@ export function setProperties(properties: any): void {
console.log(`Successfully set property '${prop}' on Artifact ${artifactUrlShort}`)
})
.catch(error => {
//test
console.log('Error while attempting to add property to Artifact:' + error);
// Handle errors here
process.exit(1); // Exiting with a non-zero code indicating an error
});
console.log('Error while attempting to add property to Artifact:' + error);
// Handle errors here
for (let errorStatus of error.response.data.errors) {
switch (errorStatus.status) {
case 400:
console.log(`Invalid request parameters (headers / body). See headers here: ${headers} '\n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
case 401:
console.log(`Endpoint requires authentication, please specify credentials. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
case 403:
console.log(`Endpoint permission requirements are not met. Message from endpoint is: Message from endpoint is: ${errorStatus.message} ${errorStatus.status} Please check that account has permission to ${prop} property on Artifact ${artifactUrlShort}.`);
logger.debug(errorStatus)
console.log(`Artifact URL endpoint that failed ${artifactUrl}`);
process.exit(1)
case 500:
console.log(`Server error! Unexpected error during request handling, check distribution logs. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
default:
console.log(`Endpoint failed with the following error: ${error.message} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
}
}
// process.exit(1); // Exiting with a non-zero code indicating an error
})
, 1000)
});
}
}else if (inputType == "build"){
Expand All @@ -71,11 +98,14 @@ export function setProperties(properties: any): void {
const buildNumber = tl.getInput('BuildNumber', true)
const projectName = tl.getInput('ProjectKey', true)
const BuildStatus = tl.getInput('BuildStatus', false)
const repos = tl.getInput('ArtifactoryRepositoryName', false)

const searchBody = {
"buildName": buildName,
"buildNumber": buildNumber,
"project" : projectName,
...(BuildStatus !== null && { myProperty: BuildStatus }),
...(repos !== undefined && { repos: [repos] }),
...(BuildStatus !== null && { buildStatus: BuildStatus }),
}

const searchUrl = `${baseUrl}/api/search/buildArtifacts`
Expand Down Expand Up @@ -109,17 +139,43 @@ export function setProperties(properties: any): void {

// Adding a delay between each API call
})
.catch(error => {
console.log('Error while attempting to add property to Artifact: ' + error);
// Handle errors here
process.exit(1); // Exiting with a non-zero code indicating an error
})
.catch(error => {
console.log('Error while attempting to add property to Artifact:' + error);
// Handle errors here
for (let errorStatus of error.response.data.errors) {
switch (errorStatus.status) {
case 400:
console.log(`Invalid request parameters (headers / body). See headers here: ${headers} '\n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
case 401:
console.log(`Endpoint requires authentication, please specify credentials. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
case 403:
console.log(`Endpoint permission requirements are not met. Message from endpoint is: Message from endpoint is: ${errorStatus.message} ${errorStatus.status} Please check that account has permission to ${prop} property on Artifact ${artifactUrlShort}.`);
console.log(`Here is the artifact URL ${artifactUrl}`);
logger.debug(errorStatus)
process.exit(1)
case 500:
console.log(`Server error! Unexpected error during request handling, check distribution logs. Message from endpoint is: ${errorStatus.message} ${errorStatus.status} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
default:
console.log(`Endpoint failed with the following error: ${error.message} \n Throwing failing code of 1`);
logger.debug(errorStatus)
process.exit(1)
}
}
// process.exit(1); // Exiting with a non-zero code indicating an error
})
, 1000)
});
}
})
.catch((error) => {
console.error('Error from Artifactory search builds API:', error.response ? error.response.data : error.message);
console.log(`Artifactory search builds body: \n ${JSON.stringify(searchBody)}`)
process.exit(1)
});
}
Expand Down
10 changes: 10 additions & 0 deletions task/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const winston_1 = require("winston");
const logger = (0, winston_1.createLogger)({
level: 'debug',
format: winston_1.format.combine(winston_1.format.colorize(), winston_1.format.timestamp(), winston_1.format.printf(({ timestamp, level, message }) => {
return `${timestamp} [${level}]: ${message}`;
})),
});
exports.default = logger;
14 changes: 14 additions & 0 deletions task/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createLogger, format } from 'winston';

const logger = createLogger({
level: 'debug',
format: format.combine(
format.colorize(),
format.timestamp(),
format.printf(({ timestamp, level, message }) => {
return `${timestamp} [${level}]: ${message}`;
})
),
});

export default logger;
24 changes: 12 additions & 12 deletions devTask/task.json → task/old.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"$schema": "https://raw.githubusercontent.com/Microsoft/azure-pipelines-task-lib/master/tasks.schema.json",
"id": "2d8ca2db-be04-4062-80ef-e139f39915a2",
"name": "snyk-artifactory-upload-dev",
"friendlyName": "snyk-artifactory-azure-upload-dev",
"description": "Development version to set's properties on Artifactory artifacts with result of snyk SAST scan",
"id": "40614d12-c808-435f-a2ea-181e09503ad4",
"name": "artifactory-azure-upload",
"friendlyName": "snyk-artifactory-azure-upload",
"description": "Set's properties on Artifactory artifacts with result of snyk SAST scan",
"helpMarkDown": "",
"category": "Utility",
"author": "Snyk",
"version": {
"Major": 1,
"Minor": 1,
"Patch": 2
"Minor": 0,
"Patch": 41
},
"instanceNameFormat": "Snyk Set Artifactory Properties",
"inputs": [
Expand All @@ -21,9 +21,9 @@
"defaultValue": "CopyAndProcess",
"helpMarkDown": "Operation determines the function of the extension, copy (Takes Snyk JSON scan information and uploads it to build directory) should come before process (Updates selected Artifact with Snyk scan details) in the pipeline",
"options": {
"Copy": "Copy",
"Process": "Process",
"CopyAndProcess": "CopyAndProcess"
"Copy" : "Copy",
"Process" : "Process",
"CopyAndProcess" : "CopyAndProcess"
},
"required": true
},
Expand Down Expand Up @@ -51,8 +51,8 @@
"visibleRule": "Operation = Process || Operation = CopyAndProcess",
"defaultValue": "Build",
"options": {
"UrlList": "UrlList",
"Build": "Build"
"UrlList" : "UrlList",
"Build" : "Build"
}
},
{
Expand Down Expand Up @@ -115,4 +115,4 @@
"target": "index.js"
}
}
}
}
Loading

0 comments on commit 83c94a8

Please sign in to comment.