forked from ros/rosdistro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-ros2-gbp-devel-branch.py
260 lines (218 loc) · 11.9 KB
/
sync-ros2-gbp-devel-branch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Copyright (c) 2020, Open Source Robotics Foundation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# This is a program to sync rosdistro source entries with the release repository devel_branch.
#
# When doing a release of a ROS package through bloom, it is possible
# to set the branch in the rosdistro 'source' entry different from the
# "devel_branch" that is used in the release repository. This is
# not recommended, as doing further releases will now cause bloom
# to look at the "wrong" branch for tags. The situation gets even
# worse since both the rosdistro 'source' entry and the devel_branch
# in the release repository can be changed by hand.
#
# Overview
# --------
# This program starts by downloading the distribution.yaml file for a
# given ROS distribution, e.g. https://github.com/ros/rosdistro/blob/master/dashing/distribution.yaml
# In its default configuration, it will also download https://github.com/ros2/ros2/blob/master/ros2.repos
# in order to determine the "core" ROS 2 packages. After finding the core packages,
# it will then go to the release repository for each package, and check to ensure that
# the devel_branch is the same as the branch listed on the rosdistro source entry.
# If they are different, it will open a pull request against the release
# distribution to synchronize them.
# Note that this program can produce false positives in corner cases, which is why it
# opens pull requests and doesn't directly push to the repositories. The pull requests it
# generates should be carefully reviewed before merging.
#
# Dependencies
# ------------
# Besides the basic Python 3 dependency, this program relies on two external packages:
# * https://pypi.org/project/PyGithub/
# * https://pypi.org/project/keyring/
#
# Credentials
# -----------
# In order to do the work that it does, this program needs Github credentials in order to open pull requests.
# In the Github developer settings, create a new Personal Access Token that has full access to the `repo`
# permission and all sub-permissions. When the token is created and it gives you a password, locally run:
#
# keyring set github-open-prs may-open-prs
#
# When it asks for a password, give it the token
#
# Usage
# -----
# usage: sync-ros-gbp-devel-branch.py [-h] [--all-repos] [--dry-run]
# distribution
#
# positional arguments:
# distribution Which ROS distribution to do the sync for
#
# optional arguments:
# -h, --help show this help message and exit
# --all-repos Check all repositories, not just core ROS 2 ones
# --dry-run Just print the differences, do not actually open PRs
import argparse
import git
import github
import keyring
import os
import sys
import tempfile
import time
import urllib.request
import yaml
def get_ros2_core_repositories(ros_distro, ros_distro_yaml):
# Now get the ros2.repos corresponding to this release, which we will use
# to constrain the list of packages that we consider to be "core".
ros2_repos_url = 'https://raw.githubusercontent.com/ros2/ros2/{ros_distro}/ros2.repos'.format(ros_distro=ros_distro)
with urllib.request.urlopen(ros2_repos_url) as response:
ros2_repos_data = response.read()
ros2_repos_yaml = yaml.safe_load(ros2_repos_data)
# Now build up the constrained list of packages to look at.
constrained_list = []
for repo in ros_distro_yaml['repositories']:
repo_dict = ros_distro_yaml['repositories'][repo]
if not 'source' in repo_dict:
print("Package '{repo}' has no source entry, skipping".format(repo=repo))
continue
source_url = repo_dict['source']['url']
item_to_delete = None
for ros2_repo in ros2_repos_yaml['repositories']:
ros2_repos_package_url = ros2_repos_yaml['repositories'][ros2_repo]['url']
if ros2_repos_package_url == source_url:
# OK, we found what we were looking for. We are going to break
# out of here and remove this from the list either way, but we
# will only add it to the constrained_list if it has both a
# 'release' section and it is on github.
item_to_delete = ros2_repo
if not 'release' in repo_dict:
print("No release section for package '{repo}', skipping".format(repo=repo))
break
release_url = repo_dict['release']['url']
if not release_url.startswith('https://github.com'):
print("Release URL {release_url} for package '{repo}' is not on GitHub, do not know how to fetch tracks.yaml data".format(release_url=release_url, repo=repo))
break
constrained_list.append(repo_dict)
break
if item_to_delete is not None:
del ros2_repos_yaml['repositories'][item_to_delete]
return constrained_list
def get_all_ros2_repositories(ros_distro_yaml):
constrained_list = []
for repo in ros_distro_yaml['repositories']:
repo_dict = ros_distro_yaml['repositories'][repo]
if not 'source' in repo_dict:
print("Package '{repo}' has no source entry, skipping".format(repo=repo))
continue
if not 'release' in repo_dict:
print("No release section for package '{repo}', skipping".format(repo=repo))
continue
release_url = repo_dict['release']['url']
if not release_url.startswith('https://github.com'):
print("Release URL {release_url} for package '{repo}' is not on GitHub, do not know how to fetch tracks.yaml data".format(release_url=release_url, repo=repo))
continue
constrained_list.append(repo_dict)
return constrained_list
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--all-repos', help='Check all repositories, not just core ROS 2 ones', action='store_true', default=False)
parser.add_argument('--dry-run', help='Just print the differences, do not actually open PRs', action='store_true', default=False)
parser.add_argument('distribution', nargs=1, help='Which ROS distribution to do the sync for', action='store')
args = parser.parse_args()
key = keyring.get_password('github-open-prs', 'may-open-prs')
if key is None:
raise RuntimeError('Failed to get GitHub API key')
gh = github.Github(key)
ros_distro = args.distribution[0]
# First get the rosdistro distribution.yaml, which we will use as the source
# of the devel_branch we should use.
rosdistro_url = 'https://raw.githubusercontent.com/ros/rosdistro/master/{ros_distro}/distribution.yaml'.format(ros_distro=ros_distro)
with urllib.request.urlopen(rosdistro_url) as response:
ros_distro_data = response.read()
ros_distro_yaml = yaml.safe_load(ros_distro_data)
if args.all_repos:
constrained_list = get_all_ros2_repositories(ros_distro_yaml)
else:
constrained_list = get_ros2_core_repositories(ros_distro, ros_distro_yaml)
# Now that we have the list of repositories constrained, iterate over each
# one, comparing what is in the tracks.yaml in the release repository to
# what is in the source entry in the <distro>/distribution.yaml
for repo in constrained_list:
release_url = repo['release']['url']
release_end = release_url[19:-4]
tracks_url = 'https://raw.githubusercontent.com/' + release_end + '/master/tracks.yaml'
with urllib.request.urlopen(tracks_url) as response:
tracks_data = response.read()
tracks_yaml = yaml.safe_load(tracks_data)
tracks_yaml_distro = tracks_yaml['tracks'][ros_distro]
if tracks_yaml_distro['devel_branch'] != repo['source']['version']:
print("Package '{reponame}' rosdistro source branch ({source_branch}) does not match release branch ({release_branch})".format(reponame=tracks_yaml_distro['name'], source_branch=repo['source']['version'], release_branch=tracks_yaml_distro['devel_branch']))
if args.dry_run:
continue
gh_body = """This PR from an automated script updates the devel_branch for {ros_distro} to match the source branch as specified in https://github.com/ros/rosdistro/{ros_distro}/distribution.yaml .
""".format(ros_distro=ros_distro)
commit_message = """Change the devel_branch for {ros_distro}.
This makes it match the source entry in https://github.com/ros/rosdistro/{ros_distro}/distribution.yaml
""".format(ros_distro=ros_distro)
branch_name = '{ros_distro}/sync-devel-branch'.format(ros_distro=ros_distro)
with tempfile.TemporaryDirectory() as tmpdirname:
gitrepo = git.Repo.clone_from(release_url, tmpdirname)
gitrepo.git.checkout('master')
branch = gitrepo.create_head(branch_name)
branch.checkout()
with open(os.path.join(tmpdirname, 'tracks.yaml'), 'r') as infp:
local_tracks_data = infp.read()
local_tracks_yaml = yaml.safe_load(local_tracks_data)
local_tracks_yaml['tracks'][ros_distro]['devel_branch'] = repo['source']['version']
with open(os.path.join(tmpdirname, 'tracks.yaml'), 'w') as outfp:
yaml.dump(local_tracks_yaml, outfp)
gitrepo.git.add(A=True)
gitrepo.index.commit(commit_message.format(ros_distro=ros_distro))
try:
gitrepo.git.push('--set-upstream', gitrepo.remote(), gitrepo.head.ref)
except git.exc.GitCommandError:
print('Could not push to release repo for {ros_distro}: {reponame}, skipping...'.format(ros_distro=ros_distro, reponame=tracks_yaml_distro['name']))
continue
gh_title = 'Update {ros_distro} devel_branch to match rosdistro source entry'.format(ros_distro=ros_distro)
gh_repo = gh.get_repo(release_end)
tries = 10
succeeded = False
while not succeeded and tries > 0:
try:
pull = gh_repo.create_pull(title=gh_title, head=branch_name, base='master', body=gh_body)
succeeded = True
except github.GithubException as e:
print('Failed to create pull request, waiting:', e)
time.sleep(30)
tries -= 1
if tries == 0:
print('Failed to create pull request and exceeded max tries, giving up')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())