|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +import httpx |
| 8 | +from github import Github |
| 9 | + |
| 10 | + |
| 11 | +def ensure_environment_variables_are_present() -> None: |
| 12 | + required_env_vars = ( |
| 13 | + "GITHUB_RELEASE_TAG", |
| 14 | + "GITHUB_TOKEN", |
| 15 | + "SLACK_TOKEN", |
| 16 | + "SLACK_SERVICE", |
| 17 | + "SLACK_TEAM", |
| 18 | + ) |
| 19 | + |
| 20 | + for env_var in required_env_vars: |
| 21 | + if env_var not in os.environ: |
| 22 | + logging.fatal(f"❌ A required environment variable is missing: {env_var}") |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + |
| 26 | +def get_gh_release_info_text_with_token(release_tag: str, access_token: str) -> str: |
| 27 | + gh = Github(access_token) |
| 28 | + repo_name = "instana/python-sensor" |
| 29 | + repo = gh.get_repo(repo_name) |
| 30 | + release = repo.get_release(release_tag) |
| 31 | + |
| 32 | + logging.info("GH Release fetched successfully %s", release) |
| 33 | + |
| 34 | + msg = ( |
| 35 | + f":mega: Oyez! Oyez! Oyez!\n" |
| 36 | + f":package: A new version of the Python Tracer has been released.\n" |
| 37 | + f"Name: Instana Python Tracer {release.title}\n" |
| 38 | + f"Tag: {release.tag_name}\n" |
| 39 | + f"Created at: {release.created_at}\n" |
| 40 | + f"Published at: {release.published_at}\n" |
| 41 | + f"{release.body}\n" |
| 42 | + ) |
| 43 | + |
| 44 | + logging.info(msg) |
| 45 | + return msg |
| 46 | + |
| 47 | + |
| 48 | +def post_on_slack_channel( |
| 49 | + slack_team: str, slack_service: str, slack_token: str, message_text: str |
| 50 | +) -> None: |
| 51 | + """Send a message to Slack channel.""" |
| 52 | + |
| 53 | + url = ( |
| 54 | + f"https://hooks.slack.com/services/T{slack_team}/B{slack_service}/{slack_token}" |
| 55 | + ) |
| 56 | + |
| 57 | + headers = { |
| 58 | + "Content-Type": "application/json", |
| 59 | + } |
| 60 | + body = {"text": message_text} |
| 61 | + |
| 62 | + with httpx.Client() as client: |
| 63 | + response = client.post(url, headers=headers, json=body) |
| 64 | + response.raise_for_status() |
| 65 | + |
| 66 | + result = response.text |
| 67 | + if "ok" in result: |
| 68 | + print("✅ Slack message sent successfully") |
| 69 | + else: |
| 70 | + print(f"❌ Slack API error: {result}") |
| 71 | + |
| 72 | + |
| 73 | +def main() -> None: |
| 74 | + # Setting this globally to DEBUG will also debug PyGithub, |
| 75 | + # which will produce even more log output |
| 76 | + logging.basicConfig(level=logging.INFO) |
| 77 | + ensure_environment_variables_are_present() |
| 78 | + |
| 79 | + msg = get_gh_release_info_text_with_token( |
| 80 | + os.environ["GITHUB_RELEASE_TAG"], os.environ["GITHUB_TOKEN"] |
| 81 | + ) |
| 82 | + |
| 83 | + post_on_slack_channel( |
| 84 | + os.environ["SLACK_TEAM"], |
| 85 | + os.environ["SLACK_SERVICE"], |
| 86 | + os.environ["SLACK_TOKEN"], |
| 87 | + msg, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments