Skip to content

Commit

Permalink
Merge pull request #39 from matt-ball/resolve-unexpected-token
Browse files Browse the repository at this point in the history
resolve unexpected token error
  • Loading branch information
matt-ball authored Oct 8, 2021
2 parents b89a446 + a8a7167 commit b46f18d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
53 changes: 35 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254208,25 +254208,24 @@ init()

async function init () {
try {
const get = core.getInput
const required = { required: true }
const apiBase = 'https://api.postman.com'
const idRegex = /^[0-9]{7,}-\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/

const options = {
apiKey: '?apikey=' + get('apiKey'),
apiKey: get('apiKey'),
collection: get('collection', required),
environment: get('environment'),
globals: get('globals'),
iterationCount: Number(get('iterationCount')),
iterationCount: num(get('iterationCount')),
iterationData: get('iterationData'),
folder: split(get('folder')),
workingDir: get('workingDir'),
insecureFileRead: safeParse(get('insecureFileRead')),
timeout: Number(get('timeout')),
timeoutRequest: Number(get('timeoutRequest')),
timeoutScript: Number(get('timeoutScript')),
delayRequest: Number(get('delayRequest')),
timeout: num(get('timeout')),
timeoutRequest: num(get('timeoutRequest')),
timeoutScript: num(get('timeoutScript')),
delayRequest: num(get('delayRequest')),
ignoreRedirects: safeParse(get('ignoreRedirects')),
insecure: safeParse(get('insecure')),
bail: safeParse(get('bail')),
Expand All @@ -254243,24 +254242,31 @@ async function init () {
cookieJar: get('cookieJar')
}

if (!options.apiKey) {
core.warning('No Postman API key provided.')
}

if (options.collection.match(idRegex)) {
options.collection = `${apiBase}/collections/${options.collection}${options.apiKey}`
if (!options.apiKey) {
core.setFailed('No Postman API key provided for collection retrieval.')
}
options.collection = `${apiBase}/collections/${options.collection}?apikey=${options.apiKey}`
}

if (options.environment.match(idRegex)) {
options.environment = `${apiBase}/environments/${options.environment}${options.apiKey}`
if (!options.apiKey) {
core.setFailed('No Postman API key provided for environment retrieval.')
}
options.environment = `${apiBase}/environments/${options.environment}?apikey=${options.apiKey}`
}

runNewman(options)
runNewman(removeEmpty(options))
} catch (error) {
core.setFailed(error.message)
}
}

function get (key, opts) {
const val = core.getInput(key, opts)
return val !== '' ? val : null
}

function safeParse (obj) {
if (obj) {
try {
Expand All @@ -254273,15 +254279,26 @@ function safeParse (obj) {
return null
}

function num (i) {
if (i) {
return Number(i)
}

return i
}

function split (str) {
return str.split(',')
return str ? str.split(',') : null
}

function removeEmpty (obj) {
return Object.entries(obj)
.filter(([_, v]) => v != null)
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})
}

function runNewman (options) {
console.log('options')
console.log(options)
newman.run(options).on('done', (err, summary) => {
console.log(err)
if (!options.suppressExitCode && (err || summary.run.failures.length)) {
core.setFailed('Newman run failed!' + (err || ''))
}
Expand Down
53 changes: 35 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ init()

async function init () {
try {
const get = core.getInput
const required = { required: true }
const apiBase = 'https://api.postman.com'
const idRegex = /^[0-9]{7,}-\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/

const options = {
apiKey: '?apikey=' + get('apiKey'),
apiKey: get('apiKey'),
collection: get('collection', required),
environment: get('environment'),
globals: get('globals'),
iterationCount: Number(get('iterationCount')),
iterationCount: num(get('iterationCount')),
iterationData: get('iterationData'),
folder: split(get('folder')),
workingDir: get('workingDir'),
insecureFileRead: safeParse(get('insecureFileRead')),
timeout: Number(get('timeout')),
timeoutRequest: Number(get('timeoutRequest')),
timeoutScript: Number(get('timeoutScript')),
delayRequest: Number(get('delayRequest')),
timeout: num(get('timeout')),
timeoutRequest: num(get('timeoutRequest')),
timeoutScript: num(get('timeoutScript')),
delayRequest: num(get('delayRequest')),
ignoreRedirects: safeParse(get('ignoreRedirects')),
insecure: safeParse(get('insecure')),
bail: safeParse(get('bail')),
Expand All @@ -40,24 +39,31 @@ async function init () {
cookieJar: get('cookieJar')
}

if (!options.apiKey) {
core.warning('No Postman API key provided.')
}

if (options.collection.match(idRegex)) {
options.collection = `${apiBase}/collections/${options.collection}${options.apiKey}`
if (!options.apiKey) {
core.setFailed('No Postman API key provided for collection retrieval.')
}
options.collection = `${apiBase}/collections/${options.collection}?apikey=${options.apiKey}`
}

if (options.environment.match(idRegex)) {
options.environment = `${apiBase}/environments/${options.environment}${options.apiKey}`
if (!options.apiKey) {
core.setFailed('No Postman API key provided for environment retrieval.')
}
options.environment = `${apiBase}/environments/${options.environment}?apikey=${options.apiKey}`
}

runNewman(options)
runNewman(removeEmpty(options))
} catch (error) {
core.setFailed(error.message)
}
}

function get (key, opts) {
const val = core.getInput(key, opts)
return val !== '' ? val : null
}

function safeParse (obj) {
if (obj) {
try {
Expand All @@ -70,15 +76,26 @@ function safeParse (obj) {
return null
}

function num (i) {
if (i) {
return Number(i)
}

return i
}

function split (str) {
return str.split(',')
return str ? str.split(',') : null
}

function removeEmpty (obj) {
return Object.entries(obj)
.filter(([_, v]) => v != null)
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})
}

function runNewman (options) {
console.log('options')
console.log(options)
newman.run(options).on('done', (err, summary) => {
console.log(err)
if (!options.suppressExitCode && (err || summary.run.failures.length)) {
core.setFailed('Newman run failed!' + (err || ''))
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "newman-action",
"version": "0.3.6",
"version": "0.3.7",
"description": "Run Postman collections with Newman as a GitHub Action",
"main": "dist/index.js",
"repository": {
Expand Down

0 comments on commit b46f18d

Please sign in to comment.