Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #19366 from code-dot-org/staging-next-github-hooks #19390

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions bin/cron/update_dtsn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

# This script updates the "DTSN" check on PRs

require_relative '../../deployment'
require 'cdo/github'
require 'cdo/developers_topic'
require 'cdo/only_one'

DTSN_FILE = '.dtsn'

def main
current_dtsn = DevelopersTopic.dtsn?
# Assume DTSN was 'yes' if the file is missing
previous_dtsn = !File.exist?(DTSN_FILE) || File.read(DTSN_FILE) == "true"
File.write(DTSN_FILE, current_dtsn.to_s)
if current_dtsn != previous_dtsn
if current_dtsn
GitHub.set_all_dtsn_check_pass
else
GitHub.set_all_dtsn_check_fail
end
end
end

main if only_one_running?(__FILE__)
4 changes: 4 additions & 0 deletions cookbooks/cdo-apps/templates/default/crontab.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
cronjob at:'*/1 * * * *', do:deploy_dir('bin', 'cron', 'update_dts')
end

if node.chef_environment == 'test' && node.name == 'staging-next'
cronjob at:'*/1 * * * *', do:deploy_dir('bin', 'cron', 'update_dtsn')
end

if node.chef_environment == 'test' && node.name == 'test' # 'real' test only
# This should be run shortly after the commit_content job run on levelbuilder.
cronjob at:'20 23 * * 1-5', do:deploy_dir('bin', 'cron', 'deploy_to_levelbuilder')
Expand Down
21 changes: 20 additions & 1 deletion lib/cdo/developers_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
module DevelopersTopic
BRANCH_PREFIXES = {
staging: 'DTS: ',
'staging-next': 'DTSN: ',
test: 'DTT: ',
production: 'DTP: ',
levelbuilder: 'DTL: '
}.freeze
STAGING = 'staging'
STAGING_NEXT = 'staging-next'
TEST = 'test'
PRODUCTION = 'production'
LEVELBUILDER = 'levelbuilder'
Expand All @@ -29,6 +31,11 @@ def self.dts?
branch_open_for_merge? STAGING
end

# @return [Boolean] Whether DTSN is yes.
def self.dtsn?
branch_open_for_merge? STAGING_NEXT
end

# @return [Boolean] Whether DTT is yes.
def self.dtt?
branch_open_for_merge? TEST
Expand All @@ -50,6 +57,12 @@ def self.dts
branch_message STAGING
end

# @return [String] The DTSN portion of the room topic.
# @raise [RuntimeError] If the existing DTS topic does not specify a message.
def self.dtsn
branch_message STAGING_NEXT
end

# @return [String] The DTT portion of the room topic.
# @raise [RuntimeError] If the existing DTT topic does not specify a message.
def self.dtt
Expand All @@ -74,6 +87,12 @@ def self.set_dts(message)
set_branch_message STAGING, message
end

# @param new_subtopic [String] The string to which DTSN should be set.
# @raise [RuntimeError] If the existing DTSN topic does not specify a message.
def self.set_dtsn(message)
set_branch_message STAGING_NEXT, message
end

# @param message [String] The string to which DTT should be set.
# @raise [RuntimeError] If the existing DTT topic does not specify a message.
def self.set_dtt(message)
Expand All @@ -94,7 +113,7 @@ def self.set_dtl(message)

private_class_method def self.get_room_for_branch(branch)
case branch
when STAGING
when STAGING, STAGING_NEXT
DEVELOPERS_ROOM
when TEST, PRODUCTION, LEVELBUILDER
DEPLOY_STATUS_ROOM
Expand Down
38 changes: 38 additions & 0 deletions lib/cdo/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ module GitHub
DASHBOARD_DB_DIR = 'dashboard/db/'.freeze
PEGASUS_DB_DIR = 'pegasus/migrations/'.freeze
STAGING_BRANCH = 'staging'.freeze
STAGING_NEXT_BRANCH = 'staging-next'.freeze
STATUS_SUCCESS = 'success'.freeze
STATUS_FAILURE = 'failure'.freeze
STATUS_CONTEXT = 'DTS'.freeze
STATUS_CONTEXT_DTSN = 'DTSN'.freeze

# Configures Octokit with our GitHub access token.
# @raise [RuntimeError] If CDO.github_access_token is not defined.
Expand Down Expand Up @@ -199,6 +201,42 @@ def self.set_all_dts_check_fail
end
end

def self.set_dtsn_check_pass(pull)
Octokit.create_status(
pull['base']['repo']['full_name'],
pull['head']['sha'],
STATUS_SUCCESS,
context: STATUS_CONTEXT_DTSN,
description: 'The staging-next branch is open.'
)
end

def self.set_all_dtsn_check_pass
configure_octokit
Octokit.pulls(REPO, base: STAGING_NEXT_BRANCH)
paged_for_each(Octokit.last_response) do |pull|
set_dtsn_check_pass(pull)
end
end

def self.set_dtsn_check_fail(pull)
Octokit.create_status(
pull['base']['repo']['full_name'],
pull['head']['sha'],
STATUS_FAILURE,
context: STATUS_CONTEXT_DTSN,
description: 'The staging-next branch is closed. Check #developers.'
)
end

def self.set_all_dtsn_check_fail
configure_octokit
Octokit.pulls(REPO, base: STAGING_NEXT_BRANCH)
paged_for_each(Octokit.last_response) do |pull|
set_dtsn_check_fail(pull)
end
end

# Iterate over a paged resource, given the first response
def self.paged_for_each(response)
loop do
Expand Down
19 changes: 19 additions & 0 deletions pegasus/routes/dev_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@
end
'success'
end

post '/api/dev/check-dtsn' do
forbidden! unless rack_env == :test || rack_env == :development
forbidden! unless verify_signature(CDO.github_webhook_secret)
data = JSON.parse(params[:payload])
unless CHECK_DTS_ACTIONS.include?(data['action']) &&
request.env['HTTP_X_GITHUB_EVENT'] == 'pull_request' &&
data['pull_request']['base']['ref'] == 'staging-next'
status 202
next 'I only check the DTSN status for PRs against staging-next'
end
GitHub.configure_octokit
if DevelopersTopic.dtsn?
GitHub.set_dtsn_check_pass(data['pull_request'])
else
GitHub.set_dtsn_check_fail(data['pull_request'])
end
'success'
end