Skip to content

Commit

Permalink
feat: allow cherry diff to take a json file as input
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Dec 2, 2023
1 parent 33986c9 commit 451dcc7
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions bin/cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ program
program
.command('diff')
.requiredOption('--metric <metric>', 'Add a metric', (value, previous) => (previous ? [...previous, value] : [value]))
.option('--input-file <input_file>', 'A JSON file containing the metrics to compare with')
.option('--api-key <api_key>', 'Your cherrypush.com API key (available on https://www.cherrypush.com/user/settings)')
.option('--error-if-increase', 'Return an error status code (1) if the metric increased since its last report')
.action(async (options) => {
const configuration = await getConfiguration()
const apiKey = options.apiKey || process.env.CHERRY_API_KEY
const metrics = options.metric
const inputFile = options.inputFile

let lastMetricValue
let previousOccurrences
Expand All @@ -189,21 +191,33 @@ program
for (const metric of metrics) {
try {
console.log('-----------------------------------')
console.log(`Fetching last value for metric ${metric}...`)

const params = {
project_name: configuration.project_name,
metric_name: metric,
api_key: apiKey,
if (inputFile) {
const content = fs.readFileSync(inputFile, 'utf8')
const metrics = JSON.parse(content)
const metricOccurrences = metrics.find((m) => m.name === metric)?.occurrences || []
lastMetricValue = _.sumBy(metricOccurrences, (occurrence) =>
_.isNumber(occurrence.value) ? occurrence.value : 1
)
previousOccurrences = metricOccurrences.map((occurrence) => occurrence.text)
} else {
console.log(`Fetching last value for metric ${metric}...`)
const params = {
project_name: configuration.project_name,
metric_name: metric,
api_key: apiKey,
}

const response = await axios.get(API_BASE_URL + '/metrics', { params }).catch((error) => {
console.error(`Error: ${error.response.status} ${error.response.statusText}`)
console.error(error.response.data.error)
process.exit(1)
})

lastMetricValue = response.data.value
previousOccurrences = response.data.occurrences
}
const response = await axios.get(API_BASE_URL + '/metrics', { params }).catch((error) => {
console.error(`Error: ${error.response.status} ${error.response.statusText}`)
console.error(error.response.data.error)
process.exit(1)
})

lastMetricValue = response.data.value
previousOccurrences = response.data.occurrences
if (!Number.isInteger(lastMetricValue)) {
console.log('No last value found for this metric, aborting.')
process.exit(1)
Expand Down

0 comments on commit 451dcc7

Please sign in to comment.