Created
October 5, 2022 10:26
-
-
Save jayco/587296374ec2eb1478e64ea669dc0cd1 to your computer and use it in GitHub Desktop.
Simple GraphQL query to get total build duration from Buildkite API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var graphql = require('graphql.js'); | |
var moment = require('moment'); | |
moment().format(); | |
var graph = graphql("https://graphql.buildkite.com/v1", { | |
method: "POST", | |
headers: { "Authorization": "Bearer <api-token>"}, | |
asJSON: true, | |
debug: true, | |
}); | |
var jobs = graph.query(` | |
query { | |
build(slug: "{organisation}/{pipeline}/{buildnumber}") { | |
jobs(first: 500) { | |
edges { | |
node { | |
... on JobTypeCommand { | |
startedAt | |
finishedAt | |
} | |
} | |
} | |
} | |
} | |
} | |
`); | |
jobs().then((response) => { | |
const times = response.build.jobs.edges.map(edge => { | |
var start = moment(edge.node.startedAt); | |
var end = moment(edge.node.finishedAt); | |
return moment.duration(end.diff(start)); | |
}); | |
const sum = times.reduce((prev, cur) => prev + cur, 0); | |
const hms = moment.utc(sum).format("HH:mm:ss"); | |
console.log('HMS: ' + hms); | |
}).catch((error) => { | |
console.log(error) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Corresponding build in BK UI