Skip to content

Commit

Permalink
Fetch PR and title from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
zzak committed May 9, 2024
1 parent 5c8973d commit 2caa95a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.envrc
!/tmp/.keep
/tmp
25 changes: 25 additions & 0 deletions bin/fetch-pr
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/buildkite/config/build_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/buildkite/config/fetch_pr.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/buildkite_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Config
autoload :Diff
autoload :DockerBuild
autoload :BuildContext
autoload :FetchPr
autoload :RakeCommand
autoload :RubyConfig
autoload :RubyGroup
Expand Down
10 changes: 10 additions & 0 deletions pipelines/rails-ci/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pipelines/rails-ci/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added tmp/.keep
Empty file.

0 comments on commit 2caa95a

Please sign in to comment.