Skip to content

Instantly share code, notes, and snippets.

@jayco
Created October 5, 2022 10:26
Show Gist options
  • Save jayco/587296374ec2eb1478e64ea669dc0cd1 to your computer and use it in GitHub Desktop.
Save jayco/587296374ec2eb1478e64ea669dc0cd1 to your computer and use it in GitHub Desktop.
Simple GraphQL query to get total build duration from Buildkite API
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)
});
@jayco
Copy link
Author

jayco commented Oct 5, 2022

% npm start

> jj@1.0.0 start
> node build-duration.js

[graphql]: POST https://graphql.buildkite.com/v1: query ... with {}...
  QUERY: query 
    query {
      build(slug: "buildkite/pipeline/build") {
        jobs(first: 500) {
          edges {
            node {
              ... on JobTypeCommand {
                startedAt
                finishedAt
              }
            }
          }
        }
      }
    }
   
  VARIABLES: {}
 
HMS: 02:27:23

Corresponding build in BK UI

Screen Shot 2022-10-05 at 8 58 51 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment