-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_if_topics_present.py
executable file
·178 lines (138 loc) · 5.21 KB
/
check_if_topics_present.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
#!/usr/bin/env python3
# Sesheta
# Copyright(C) 2019, 2020 Christoph Görn
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Make sure that all our repos have the minimum topics."""
import json
import logging
import os
import daiquiri
import requests
from requests.auth import HTTPBasicAuth
import sesheta
DEBUG = bool(os.getenv("DEBUG", False))
GITHUB_ACCESS_TOKEN = os.getenv("SESHETA_GITHUB_SRCOPS_ACCESS_TOKEN", None)
daiquiri.setup(level=logging.INFO)
logger = daiquiri.getLogger("topic_checker")
if DEBUG:
logger.setLevel(level=logging.DEBUG)
else:
logger.setLevel(level=logging.INFO)
logger.info(f"Version v{sesheta.__version__}")
# lets see what requests is doing
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
_QUERY = """
query GetRepositoriesAndTopics {{
organization(login:"{org_name}") {{
name
repositories(first: 100) {{
edges {{
node {{
id
name
repositoryTopics(first: 100) {{
edges {{
node {{
topic {{
name
}}
}}
}}
}}
}}
}}
}}
}}
}}
"""
_MUTATION_TEMPLATE = """
mutation SetRepositoryTopics {{
updateTopics(input: {{
repositoryId: "{repository_id}",
topicNames: {topic_names},
clientMutationId: "{id}"}}) {{
clientMutationId
invalidTopicNames
}}
}}
"""
class GraphQLClient:
"""GraphQL client."""
def __init__(self, token):
"""Init with some sane defaults."""
self.session = requests.Session()
self.token = token
self.session.headers.update({"Authorization": f"bearer {token}"})
self.session.headers.update({"User-Agent": f"thoth_sesheta_topic_checker/{sesheta.__version__}"})
def request(self, query):
"""Do a GraphQL request."""
if not self.token:
raise RuntimeError("Please set an environment variable SESHETA_GITHUB_ACCESS_TOKEN.\n")
logger.debug(f"session = {self.session.headers}")
logger.debug(f"query = {query}")
response = self.session.post(
url="https://api.github.com/graphql", json={"query": query}, auth=HTTPBasicAuth("sesheta", self.token)
)
return response
if __name__ == "__main__":
logger.info(f"Hi, I'm sesheta, and I'm fully operational now!")
logger.debug("... and I am running in DEBUG mode!")
if not GITHUB_ACCESS_TOKEN:
logger.error("GitHub Token not provided via environment variable SESHETA_GITHUB_SRCOPS_ACCESS_TOKEN")
exit(-1)
G = GraphQLClient(GITHUB_ACCESS_TOKEN)
try:
repos = G.request(_QUERY.format(org_name="thoth-station")).json()["data"]
except Exception as exp:
logger.error(exp)
exit(-1)
logger.debug(repos)
if "errors" in repos:
raise RuntimeError("There was something wrong with the repos query")
for repo in repos["organization"]["repositories"]["edges"]:
repo_id = repo["node"]["id"]
repo_name = repo["node"]["name"]
repo_topics = set()
for topic in repo["node"]["repositoryTopics"]["edges"]:
if topic["node"]["topic"]["name"] == "ansible-role":
continue
repo_topics.add(topic["node"]["topic"]["name"])
repo_topics.add("artificial-intelligence")
if "Thoth Station" in repos["organization"]["name"]:
repo_topics.add("thoth")
if "sesheta" in repo_name:
repo_topics.add("thoth")
repo_topics.add("sesheta")
repo_topics.add("bot")
repo_topics.add("cyborg")
if ("prometheus" in repo_name) or ("thanos" in repo_name):
repo_topics.add("prometheus")
if ("tensorflow" in repo_name) or ("tf-" in repo_name):
repo_topics.add("tensorflow")
if repo_name.startswith("zuul"):
repo_topics.add("zuul")
repo_topics.add("zuul-ci")
if repo_name.startswith("ansible-role"):
repo_topics.add("ansible")
repo_topics.add("ansible-roles")
if repo_name.startswith("srcops"):
repo_topics.add("srcops")
logger.info(f"repo({repo_id}): {repo_name}: {repo_topics}")
mutation = G.request(
_MUTATION_TEMPLATE.format(repository_id=repo_id, topic_names=json.dumps(list(repo_topics)), id="1")
)
logger.debug(mutation.json())