手动开始Xcode Bot的集成?

时间:2015-02-06 11:45:59

标签: xcode xcodebuild xcode-bots

我正在观看WWDC 2014“与Xcode持续集成”视频,它看起来很棒,如何使用机器人进行测试。 但我的问题是任何看过该视频的人,当他向Jeeves发送信息说“整合CoffeeBoard”时。开始整合。我想知道他是怎么做到的。

我想在github上添加post-receive hook,在接收任何提交时应该在我的OS X Server上启动Xcode bot。我的大多数团队成员使用SourceTree或GitHub来管理他们的git,他们不想使用Xcode Source Control。我认为创建一个机器人并设置其手动启动的选项将成功。我需要知道,“OS X Server是否会为您提供类似于启动机器人的URL的选项?”

抱歉,如果我不够清楚的话。但这对我来说太混乱了,因为他们对触发器的文档很少。虽然他提到它是很酷的新功能,但他们没有包含任何信息来实现这一目标

3 个答案:

答案 0 :(得分:9)

之前的两个答案并没有完全回答原来的问题"他们是如何做到的"从消息应用程序中启动机器人。

我重新创建了模仿Jeeves虚拟助手与机器人交互(以及获取天气)所需的确切工作流程和脚本。

有关完整详细信息,请参阅链接的PDF文档:

https://s3.amazonaws.com/icefield/IntegratingXcodeBotsWithMessages.pdf

编辑:原始答案已被删除,我相信,因为我通过链接引用了完整答案。此编辑添加了完整的实现细节作为此答案的一部分。我希望答案不会太长。

将Xcode机器人与消息集成

