-->

Using script to fire Xcode bot

2020-02-05 11:07发布

问题:

Is there a way to manually fire existing Xcode bots using shell scripts? I have a manual bot and I'd like to fire it based on certain custom logic criteria.

回答1:

Yes.

You'll need to do a couple of things: Firstly, I'm going to call your Xcode Server's IP address XCS_IP, usually localhost if you're on the machine where Xcode Server's running.

  • Find out the ID of the bot: in Terminal, run curl -k "https://XCS_IP:20343/api/bots". Copy the output to some editor and find the value for key _id for your bot, will be something like 6b3de48352a8126ce7e08ecf85093613. Let's call it BOT_ID.

  • Trigger an integration by running curl -k -X POST -u "username:password" "https://XCS_IP:20343/api/bots/BOT_ID/integrations" -i

Where username and password are credentials of a user that is allowed to create bots on the server, an admin will do.

If you're interested in more details, I have an app in Swift that uses that API and many more: https://github.com/czechboy0/Buildasaur/blob/master/BuildaCIServer/XcodeServer.swift#L324

And checkout my article on how to find Xcode Server's API "documentation": http://honzadvorsky.com/blog/2015/5/4/under-the-hood-of-xcode-server.

TL;DR? On your Mac, look at /Applications/Xcode.app/Contents/Developer/usr/share/xcs/xcsd/routes/routes.js, where you can find the available APIs.

Hope this helped.



回答2:

Apple has added documentation for the Xcode server API that you can use to trigger bots.

https://developer.apple.com/library/tvos/documentation/Xcode/Conceptual/XcodeServerAPIReference/index.html#//apple_ref/doc/uid/TP40016472-CH1-SW1

Below is some example code on how you can make a python script that triggers a bot.

import requests

xcodeIP = '1.2.3.4.5'

def main():
    botName = "name of bot"
    runBot(botName)

def runBot(botName):
    requests.post(xcodeIP + '/api/bots/' + getBot(botName)["_id"] + '/integrations', auth=('username', 'password'), verify=False)

def getBot(botName):
    botIDRequest = requests.get(xcodeIP + '/api/bots', auth=('username', 'password'), verify=False)
    bots = botIDRequest.json()["results"]
    for bot in bots:
        if bot["name"] == botName:
            return bot

if __name__ == "__main__":
    main()