forked from fastapi/fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitter_releases_bot.py
67 lines (51 loc) · 1.73 KB
/
gitter_releases_bot.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
import inspect
import os
import requests
room_id = "5c9c9540d73408ce4fbc1403" # FastAPI
# room_id = "5cc46398d73408ce4fbed233" # Gitter development
gitter_token = os.getenv("GITTER_TOKEN")
assert gitter_token
github_token = os.getenv("GITHUB_TOKEN")
assert github_token
tag_name = os.getenv("TAG")
assert tag_name
def get_github_graphql(tag_name: str):
github_graphql = """
{
repository(owner: "tiangolo", name: "fastapi") {
release (tagName: "{{tag_name}}" ) {
description
}
}
}
"""
github_graphql = github_graphql.replace("{{tag_name}}", tag_name)
return github_graphql
def get_github_release_text(tag_name: str):
url = "https://api.github.com/graphql"
headers = {"Authorization": f"Bearer {github_token}"}
github_graphql = get_github_graphql(tag_name=tag_name)
response = requests.post(url, json={"query": github_graphql}, headers=headers)
assert response.status_code == 200
data = response.json()
return data["data"]["repository"]["release"]["description"]
def get_gitter_message(release_text: str):
text = f"""
New release! :tada: :rocket:
(by FastAPI bot)
## {tag_name}
"""
text = inspect.cleandoc(text) + "\n\n" + release_text
return text
def send_gitter_message(text: str):
headers = {"Authorization": f"Bearer {gitter_token}"}
url = f"https://api.gitter.im/v1/rooms/{room_id}/chatMessages"
data = {"text": text}
response = requests.post(url, headers=headers, json=data)
assert response.status_code == 200
def main():
release_text = get_github_release_text(tag_name=tag_name)
text = get_gitter_message(release_text=release_text)
send_gitter_message(text=text)
if __name__ == "__main__":
main()