在WWDC 2014 Session 415,与Xcode 6的持续集成中,Apple展示了通过自定义集成触发器将Xcode机器人与Messages应用程序集成。更具体地说,从该会话的视频(https://developer.apple.com/videos/play/wwdc2014-415/)的23分钟标记处开始,Apple演示了将集成触发器与消息结合使用以接收构建服务器上的集成状态。此外,通过使用虚拟聊天室成员Jeeves,他们展示了直接从Messages应用程序中启动集成的能力。以下文章提供了重现该功能的逐步说明。

客户端和服务器配置

首先,以下是我用来模仿Jeeves功能的客户端和服务器的配置:

<强>客户端 OS X版本10.11(El Capitan),Xcode 7.0.1

服务器 OS X版本10.11(El Capitan),OS X Server 5.0.4,Xcode 7.0.1,Ruby 2.0.0p645

网络 对于我的开发和持续集成,我使用内部网络。我的OS X服务器位于domain.local,我的开发机器是同一内部网络上的另一个节点。无论您使用的是内部服务器还是外部服务器,下面的说明都可以使用。

Jabber - 消息的基础

Jabber是用于实例消息传递的开源协议的原始名称。 Jabber更名为可扩展消息传递和在线协议(XMPP)。 OS X Messages应用程序是使用Jabber构建的。

我们将在这项工作中广泛使用Jabber(消息),所以让我们确保它已经开启。从OS X Server App,选择服务&gt;消息视图,并在右上角的消息上切换。对于Jeeves,我使用的消息服务设置如下:

Messages service settings

从服务器上的终端窗口,如果要检查Jabber的特定设置,请使用

$ sudo serveradmin settings jabber

特别注意jabberClientPortTLS(5222)和jabberClientPortSSL(5223)值。这些是服务器上用于与Jabber服务通信的端口。

我们将使用Ruby为Jeeves编写大部分脚本,我们需要一个XMPP / Jabber库来实现这一目标。从服务器上的终端窗口,使用

安装XMPP4R(Ruby的XMPP / Jabber库)
$ gem install xmpp4r

为Jabber服务创建用户

因为我的服务器是一个没有任何开发者帐户的本地服务器,所以我需要为各种开发人员创建帐户以登录Jabber。您可能需要或可能不需要此步骤,具体取决于您的服务器是否已定义用户帐户。

从服务器上的OS X Server应用程序转到帐户&gt;用户列出并为将使用虚拟Jeeves助手的每个客户端添加新用户。务必为Jeeves创建一个新用户。对于用户'Tom',以下是使用的设置。请务必为每个用户创建一个电子邮件地址,但不需要运行Mail服务。这些电子邮件地址将用于从客户端上的消息应用程序登录Jabber服务。

User settings

从客户端开发机器登录Jabber

使用服务器上定义的用户帐户,现在可以从客户端计算机登录Jabber帐户了。在客户端的消息应用中,转到消息&gt;偏好&gt;帐户。选择左下方的+号,选择“其他消息帐户...”,然后按继续。在“添加消息”帐户对话框中,为“帐户类型”选择“Jabber”,然后填写用户的凭据信息。以下是我使用的设置:

enter image description here

(注意SSL切换为开启时,端口(5223)与您在检查服务器上Jabber服务设置时列出的jabberClientPortSSL值相匹配。)

成功登录Jabber服务后,您可以选择在Jabber帐户的“聊天设置”页面下更改您的帐户昵称。所有其他默认设置都可以保持原样。

创建聊天室

我们希望所有僵尸程序集成状态和与我们的虚拟助手Jeeves的通信都通过消息聊天室。聊天室允许群组通信,但您不需要加入邀请。要创建聊天室,请执行以下操作。

从消息中,选择文件&gt;去聊天室。您应该会看到登录到Jabber服务的帐户。键入@ Room..local的房间名称,然后选择Go。 (请注意,我发现聊天室必须是'rooms..local'.com'&gt;。使用'rooms'以外的单词不会创建聊天室。)

Create chat room

配置服务器网站服务

当从客户端计算机上运行的Xcode开始集成时,集成前和集成后脚本通过对OS X Server网站服务上的文件进行http调用来与Jabber服务进行通信。您必须配置OS X Server网站服务以处理这些呼叫。

您需要修改非SSL http(端口80)站点的设置。以下是我使用的设置。

Web server settings

选择端口80网站,然后选择下方的铅笔图标以使您的设置与这些设置相匹配。

Web server settings

选择“编辑高级设置...”并使您的设置与这些设置相匹配。 (启用“允许CGI执行...”可以执行Ruby脚本。)

Web server settings

最后,您需要启用一个特定文件(message_room - 我们稍后会讨论),将其配置为以Ruby脚本运行。为此,请将以下.htaccess文件放在Web服务器的默认主文件夹中(通常为/ Library / Server / Web / Data / Sites / Default)。

Options +ExecCGI 
<FilesMatch message_room$>
    SetHandler cgi-script 
</FilesMatch>

注意:在以下所有ruby脚本中,您需要修改每个脚本中“凭据”注释下的变量,以匹配您的域和登录凭据。

集成前和集成后脚本 当我们在客户端计算机上从Xcode开始集成时,我们希望向Jabber Integration聊天室发送消息,以便可以通知聊天室的所有成员已经开始(并完成)集成。在Xcode中的bot Triggers页面上将以下集成前和集成后脚本添加到项目的bot中。

这是预集成触发器脚本:

#!/usr/bin/env ruby 
require 'json' 
require 'net/http' 
require 'uri'

# ------------------------------------------------------------------------------------- 
# credentials and such
domain = "<yourDomain>.local"

# ------------------------------------------------------------------------------------- 
# our messaging endpoint
uri = URI.parse("http://#{domain}:80/message_room")

# ------------------------------------------------------------------------------------- 
# what we want to say
message = "#{ENV['XCS_BOT_NAME']} integration #{ENV['XCS_INTEGRATION_NUMBER']} is now starting."

# ------------------------------------------------------------------------------------- 
# build up the request body
reqBody = {:message => message}
body = JSON.generate(reqBody)

# ------------------------------------------------------------------------------------- 
# the connect type
http = Net::HTTP.new(uri.host, uri.port)

# ------------------------------------------------------------------------------------- 
# build up the request
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-type', 'application/json')
request.body = body

# ------------------------------------------------------------------------------------- 
# send the request and get the response
response = http.request(request)

这是集成后触发器脚本:

#!/usr/bin/env ruby 
require 'json' 
require 'net/http' 
require 'uri'

# ------------------------------------------------------------------------------------- 
# credentials and such
domain = "<yourDomain>.local"

# ------------------------------------------------------------------------------------- 
# our messaging endpoint
uri = URI.parse("http://#{domain}:80/message_room")

# ------------------------------------------------------------------------------------- 
# what we want to say
integrationResult = case ENV['XCS_INTEGRATION_RESULT']
    when "succeeded"
        "has completed successfully."
    when "test-failures"
        tc = ENV['XCS_TEST_FAILURE_COUNT'].to_i
        "completed with #{tc} failing #{(tc ==1 ) ? 'test' : 'tests'}."
    when "build-errors"
        ec = ENV['XCS_ERROR_COUNT'].to_i
        "failed with #{ec} build #{(ec == 1) ? 'error' : 'errors'}."
    when "warnings"
        wc = ENV['XCS_WARNING_COUNT'].to_i
        "completed with #{wc} #{(wc == 1) ? 'warning' : 'warnings'}."
    when "analyzer-warnings"
        ic = ENV['XCS_ANALYZER_WARNING_COUNT'].to_i
        "completed with #{ic} static analysis #{(ic == 1) ? 'issue' : 'issues'}."
    when "trigger-error"
        "failed running trigger script."
    when "checkout-error"
        "failed to checkout from source control."
    else
        "failed with unexpected errors."
    end

message = "#{ENV['XCS_BOT_NAME']} integration #{ENV['XCS_INTEGRATION_NUMBER']} #{integrationResult}"

# ------------------------------------------------------------------------------------- 
# build up the request body
reqBody = {:message => message}
body = JSON.generate(reqBody)

# ------------------------------------------------------------------------------------- 
# the connect type
http = Net::HTTP.new(uri.host, uri.port)

# ------------------------------------------------------------------------------------- 
# build up the request
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-type', 'application/json')
request.body = body

# -------------------------------------------------------------------------------------
# send the request and get the response
response = http.request(request)

前两个Ruby脚本调用驻留在OS X Server Website主文件夹中的message_room文件(通常为/ Library / Server / Web / Data / Sites / Default)。将以下message_room文件放入该文件夹。

#!/usr/bin/env ruby
require 'cgi' 
require 'json' 
require 'xmpp4r' 
require 'xmpp4r/muc'

# ------------------------------------------------------------------------------------- 
# credentials and such
domain = "<domain>.local"
userId = "jeeves@#{domain}"
userPw = "<jeevesAccountPassword>"
roomName = "integration@rooms.#{domain}"

# ------------------------------------------------------------------------------------- 
# header sent back
cgi = CGI.new
puts cgi.header( "type" => "text/html", "status" => "OK")

# ------------------------------------------------------------------------------------- 
# get the message out of the json formatted text
keyValue = JSON.parse(cgi.params.keys.first)
key = "message"
value = keyValue[key] puts value

# ------------------------------------------------------------------------------------- 
# create the message to the iChat (jabber) room
fromJID = Jabber::JID.new(userId)
jabberClient = Jabber::Client.new(fromJID)
jabberClient.connect
jabberClient.auth(userPw)
jabberClient.send(Jabber::Presence.new.set_type(:available))

# ------------------------------------------------------------------------------------- 
# send the message to a chat room
roomID = roomName + "/" + jabberClient.jid.node
roomJID = Jabber::JID::new(roomID)
room = Jabber::MUC::MUCClient.new(jabberClient) room.join(roomJID)
roomMessage = Jabber::Message.new(roomJID, value) room.send(roomMessage)

从消息应用中启动集成

我们希望能够在消息应用程序中向我们的虚拟助手Jeeves发出指令。我们将支持三条指令:

  1. Jeeves,天气#获取当前天气(没有 zip默认为Cupertino)

  2. Jeeves,整合(Bot Name)#开始给定的集成 机器人

  3. Jeeves,在OS X服务器上退出#shutdown Jeeves

  4. 以下文件将放置在OS X Server网站的默认文件夹中(通常为/ Library / Server / Web / Data / Sites / Default)。

    处理虚拟助手Jeeves的主文件是jeevesManager.rb。启动此文件以输入

    唤醒Jeeves
    $ ruby ./jeevesManager.rb
    

    来自网站上服务器的默认文件夹。

    #!/usr/bin/env ruby
    require 'xmpp4r'
    require 'xmpp4r/muc'
    require 'xmpp4r/delay'
    require './jeevesWeather.rb' 
    require './jeevesIntegration.rb'
    
    # ------------------------------------------------------------------------------------- 
    # credentials and such
    domain = "<domain>.local"
    userId = "jeeves@#{domain}"
    userPw = "<jeevesAccountPassword>"
    roomName = "integration@rooms.#{domain}" 
    defaultWeatherZipCode = "95015"
    
    # ------------------------------------------------------------------------------------- 
    # create the client we'll use
    fromJID = Jabber::JID.new(userId)
    jabberClient = Jabber::Client.new(fromJID)
    jabberClient.connect
    jabberClient.auth(userPw)
    jabberClient.send(Jabber::Presence.new.set_type(:available))
    
    # ------------------------------------------------------------------------------------- 
    # connect to the chatroom
    roomID = roomName + "/" + jabberClient.jid.node
    roomJID = Jabber::JID::new(roomID)
    room = Jabber::MUC::MUCClient.new(jabberClient) room.join(roomJID)
    
    # ------------------------------------------------------------------------------------- 
    # weather
    def getWeather(m)
        begin
            words = m.body.downcase.split("weather") 
            where = defaultWeatherZipCode
            if (words.length == 2)
                where = words[1].strip 
            end
            weather = get_weather_for_city(where,'f') 
        rescue
            weather = "Couldn't get weather for that location - try zip code" 
        end
        return weather 
    end
    
    # ------------------------------------------------------------------------------------- 
    # integration
    def startIntegration(m)
        begin
            words = m.body.split("integrate") 
            botName = "Invalid BOT Name"
            if (words.length == 2)
                botName = words[1].strip 
            end
            integrationMessage = jeevesIntegration(botName) 
        rescue
            integrationMessage = "Failed integrating #{botName}" 
        end
        return integrationMessage 
    end
    
    # ------------------------------------------------------------------------------------- 
    # listen for messages in chatroom (this callback will run in a separate thread) 
    room.add_message_callback do |m|
        if (m.x.nil?) # the msg is current 
            if m.type != :error
                body = m.body;
                if (body.downcase.include? "jeeves")
    
                    # assume Jeeves does not understand command
                    understood = 0
    
                    # exit Jeeves
                    if (body.downcase.include? "exit") 
                        understood = 1
                        message = "Good-bye"
                        mainthread.wakeup
                    end
    
                    # Weather
                    if (body.downcase.include? "weather") 
                        understood = 1
                        message = getWeather(m) 
                    end
    
                    # Integrate BOT
                    if (body.downcase.include? "integrate") 
                        understood = 1
                        message = startIntegration(m) 
                    end
    
                    # Jeeves doesn't understand command
                    if (understood == 0)
                        message = "I don't understand that command!"
                    end
    
                    # let user know what has happened
                    roomMessage = Jabber::Message.new(roomJID, message)
                    room.send(roomMessage)
                end
            end
        end
    end
    
    
    # ------------------------------------------------------------------------------------- 
    # add the callback to respond to server ping (to keep the connect alive)
    jabberClient.add_iq_callback do |iq_received|
        if iq_received.type == :get
            if iq_received.queryns.to_s != 'http://jabber.org/protocol/disco#info'
                iq = Jabber::Iq.new(:result, jabberClient.jid.node) 
                iq.id = iq_received.id
                iq.from = iq_received.to
                iq.to = iq_received.from
                jabberClient.send(iq) 
            end
        end 
    end
    
    # ------------------------------------------------------------------------------------- 
    # stop the main thread (the call back will still be alive this way)
    print "Connected to chat room...\n"
    Thread.stop
    print "Disconnected from chat room...\n"
    
    # leave chat room and log out of Jabber
    room.exit 
    jabberClient.close
    

    上面的Jeeves经理文件使用了另外两个补充文件。下面的第一个处理天气预报和格式化,第二个处理开始集成。

    ######### Weather #########
    require 'rexml/document' 
    require 'open-uri' 
    require 'net/smtp'
    
    # ------------------------------------------------------------------------------------- 
    # yahoo weather url info
    # http://developer.yahoo.net/weather/#examples
    
    # ------------------------------------------------------------------------------------- 
    #Returns a hash containing the location and temperature information
    #Accepts US zip codes or Yahoo location id's
    def yahoo_weather_query(loc_id, units)
        h = {}
        open("http://xml.weather.yahoo.com/forecastrss?p=#{loc_id}&u=#{units}") do |http|
        response = http.read
        doc = REXML::Document.new(response)
        root = doc.root
        channel = root.elements['channel']
        location = channel.elements['yweather:location']
        h[:city] = location.attributes["city"]
        h[:region] = location.attributes["region"]
        h[:country] = location.attributes["country"]
        h[:temp] = channel.elements["item"].elements["yweather:condition"].attributes["temp"]         
        h[:text] = channel.elements["item"].elements["yweather:condition"].attributes["text"] 
        h[:wind_speed] = channel.elements['yweather:wind'].attributes['speed']
        h[:humidity] = channel.elements['yweather:atmosphere'].attributes['humidity'] 
        h[:sunrise] = channel.elements['yweather:astronomy'].attributes['sunrise']
        h[:sunset] = channel.elements['yweather:astronomy'].attributes['sunset']
        h[:forecast_low] = channel.elements["item"].elements['yweather:forecast'].attributes['low']
        h[:forecast_high] = channel.elements["item"].elements['yweather:forecast'].attributes['high'] end
        return h
    end
    
    # -------------------------------------------------------------------------------------
    def get_weather_for_city(city_code,units)
        weather_info = yahoo_weather_query(city_code, units)
        city = weather_info[:city]
        region = weather_info[:region]
        country = weather_info[:country]
        temp = weather_info[:temp]
        wind_speed = weather_info[:wind_speed]
        humidity = weather_info[:humidity]
        text = weather_info[:text]
        sunrise = weather_info[:sunrise]
        sunset = weather_info[:sunset]
        forecast_low = weather_info[:forecast_low] 
        forecast_high = weather_info[:forecast_high]
    
        return "#{city}, #{region}:\n" + " Currently #{temp} degrees, #{humidity}% humidity, #{wind_speed} mph winds, #{text}.\n" + " Forecast: #{forecast_low} low, #{forecast_high} high.\n" + " Sunrise: #{sunrise}, sunset: #{sunset}.\n"
    end
    

    最后,这是启动Messages app

    集成的脚本
    require 'json' 
    require 'open-uri' 
    require 'openssl'
    
    # -------------------------------------------------------------------------------------
    def jeevesIntegration(botToIntegrate)
    
        # credentials
        domain = "<domain>.local"
        endpoint = "https://#{domain}:20343"
        user = "your-integration-username (not Jeeves)" 
        password = "password"
    
        # return message
        message = "Bot '#{botToIntegrate}' does not exist on server #{domain}"
    
        # request JSON construct with all the BOTS
        botsRequestURI = URI.parse("#{endpoint}/api/bots")
        output = open(botsRequestURI, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}) 
        bots = JSON.parse(output.readlines.join(""))
    
        # loop through full list of BOTS for the one we're interested in
        bots['results'].each do |bot| 
            botName = bot['name']
            if (botName.downcase == botToIntegrate.downcase) 
                botID = bot['_id']
    
                # curl -k -X POST -u "#{user}:#{password}" "#{endpoint}/api/bots/#{botid}/integrations" -i
    
                # ------------------------------------------------------------------- 
                # kickoff integration
                uri = URI.parse(endpoint)
                http = Net::HTTP.new(uri.host, uri.port)
                http.use_ssl = true
                http.verify_mode = OpenSSL::SSL::VERIFY_NONE
                request = Net::HTTP::Post.new("/api/bots/#{botID}/integrations")
                request.basic_auth(user, password)
                response = http.request(request)
                message = "Integrating #{botName} on server #{domain}" 
            end
        end
    
        return message 
    end
    

答案 1 :(得分:2)

是的,正如我answered here,您首先需要找到机器人_id,然后向机器人的终端发送POST请求。有关详细信息,请参阅链接。

答案 2 :(得分:1)

  

我想在github上添加post-receive hook,在接收任何提交时应该在我的OS X Server上启动Xcode bot。

如果你想在提交的基础上建立起来&#39;然后在创建机器人时选择该选项。您可以选择运行bot 手动定期提交时。后者做你所描述的。只要您的一个团队成员对您的github仓库进行更改,Xcode服务器就会进行构建。