|
| 1 | +''' |
| 2 | +Helper script for issue #4: https://github.com/sio/cirrus-run/issues/4 |
| 3 | +
|
| 4 | +Find recent builds that contain more than one task. This is helpful for finding |
| 5 | +builds where a single defined task was automatically restarted by CirrusCI due |
| 6 | +to GCP instance termination. |
| 7 | +
|
| 8 | +Execute `make debug/find_multiple_tasks` from repo top-level directory |
| 9 | +''' |
| 10 | + |
| 11 | +import os |
| 12 | +from cirrus_run import CirrusAPI |
| 13 | +from cirrus_run.cli import ENVIRONMENT |
| 14 | +from cirrus_run.queries import get_repo |
| 15 | + |
| 16 | + |
| 17 | +def main(*a, **ka): |
| 18 | + token = os.environ.get(ENVIRONMENT['token'], "") |
| 19 | + api = CirrusAPI(token) |
| 20 | + owner, repo = os.environ[ENVIRONMENT['github']].split('/') |
| 21 | + repo_id = get_repo(api, owner, repo) |
| 22 | + |
| 23 | + print('Recent builds in {owner}/{repo} with more than 1 task:'.format(**locals())) |
| 24 | + for url in filter_builds(api, repo_id): |
| 25 | + print(' {}'.format(url)) |
| 26 | + |
| 27 | + |
| 28 | +def filter_builds(api, repo_id): |
| 29 | + query = ''' |
| 30 | + query tasks_by_job($repo_id: ID!) { |
| 31 | + repository(id: $repo_id) { |
| 32 | + builds(last: 100) { |
| 33 | + edges { |
| 34 | + node { |
| 35 | + id |
| 36 | + tasks { |
| 37 | + id |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + ''' |
| 45 | + params = dict(repo_id=repo_id) |
| 46 | + response = api(query, params) |
| 47 | + url = 'https://cirrus-ci.com/build/{}' |
| 48 | + |
| 49 | + for build in response['repository']['builds']['edges']: |
| 50 | + if len(build['node']['tasks']) > 1: |
| 51 | + build_id = build['node']['id'] |
| 52 | + yield url.format(build_id) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + main() |
0 commit comments