Module: BuildkiteWatcher

Defined in:
lib/buildkite_watcher.rb,
lib/buildkite_watcher/config.rb,
lib/buildkite_watcher/version.rb,
lib/buildkite_watcher/config_loader.rb

Overview

Command line utility that continuously watches for the buildkite job running current git HEAD and notifies on build status changes.

Defined Under Namespace

Classes: Config, ConfigLoader

Constant Summary collapse

GRAPHQL_QUERY =
<<~GRAPHQL
  query ($pipelineSlug: ID!, $commitHash: [String!], $branch: [String!]) {
    pipeline(slug: $pipelineSlug) {
      commit_hash_builds: builds(commit: $commitHash, state: [PASSED, RUNNING, FAILED, BLOCKED], first: 1) {
        edges {
          node {
            commit
            message
            state
            jobs(first: 1, step: { key: "aggregate_results" }) {
              edges {
                node {
                  ... on JobTypeCommand {
                    label
                    artifacts {
                      edges {
                        node {
                          downloadURL
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      branch_builds: builds(branch: $branch, state: [PASSED, RUNNING, FAILED, BLOCKED], first: 2) {
        edges {
          node {
            commit
            message
            state
            jobs(first: 1, step: { key: "aggregate_results" }) {
              edges {
                node {
                  ... on JobTypeCommand {
                    label
                    artifacts {
                      edges {
                        node {
                          downloadURL
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
BUILD_PASSED =
"PASSED"
BUILD_BLOCKED =
"BLOCKED"
BUILD_RUNNING =
"RUNNING"
BUILD_FAILED =
"FAILED"
BUILD_UNKNOWN_STATUS =
"UNKNOWN"
VERSION =
"0.3.1"

Class Method Summary collapse

Class Method Details

.result(config) ⇒ Object

Scenarios:

  • current commit doesn’t have a build, no prior failed build

  • current commit doesn’t have a build, there is a prior failed build

  • current commit has a build, is running, no prior failed build

  • current commit has a build, is running, there is a prior failed build

  • current commit has a build, has passed

  • current commit has a build, has failed



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/buildkite_watcher.rb', line 104

def result(config)
  puts Rainbow("Branch: ").bright + Rainbow(branch_name)
  puts Rainbow("HEAD ➜  ").bright + Rainbow(commit_hash)
  puts

  buildkite_query = {
    query: GRAPHQL_QUERY,
    variables: {
      pipelineSlug: config.pipeline_slug,
      commitHash: commit_hash,
      branch: branch_name,
    },
  }

  builds = fetch_build_data(buildkite_query, config)
  commit_hash_builds = simplify_builds_response_data(builds.commit_hash_builds)
  branch_builds = simplify_builds_response_data(builds.branch_builds)

  if commit_hash_builds.any?
    build = commit_hash_builds.first
    print_state(build.state)
    return build.state if build_passed?(build.state) || branch_builds.empty?

    if build.state == BUILD_RUNNING && prior_build_failed?(branch_builds)
      puts
      puts Rainbow("Prior failure summary:").bright
      print_failures(prior_failed_build(branch_builds))
    elsif build.state == BUILD_FAILED
      print_failures(build)
    end
  else
    print_no_current_build

    return build.state if branch_builds.empty?

    puts
    puts Rainbow("Prior finished build summary:").bright
    puts

    build = branch_builds.first
    print_state(build.state)
    return build.state if build_passed?(build.state)

    if last_build_failed?(branch_builds)
      puts
      puts Rainbow("Failure summary:").bright

      build = branch_builds.first
      print_failures(build)
    end
  end
  build.state
rescue StandardError => e
  puts
  puts Rainbow("ERROR: \n#{e.backtrace.reverse.join("\n")}\n#{e.message}").red
end

.result_watchObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/buildkite_watcher.rb', line 75

def result_watch
  config = ConfigLoader.load

  Signal.trap("SIGINT") do
    puts
    puts Rainbow("Program interrupt received. Exiting.").red
    exit 1
  end

  previous_result = BUILD_UNKNOWN_STATUS
  loop do
    system("clear")
    new_result = result(config)

    maybe_notify(previous_result, new_result)
    previous_result = new_result

    sleep 30
  end
end