Skip to content

Commit

Permalink
[mod] 添加release机器人
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingyueyixi committed Feb 12, 2023
1 parent 7bf0bcc commit 03ccdb8
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/release-post.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Release-Robot

on:
release:
types: [ published ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10" ]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

# debug
- name: Dump Env
run: env | sort

- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# GIT_TAG=$(git describe --exact-match --abbrev=0 --tags "${COMMIT}" 2> /dev/null || true)
# https://github.com/actions/actions-runner-controller/blob/acbce4b70ad1cf719f3bab94e127ca38a1576228/hack/make-env.sh
# GIT_TAG=${GITHUB_REF/refs\/tags\//}
# 这个方式在commit没有tag时,结果类似于: refs/heads/main
- name: Prepare Env
id: Prepare
run: |
GIT_TAG=${GITHUB_REF/refs\/tags\//}
GIT_COMMIT=$(git rev-parse HEAD)
echo "GIT_COMMIT=$GIT_COMMIT$">> $GITHUB_ENV
echo "GIT_TAG=$GIT_TAG">> $GITHUB_ENV
gradle --version
- name: Robot Post Message
env:
tag_name: ${{ github.event.release.tag_name }}
chat_id: ${{ secrets.TG_CI_CHAT_ID }}
bot_token: ${{ secrets.TG_CI_BOT_TOKEN }}
github_user: ${{ github.actor }}
github_token: ${{ secrets.GITHUB_TOKEN }}
run: |
python robot.py -e release
61 changes: 61 additions & 0 deletions robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
# @Date:2023/02/13 0:06
# @Author: Lu
# @Description
import argparse
import os

import requests

http = requests.Session()


class ReleaseRobot(object):

def post(self):
url = f"https://api.github.com/repos/Mingyueyixi/MaskWechat/releases/tags/{tag_name}"
headers = {
"Accept": "application/vnd.github+json",
}
if github_token:
headers["Authorization"] = github_token

res = http.get(url, headers=headers)
print(url, res.text)
json_data = res.json()
if not json_data:
raise Exception(f"{url} response is empty")

mess = f"新版本{json_data['name']}发布: {json_data['html_url']}\n"
for ass in json_data.get("assets"):
mess += "下载:" + ass["browser_download_url"] + "\n"

print(mess)
res = http.post(f"https://api.telegram.org/bot{bot_token}/sendMessage", json={
"chat_id": chat_id,
"text": mess
})
print(res.text)


def main():
parser = argparse.ArgumentParser(description='Mask Robot')
parser.add_argument('-e', '--event', metavar='pattern', required=True,
dest='event', action='store',
help='the event for robot')
args = parser.parse_args()
print(f"python {__file__} by --event {args.event}")
if args.event == "release":
ReleaseRobot().post()
else:
print("not support robot")


if __name__ == '__main__':
tag_name = os.environ.get("TAG_NAME")
github_token = os.environ.get("github_token")
chat_id = os.environ.get("chat_id")
bot_token = os.environ.get("bot_token")

tag_name = "v1.11"
main()

0 comments on commit 03ccdb8

Please sign in to comment.