|
| 1 | +# Copyright (C) <2021> Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +'''Script to roll owt-deps-webrtc revision. |
| 6 | +
|
| 7 | +This script is expected to be ran by GitHub action runners when a new change is |
| 8 | +committed to owt-deps-webrtc. It updates the revision in DEPS to use the latest |
| 9 | +owt-deps-webrtc. A change will be submitted owt-bot/owt-client-native's roll |
| 10 | +branch. And a pull request will be opened if it doesn't exist. |
| 11 | +''' |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import argparse |
| 16 | +import re |
| 17 | +import subprocess |
| 18 | +import requests |
| 19 | +import json |
| 20 | + |
| 21 | +SRC_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
| 22 | +DEPS_PATH = os.path.join(SRC_PATH, 'DEPS') |
| 23 | +# Regex expression to match commit hash of owt-deps-webrtc. |
| 24 | +REVISION_RE = re.compile( |
| 25 | + r"(?<=Var\('deps_owt_git'\) \+ '/owt-deps-webrtc' \+ '@' \+ ')[0-9a-f]{40}(?=',)") |
| 26 | +TARGET_BRANCH = 'roll' |
| 27 | + |
| 28 | + |
| 29 | +def webrtc_revision(): |
| 30 | + '''Return current owt-deps-webrtc revision in DEPS.''' |
| 31 | + with open(DEPS_PATH, 'r') as f: |
| 32 | + deps = f.read() |
| 33 | + return re.search(REVISION_RE, deps).group(0) |
| 34 | + |
| 35 | + |
| 36 | +def roll(revision): |
| 37 | + '''Update DEPS.''' |
| 38 | + with open(DEPS_PATH, 'r+') as f: |
| 39 | + deps = f.read() |
| 40 | + new_deps = re.sub(REVISION_RE, revision, deps) |
| 41 | + f.seek(0) |
| 42 | + f.truncate() |
| 43 | + f.write(new_deps) |
| 44 | + return |
| 45 | + |
| 46 | + |
| 47 | +def commit_message(old_revision, new_revision): |
| 48 | + message = 'Roll WebRTC revision %s..%s.' % ( |
| 49 | + old_revision[:8], new_revision[:8]) |
| 50 | + return message |
| 51 | + |
| 52 | + |
| 53 | +def commit(old_revision, new_revision): |
| 54 | + '''Create a git commit.''' |
| 55 | + message = commit_message(old_revision, new_revision) |
| 56 | + commands = [['git', 'config', 'user.name', 'owt-bot'], ['git', 'config', 'user.email', |
| 57 | + '82484650+owt-bot@users.noreply.github.com']] |
| 58 | + # Create a git commit with message above. |
| 59 | + commands.extend([['git', 'add', 'DEPS'], ['git', 'commit', '-m', message]]) |
| 60 | + # Force push because when a new version of owt-deps-webrtc is available, the old commit could be rewritten. |
| 61 | + commands.append(['git', 'push', '-f', 'https://github.com/owt-bot/owt-client-native.git', |
| 62 | + 'HEAD:refs/heads/%s' % TARGET_BRANCH]) |
| 63 | + # Run commands. |
| 64 | + for c in commands: |
| 65 | + ret = subprocess.call(c, cwd=SRC_PATH) |
| 66 | + if ret != 0: |
| 67 | + return ret |
| 68 | + return 0 |
| 69 | + |
| 70 | + |
| 71 | +def pr(old_revision, new_revision, base_branch, token): |
| 72 | + '''Create a pull request. If a PR is already open for webrtc roller, do nothing.''' |
| 73 | + # Check if a pull request exists. |
| 74 | + # REST API ref: https://docs.github.com/en/rest/reference/pulls#list-pull-requests |
| 75 | + params = {'base': base_branch, |
| 76 | + 'head': 'owt-bot:%s' % (TARGET_BRANCH), 'state': "open"} |
| 77 | + url = 'https://api.github.com/repos/open-webrtc-toolkit/owt-client-native/pulls' |
| 78 | + headers = { |
| 79 | + "Authorization": "token %s" % token, |
| 80 | + "Accept": "application/vnd.github.v3+json" |
| 81 | + } |
| 82 | + response = requests.get(url, params=params, headers=headers) |
| 83 | + if response.status_code != 200: |
| 84 | + print('Failed to get pull request list, REST response status code is %d.' % ( |
| 85 | + response.status_code), file=sys.stderr) |
| 86 | + print('Error message: %s' % (response.text), file=sys.stderr) |
| 87 | + return -1 |
| 88 | + fetched_prs = response.json() |
| 89 | + if len(fetched_prs) > 0: |
| 90 | + for pr in fetched_prs: |
| 91 | + if pr['user']['login'] == 'owt-bot': |
| 92 | + # Update existing PR. |
| 93 | + update_url = pr['url'] |
| 94 | + print(update_url) |
| 95 | + update_params = {'title': commit_message( |
| 96 | + old_revision, new_revision)} |
| 97 | + update_response = requests.patch( |
| 98 | + update_url, json=update_params, headers=headers) |
| 99 | + print(json.dumps(update_params)) |
| 100 | + if update_response.status_code != 200: |
| 101 | + print(update_response.headers) |
| 102 | + print(update_response.content) |
| 103 | + print('Failed to update pull request, REST response status code is %d.' % ( |
| 104 | + update_response.status_code), file=sys.stderr) |
| 105 | + return -1 |
| 106 | + return 0 |
| 107 | + # Create a new PR. |
| 108 | + create_params = {'title': commit_message( |
| 109 | + old_revision, new_revision), 'head': 'owt-bot:%s' % (TARGET_BRANCH), 'base': base_branch} |
| 110 | + create_response = requests.post(url, json=create_params, headers=headers) |
| 111 | + if create_response.status_code != 201: |
| 112 | + print('Failed to create pull request, REST response status code is %d.' % ( |
| 113 | + create_response.status_code), file=sys.stderr) |
| 114 | + return -1 |
| 115 | + return 0 |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + parser = argparse.ArgumentParser() |
| 120 | + parser.add_argument( |
| 121 | + '--revision', help='owt-deps-webrtc revision to roll to.') |
| 122 | + parser.add_argument( |
| 123 | + '--base_branch', help='Pull request will be merged to this branch.', default='main') |
| 124 | + parser.add_argument( |
| 125 | + '--token', help='Personal access token to create pull requests.') |
| 126 | + opts = parser.parse_args() |
| 127 | + old_revision = webrtc_revision() |
| 128 | + ret = roll(opts.revision) or commit(old_revision, opts.revision) or pr( |
| 129 | + old_revision, opts.revision, opts.base_branch, opts.token) |
| 130 | + return ret |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + sys.exit(main()) |
0 commit comments