-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_to_mastodon.py
executable file
·105 lines (84 loc) · 3.16 KB
/
post_to_mastodon.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
#!/usr/bin/env python3
import argparse
import logging
import os
import re
import sys
import jinja2
from mastodon import Mastodon
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("--message", help="Message to post to Mastodon")
args = parser.parse_args()
# Configure Mastodon connection
mastodon_access_token = os.environ.get("MASTODONBOT")
mastodon_base_url = os.environ.get("MASTODON_BASE_URL", "https://fediscience.org")
if not mastodon_access_token:
logger.error("MASTODONBOT environment variable not set")
sys.exit(1)
m = Mastodon(access_token=mastodon_access_token,
api_base_url=mastodon_base_url)
pr_title = os.environ["PR_TITLE"]
if pr_title == "":
logger.error("PR_TITLE is empty")
sys.exit(1)
match = re.search(r'[Rr]elease\s+([0-9]+\.[0-9]+\.[0-9]+)', PR_TITLE)
if match:
version = match.group(1)
else:
logger.error("No version found in PR_TITLE")
sys.exit(1)
# validate version format
if not re.match(r'^[0-9]+\.[0-9]+\.[0-9]+$', version):
logger.error("Invalid version format")
sys.exit(1)
# Get repository information
github_repository = os.environ.get("GITHUB_REPOSITORY", "")
repository_url = os.environ.get("REPOSITORY_URL", "")
# If REPOSITORY_URL is not set, try to construct it from GITHUB_REPOSITORY
if not repository_url and github_repository:
repository_url = f"https://github.com/{github_repository}"
elif not repository_url:
logger.error("Neither REPOSITORY_URL nor GITHUB_REPOSITORY environment variables are set")
sys.exit(1)
# Get issue URL if available
issue_url = os.environ.get("ISSUE_URL", f"{repository_url}/issues")
# Determine changelog path/URL
# Check if a custom changelog path is provided
changelog_path = os.environ.get("CHANGELOG_PATH", "")
if changelog_path:
# Use the provided changelog path
changelog = f"{repository_url}/{changelog_path}"
else:
# Try to determine if this is a release tag or a changelog file
# Default to releases/tag format
changelog = f"{repository_url}/releases/tag/v{version}"
# Check if CHANGELOG_FILE environment variable is set
# Check if CHANGELOG_FILE environment variable is set and not empty
changelog_file = os.environ.get("CHANGELOG_FILE", "")
if changelog_file.strip():
# Use the specified changelog file instead of releases/tag
changelog = f"{repository_url}/blob/main/{changelog_file}"
# Maximum characters for Mastodon (on FediScience) is 1500
MAX_TOOT_LENGTH = int(os.environ.get("MAX_TOOT_LENGTH", 1500))
# Render the message with all available variables
template = jinja2.Template(args.message)
message = template.render(
version=version,
changelog=changelog,
issue_url=issue_url,
repository_url=repository_url
)
# check if the message is too long
if len(message) > MAX_TOOT_LENGTH:
logger.error("The received message is too long for the Mastodon Robot. We are limited to 1500 characters.")
sys.exit(1)
# post the message
try:
m.status_post(message)
except Exception as e:
logger.error(f"Failed to post to Mastodon: {e}")
sys.exit(1)
# report a successful post
logger.info("Message posted to Mastodon")