This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
repoxplorer-github-organization
executable file
·136 lines (122 loc) · 4.26 KB
/
repoxplorer-github-organization
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
#!/usr/bin/env python
# Copyright 2016, 2017 Fabien Boucher
# Copyright 2016, 2017 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import argparse
import sys
import yaml
import github3
parser = argparse.ArgumentParser(
description='Read a Github organization and create'
'the repoXplorer description file')
parser.add_argument(
'--token', type=str,
required=False, default=None,
help='Specify an authentication token used to fetch private repositories')
parser.add_argument(
'--org', type=str,
required=True,
help='Specify the Github organization name')
parser.add_argument(
'--repo', type=str,
required=False,
help='Specify the repository name or a regular expression')
parser.add_argument(
'--mt-stars', type=int,
required=False,
help='Only repositories in top N by stargazers count')
parser.add_argument(
'--output-path', type=str,
help='yaml file path to register organization repositories details')
parser.add_argument(
'--skip-fork', action='store_true',
help='Do not consider forked repositories')
parser.add_argument(
'--all-branches', action='store_true',
help='Include all branches in indexed repositories')
args = parser.parse_args()
if __name__ == "__main__":
gh = github3.GitHub()
if args.token:
gh.login(token=args.token)
org = gh.organization(args.org)
if not org:
print((
"Org %s not found, try to find single"
" user's repos ..." % args.org))
if not args.token:
repos = gh.repositories_by(args.org)
else:
repos = gh.repositories(type='owner')
else:
repos = org.repositories()
templates = {
args.org: {
"branches": ["master", ],
"uri": "http://github.com/%s/" % args.org +
"%(name)s",
"gitweb": "http://github.com/%s/" % args.org +
"%(name)s/commit/%%(sha)s"}
}
projects = {
args.org: {
"repos": {},
"description": "The %s Github organization" % args.org,
}
}
if args.mt_stars:
stars = []
for r in repos:
stars.append((r.name, int(r.stargazers_count)))
stars_sorted = sorted(stars, key=lambda s: s[1], reverse=True)
top = stars_sorted[:args.mt_stars]
top = [t[0] for t in top]
if args.repo:
repo_re = re.compile(args.repo)
for r in repos:
if r.fork and args.skip_fork:
continue
if args.repo and not repo_re.match(r.name):
continue
if args.mt_stars:
if r.name not in top:
continue
data = {r.name: {"template": args.org}}
# Modified for addition of `--all-branches` option.
# Revised to follow morruci's advice (set converted).
branches = set([r.default_branch])
if args.all_branches:
for branch in r.branches():
branches.add(branch.name)
data[r.name]['branches'] = list(branches)
projects[args.org]["repos"].update(data)
print("Found %s" % r.name)
struct = {'projects': projects,
'project-templates': templates}
path = '%s.yaml' % args.org
if args.output_path:
path = os.path.expanduser(args.output_path)
if not (path.endswith('.yaml') or path.endswith('.yml')):
path += '.yaml'
with open(path, 'w') as fd:
fd.write(yaml.safe_dump(struct,
default_flow_style=False))
print()
print(("%s source repositories details"
" has been written to %s" % (args.org, path)))
print("Please edit the yaml file if needed (like adding additional"
" branches to index, defines custom releases, ...)")
sys.exit(0)