码迷,mamicode.com
首页 > 编程语言 > 详细

python为Gitalk自动创建Issue

时间:2021-01-26 12:36:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:actions   str   hashlib   data   enc   编写   import   upd   require   

背景:gitalk需要人工访问文章页才能触发创建issue
其实有个更简便的方式,当我们push代码到github时,可以利用github action的自动化流程自动执行下面的python脚本来创建issue,在编写Github Action时,设定触发条件为push,详细的yml脚本内容为:
createissue.yml

name: Execute python script to create github issue

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: checkout actions
        uses: actions/checkout@v1        

      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

      - name: Create github issue
        env:
            GITHUB_TOKEN: ${{ secrets.GRIDEA }}
        run: |
          GITHUB_TOKEN=${GITHUB_TOKEN} python createissue.py

*python脚本如下 createissue.py *

#!/usr/bin/python3
import xml.etree.ElementTree as etree
import requests,json
import hashlib,os

api_user=‘lonbaw‘  #github用户名
api_token=os.environ.get(‘GITHUB_TOKEN‘) #申请的github访问口令

repo=‘lonbaw.github.io‘  #存储issue的repo
endpoint=‘https://lonbaw.github.io/atom.xml‘   #网站+/atom.xml

ssion=requests.session()
result=ssion.get(url=endpoint,verify=False)
result.encoding=‘utf-8‘ #修复中文乱码问题

def md5(s):
    hash = hashlib.md5()
    hash.update(s.encode(‘utf8‘))
    return hash.hexdigest()

root = etree.fromstring(result.text)
all_entrys=root.findall(‘{http://www.w3.org/2005/Atom}entry‘)

headers = {
    "Authorization" : "token {}".format(api_token),
    "Accept": "application/vnd.github.v3+json"
}

for i in range(len(all_entrys)):
    _title=all_entrys[i].find(‘{http://www.w3.org/2005/Atom}title‘).text
    _url=all_entrys[i].find(‘{http://www.w3.org/2005/Atom}id‘).text
    # 从"https://lonbaw.github.io/post/test/" 中截取 "/post/test/"
    label_id=md5(_url[24:])
    data = {
        "title": _title+‘ | lonbaw‘,
        "labels": [‘Gitalk‘,label_id],
        "body": _url
    }
    _get_issue=ssion.get(url=‘https://api.github.com/repos/{}/{}/issues?labels=Gitalk,{}‘.format(api_user,repo,label_id),verify=False)

    if not _get_issue.json():
        print(f‘post 《 {_title} 》是新发布的文章,开始创建issue!‘)
        _result=ssion.post(url=‘https://api.github.com/repos/{}/{}/issues‘.format(api_user,repo),headers=headers,data=json.dumps(data),verify=False)
        if _result.status_code==201:
            print(f‘post 《 {_title} 》create issue success !‘)
        else:
            print(f‘post 《 {_title} 》create issue failed!!!,reson: {_result.text}‘) 
        continue       
    print(f‘post 《 {_title} 》是旧文章‘)
ssion.close()

需要满足的目录结构为:

requirements.txt
createissue.py 
.github
└── workflows
    └── createissue.yml

python为Gitalk自动创建Issue

标签:actions   str   hashlib   data   enc   编写   import   upd   require   

原文地址:https://www.cnblogs.com/lonbaw/p/14324582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!