diff --git a/.github/ISSUE_TEMPLATE/process_har.yaml b/.github/ISSUE_TEMPLATE/process_har.yaml new file mode 100644 index 0000000..acfb68e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/process_har.yaml @@ -0,0 +1,43 @@ +name: Process HAR +description: 请按照提示, 上传或修改 HAR 模板文件 +title: " 评论区" +labels: ["har"] +body: + - type: input + id: name + attributes: + label: HAR 模板名称 + description: | + 请提供 HAR 文件的命名, 空格请用下划线代替, 允许中文 + placeholder: "Example_HARNAME" + validations: + required: true + - type: input + id: author + attributes: + label: 作者信息 + description: | + 请提供 HAR 文件的作者名或昵称, 用英文逗号 `,` 分隔多个作者 + placeholder: "Example_Author1, Example_Author2" + validations: + required: false + - type: textarea + id: comments + attributes: + label: 模板备注及说明 + description: | + HAR 文件的备注信息及说明, 选填 + - type: input + id: filename + attributes: + label: HAR 文件名 + description: | + 请提供 HAR 文件的文件名, 含 `.har` 后缀, 允许中文 + placeholder: "Example_HARNAME.har" + - type: textarea + id: har_content + attributes: + label: HAR 模板内容 + description: | + 请将 HAR 文件内容粘贴到此处, 建议使用 json 格式化工具进行格式化后再粘贴 + render: JSON diff --git a/.github/src/extract_issue_body.py b/.github/src/extract_issue_body.py new file mode 100644 index 0000000..d1b86e1 --- /dev/null +++ b/.github/src/extract_issue_body.py @@ -0,0 +1,68 @@ +import base64 +import json +import os +import time + +with open('tpls_history.json', 'r', encoding='utf8') as f: + hfile = json.loads(f.read()) +issue_body = os.getenv('ISSUE_JSON','{}') +issue_json:dict = json.loads(issue_body) +repo_full_name = os.getenv('REPO_FULL_NAME','') +repo_default_branch = os.getenv('REPO_DEFAULT_BRANCH','') +if len(issue_json) > 0 and 'name' in issue_json and 'author' in issue_json and 'filename' in issue_json and 'har_content' in issue_json: + commenturl = os.getenv('ISSUE_URL','') + issue_json['name'] = issue_json['name'].replace(' ','_') + issue_json['filename'] = issue_json['filename'].replace(' ','_') + har_content = issue_json['har_content'].replace('```JSON','').replace('```','').strip() + try: + har_content = json.loads(har_content) + except: + os._exit(0) + + update = False + if 'har' not in hfile or not isinstance(hfile['har'], dict): + hfile['har'] = {} + elif issue_json['name'] in hfile['har']: + update = True + else: + for k,v in list(hfile['har'].items()): + if v['commenturl'] == commenturl: + hfile['har'][issue_json['name']] = v + update = True + hfile['har'].pop(k) + break + + if not issue_json['filename']: + issue_json['filename'] = issue_json['name'] + '.har' + if not issue_json['filename'].endswith('.har'): + issue_json['filename'] = issue_json['filename'] + '.har' + if update and issue_json['filename'] != hfile['har'][issue_json['name']]['filename'] and os.path.exists(hfile['har'][issue_json['name']]['filename']): + os.remove(hfile['har'][issue_json['name']]['filename']) + + content_jsonstring = json.dumps(har_content, indent=4, ensure_ascii=False) + with open(issue_json['filename'], 'w', encoding='utf8') as f: + f.write(content_jsonstring) + + har = { + 'name': issue_json['name'], + 'author': issue_json['author'], + 'url': 'https://raw.githubusercontent.com/'+ repo_full_name + '/' + repo_default_branch + '/' + issue_json['filename'], + 'update': update, + 'comments': issue_json.get('comments','').replace('\\r', '\r').replace('\\n', '\n').replace('\r','').replace('\n','
'), + 'filename': issue_json['filename'], + 'content': base64.b64encode(content_jsonstring.encode('utf8')).decode('utf8'), + 'date': hfile['har'][issue_json['name']]['date'] if update else time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), + 'version': hfile['har'][issue_json['name']]['version'] if update else time.strftime('%Y%m%d', time.localtime()), + 'commenturl': commenturl + } + + # 判断更新后的hfile['har'][issue_json['name']]是否与原来的一致,如果一致则不更新 + if not hfile['har'].get(issue_json['name']): + hfile['har'][issue_json['name']] = har + elif update and hfile['har'][issue_json['name']] != har: + hfile['har'][issue_json['name']] = har + hfile['har'][issue_json['name']]['date'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + hfile['har'][issue_json['name']]['version'] = time.strftime('%Y%m%d', time.localtime()) + + with open('tpls_history.json', 'w', encoding='utf8') as f: + f.write(json.dumps(hfile, indent=4, ensure_ascii=False)) diff --git a/.github/workflows/process_har.yaml b/.github/workflows/process_har.yaml new file mode 100644 index 0000000..42bd677 --- /dev/null +++ b/.github/workflows/process_har.yaml @@ -0,0 +1,93 @@ +name: HAR_Process +on: + workflow_dispatch: {} + issues: + types: [edited, labeled] + +env: + TZ: Asia/Shanghai + REPO_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + +jobs: + extract-issue-body: + runs-on: ubuntu-latest + # Only run if the issue is not a PR and is labeled by har + if: github.event.issue.pull_request == null && contains(github.event.issue.labels.*.name, 'har') + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.11 + uses: actions/setup-python@v3 + with: + python-version: "3.11" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - uses: stefanbuck/github-issue-parser@v3 + id: issue-parser + with: + template-path: .github/ISSUE_TEMPLATE/process_har.yaml + + - name: Echo issue body + run: | + echo "${{ steps.issue-parser.outputs.jsonString }}" + + - name: Obtain HARNAME + env: + ISSUE_JSON: ${{ steps.issue-parser.outputs.jsonString }} + id: harname + run: | + harname=$(python3 -c """ + import json + import os + + issue_json:dict = json.loads(os.getenv('ISSUE_JSON','{}')) + if len(issue_json) > 0 and 'name' in issue_json: + print(issue_json['name']) + else: + print('') + """) + echo "harname=${harname}" >> $GITHUB_OUTPUT + + - name: Judge HAR is update + env: + ISSUE_JSON: ${{ steps.issue-parser.outputs.jsonString }} + id: isupdate + run: | + isupdate=$(python3 -c """ + import json + import os + + with open('tpls_history.json', 'r', encoding='utf8') as f: + hfile = json.loads(f.read()) + issue_json:dict = json.loads(os.getenv('ISSUE_JSON','{}')) + if len(issue_json) > 0 and 'name' in issue_json: + if (issue_json['name'] in hfile['har']): + print('Update') + else: + print('Add') + else: + print('Add') + """) + echo "isupdate=${isupdate}" >> $GITHUB_OUTPUT + + + - name: Extract issue to json + env: + ISSUE_JSON: ${{ steps.issue-parser.outputs.jsonString }} + ISSUE_URL: ${{ github.event.issue.html_url }} + REPO_FULL_NAME: ${{ github.repository }} + run: | + python .github/src/extract_issue_body.py + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "${{ steps.isupdate.outputs.isupdate }} HAR: ${{ steps.harname.outputs.harname }}" + title: "${{ steps.isupdate.outputs.isupdate }} HAR: ${{ steps.harname.outputs.harname }}" + body: "Auto create pull request by HAR_Process action.\n\nIssue: ${{ github.event.issue.html_url }}\n\nAuthor: @${{ github.event.issue.user.login }}" + branch: process-har-${{ github.event.issue.number }} + delete-branch: true + base: ${{ env.REPO_DEFAULT_BRANCH }} diff --git a/.github/workflows/update_content.yaml b/.github/workflows/update_content.yaml index 4639ebc..d318f82 100644 --- a/.github/workflows/update_content.yaml +++ b/.github/workflows/update_content.yaml @@ -3,12 +3,14 @@ on: workflow_dispatch: {} push: paths: [tpls_history.json] - pull_request: - paths: [tpls_history.json] + branches: + - master + - main jobs: sync: runs-on: ubuntu-latest + if: github.event_name == 'workflow_dispatch' || github.event_name == 'push' && !contains(github.event.head_commit.author.name, '[bot]') steps: - uses: actions/checkout@v3 - name: Set up Python 3.11 diff --git a/README.md b/README.md index 7adba7f..aac0bc4 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,49 @@ Tips: 1. 链接里最好使用 `raw.githubusercontent.com` 的模板地址,其他的链接没有测试过 2. 修改日期格式 `四位年-两位月-两位日 24小时:两位分:两位秒` ,例子:`2020-05-15 07:03:47` +## 😀使用 Issue 发布模板 + +1. 进入 Issue 界面 +2. 点击右上角 `New issue` 按钮, 选择 `Process HAR` 对应的 `Get Started` 按钮 +3. 标题填写 "模板名称 评论区",内容按照要求的格式进行填写 +4. 点击 `Submit new issue` 按钮 +5. 等待 Github Actions 运行完成, 会自动进行 PR +6. 等待模板审核通过, 仓库拥有者会进行合并 +7. 合并完成后, 即完成模板的发布 + +> Tips: +> +> 1. 请勿在 Issue 里发布其他内容, 否则会被关闭 +> 2. 更新模板直接在 Issue 里编辑内容, 不要重新发布 Issue + +> **PS: 旧的 HAR 模板如何更新?** +> +> 参考如下格式编辑并更新 HAR 模板所对应的 Issue 评论区内容: +> +> ~~~markdown +> ### HAR 模板名称 +> +> 请将此行替换为**模板名称** ( HAR 文件的命名, 空格请用下划线代替, 允许中文) +> +> ### 作者信息 +> +> 请将此行替换为**作者信息** ( HAR 文件的作者名或昵称, 用英文逗号 `,` 分隔多个作者) +> +> ### 模板备注及说明 +> +> 请在此输入模板备注及说明 ( HAR 文件的备注及说明, 直接换行即可, 无需
) +> +> ### HAR 文件名 +> +> 请将此行替换为 **HAR 文件名** ( HAR 文件的文件名, 含 `.har` 后缀, 允许中文) +> +> ### HAR 模板内容 +> +> ```JSON +> 请将此行替换为 **HAR 模板内容** (请粘贴 HAR 文件内容, 建议使用 json 格式化工具进行格式化后再粘贴) +> ``` +> ~~~ + ## 📄如何注册第三方库 20211021版本已经开放注册第三方库的功能,默认提供 仓库,如果需要自建第三方库,请注意一下几点: @@ -38,13 +81,13 @@ Tips: "version":"版本号 yyyymmdd", "har": { "必填,和name保持一致,注意要在文件里保持唯一": { - "name": "必填", - "author": "选填,作者", - "url": "选填,har链接", - "update": false, - "comments": "选填,har文件的注释,可用来解释har所需变量的说明", - "filename": "必填,content为空时通过此来读取har", - "content": "选填,不填则根据 filename 的值来读取对应的har文件,默认为base64编码", + "name": "必填", + "author": "选填,作者", + "url": "选填,har链接", + "update": false, + "comments": "选填,har文件的注释,可用来解释har所需变量的说明", + "filename": "必填,content为空时通过此来读取har", + "content": "选填,不填则根据 filename 的值来读取对应的har文件,默认为base64编码", "date": "必填, 日期", "version":"必填, 版本号 yyyymmdd,框架通过版本号来判断是否更新模板", "commenturl":"选填,模板对应的评论区,留空时不显示按钮" @@ -68,6 +111,7 @@ Tips: [模板书写规范](https://github.com/github-h/qiandao-templates/blob/self-bak/README.md) + ## 💝鸣谢 - [gxitm](https://github.com/gxitm)