Class: BotGithub

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bot_github.rb

Instance Method Summary collapse

Constructor Details

#initializeBotGithub

Returns a new instance of BotGithub.



10
11
12
13
# File 'lib/bot_github.rb', line 10

def initialize
  @client = Octokit::Client.new :access_token => BotConfig.instance.github_access_token
  @client.
end

Instance Method Details

#syncObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bot_github.rb', line 15

def sync
  puts "\nStarting Github Xcode Bot Builder #{Time.now}\n-----------------------------------------------------------"
  # Check to see if we're already running and skip this run if so
  running_instances = `ps aux | grep [b]ot-sync-github | grep -v bin/sh`.split("\n")
  if (running_instances.count > 1)
    $stderr.puts "Skipping run since bot-sync-github is already running"
    PP.pp(running_instances, STDERR)
    exit 1
  end

  bot_statuses = BotBuilder.instance.status_of_all_bots
  bots_processed = []
  pull_requests.each do |pr|
    # Check if a bot exists for this PR
    bot = bot_statuses[pr.bot_short_name_without_version]
    bots_processed << pr.bot_short_name
    if (bot.nil?)
      # Create a new bot
      BotBuilder.instance.create_bot(pr.bot_short_name, pr.bot_long_name, pr.branch,
                                     BotConfig.instance.scm_path,
                                     BotConfig.instance.xcode_project_or_workspace,
                                     BotConfig.instance.xcode_scheme,
                                     BotConfig.instance.xcode_devices)
      create_status_new_build(pr)
    else
      github_state_cur = latest_github_state(pr).state # :unknown :pending :success :error :failure
      github_state_new = convert_bot_status_to_github_state(bot)
      if (github_state_new == :pending && github_state_cur != github_state_new)
        # User triggered a new build by clicking Integrate on the Xcode server interface
        create_status(pr, github_state_new, convert_bot_status_to_github_description(bot), bot.status_url)
      elsif (github_state_new != :unknown && github_state_cur != github_state_new)
        # Build has passed or failed so update status and comment on the issue
        create_comment_for_bot_status(pr, bot)
        create_status(pr, github_state_new, convert_bot_status_to_github_description(bot), bot.status_url)
      elsif (github_state_cur == :unknown || user_requested_retest(pr, bot))
        # Unknown state occurs when there's a new commit so trigger a new build
        BotBuilder.instance.start_bot(bot.guid)
        create_status_new_build(pr)
      else
        puts "PR #{pr.number} (#{github_state_cur}) is up to date for bot #{bot.short_name}"
      end
    end
  end

  # Delete bots that no longer have open pull requests
  bots_unprocessed = bot_statuses.keys - bots_processed
  bots_unprocessed.each do |bot_short_name|
    bot = bot_statuses[bot_short_name]
    BotBuilder.instance.delete_bot(bot.guid) unless !is_managed_bot(bot)
  end

  puts "-----------------------------------------------------------\nFinished Github Xcode Bot Builder #{Time.now}\n"
end