Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created Jul 2, 2019
Embed
What would you like to do?
This is a simple script to look at projects configuration in SemaphoreCI 1.0.

This is a simple script to look at projects configuration in SemaphoreCI 1.0.

import requests
# Need to customize this.
AUTH_TOKEN = 'XXXX'
ORG = 'XXXX'
# Globals
AUTH = {'auth_token': AUTH_TOKEN}
projects = []
# Step 1: find all projects in the org.
# There is pagination that we need to be aware of (30 items per response).
page = 1
while True:
r = requests.get('https://api.semaphoreci.com/v2/orgs/{}/projects?page={}'.format(ORG, page), AUTH).json()
projects += r
if len(r) < 30:
break
page += 1
projects = {p['name']:p for p in projects}
project_names = projects.keys()
configs = {}
total = len(project_names)
# Step 2: retrieve project settings
# API is internal so it may break without notice.
# These are the same rest endpoints used by the UI to update the settings.
# AFAIK, there isn't an API to get settings otherwise.
for i, name in enumerate(sorted(project_names)):
print('{}/{} {}'.format(i+1, total, name))
othername = projects[name]['html_url'].rsplit('/',1)[-1]
if othername != name:
print("\t{} => {}".format(name, othername))
config_url = 'https://semaphoreci.com/api/internal/{}/{}/thread_configs'.format(ORG, othername)
config = requests.get(config_url, AUTH).json()
configs[name] = config
# Step 3: list setup and after job steps.
# I'm using this to check which jobs aren't up to date.
for i, name in enumerate(sorted(project_names)):
print('{}/{} {}'.format(i+1, total, name))
setup = list(filter(lambda c: c['type'] == 'setup', configs[name]))
after = list(filter(lambda c: c['type'] == 'post_thread', configs[name]))
if setup:
for command in setup[0]['commands']:
print("\tpre:{}".format(command))
if after:
for command in setup[0]['commands']:
print("\tpost:{}".format(command))
Copyright (c) 2019 Damien Nozay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment