diff --git a/.gitignore b/.gitignore index 7af72fbf..ad970dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .envrc +!/tmp/.keep /tmp diff --git a/bin/fetch-pr b/bin/fetch-pr new file mode 100755 index 00000000..c3334a26 --- /dev/null +++ b/bin/fetch-pr @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "net/http" +require "json" +require "uri" + +def github_token + ENV.fetch("GITHUB_PUBLIC_REPO_TOKEN") { raise "Missing GITHUB_PUBLIC_REPO_TOKEN!" } +end + +def pull_request_number + ENV.fetch("BUILDKITE_PULL_REQUEST") { raise "Missing BUILDKITE_PULL_REQUEST!" } +end + +uri = URI("https://api.github.com/repos/rails/rails/pulls/#{pull_request_number}") + +req = Net::HTTP::Get.new(uri) +req["Authorization"] = "token #{github_token}" + +res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(req) +end + +puts res.body diff --git a/lib/buildkite/config/build_context.rb b/lib/buildkite/config/build_context.rb index 5d419d87..ae3fd561 100644 --- a/lib/buildkite/config/build_context.rb +++ b/lib/buildkite/config/build_context.rb @@ -25,7 +25,7 @@ def nightly? def skip? # [ci skip], [skip ci], [ci-skip], or [skip-ci] - [ENV["BUILDKITE_MESSAGE"]].grep(/(ci skip|skip ci|ci-skip|skip-ci)/i).any? + [ENV["BUILDKITE_MESSAGE"], FetchPr.title].grep(/(ci skip|skip ci|ci-skip|skip-ci)/i).any? end def rails_root diff --git a/lib/buildkite/config/fetch_pr.rb b/lib/buildkite/config/fetch_pr.rb new file mode 100644 index 00000000..ed76bf86 --- /dev/null +++ b/lib/buildkite/config/fetch_pr.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "json" + +module Buildkite::Config + module FetchPr + def self.title + pr = JSON.parse(File.read(".buildkite/tmp/.pr-meta.json")) + pr["title"] + rescue + "" + end + end +end diff --git a/lib/buildkite_config.rb b/lib/buildkite_config.rb index de769097..a5b992df 100644 --- a/lib/buildkite_config.rb +++ b/lib/buildkite_config.rb @@ -11,6 +11,7 @@ module Config autoload :Diff autoload :DockerBuild autoload :BuildContext + autoload :FetchPr autoload :RakeCommand autoload :RubyConfig autoload :RubyGroup diff --git a/pipelines/rails-ci/initial.yml b/pipelines/rails-ci/initial.yml index c9753760..22e77c8d 100644 --- a/pipelines/rails-ci/initial.yml +++ b/pipelines/rails-ci/initial.yml @@ -26,6 +26,16 @@ steps: git clone -b "$$GIT_BRANCH" "$$GIT_REPO" .buildkite rm -rf .buildkite/.git + + echo "Fetching pull-request metadata:" + (docker run --rm \ + -v "$$PWD":/app:ro -w /app \ + -e GITHUB_PUBLIC_REPO_TOKEN \ + -e BUILDKITE_PULL_REQUEST \ + ruby:latest \ + .buildkite/bin/fetch-pr > .buildkite/tmp/.pr-meta.json) || true + + echo "Generating pipeline:" sh -c "$$PIPELINE_COMMAND" ([ -f .buildkite/.dockerignore ] && cp .buildkite/.dockerignore .dockerignore) || true diff --git a/pipelines/rails-ci/pipeline.rb b/pipelines/rails-ci/pipeline.rb index e0b8dad9..36f9b670 100644 --- a/pipelines/rails-ci/pipeline.rb +++ b/pipelines/rails-ci/pipeline.rb @@ -13,7 +13,7 @@ if build_context.skip? command do label "skip" - command "echo 'Build skipped'" + command "echo '+++ :bk-status-passed: Build skipped'" end next diff --git a/tmp/.keep b/tmp/.keep new file mode 100644 index 00000000..e69de29b