Skip to content

Commit

Permalink
feat: 初始验证
Browse files Browse the repository at this point in the history
  • Loading branch information
MarleneJiang committed Aug 16, 2023
1 parent f65e5d7 commit 6aa090a
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 581 deletions.
35 changes: 0 additions & 35 deletions .github/ISSUE_TEMPLATE/automatic_review.yaml

This file was deleted.

20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/marketplace_submission.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Submit to marketplace
description: Submit your plugin to be automatically validated and published on the marketplace.
title: '[plugin/adapter/bot]: xxx'
body:
- type: markdown
attributes:
value: |
请在标题中括号内填入类型,例如:[plugin],冒号后附带插件名称,例如:[plugin]: xxx
- type: input
attributes:
label: module_name
description: 导入时的模块名称,类型为bot时不填
- type: input
attributes:
label: pypi_name
description: 导入时的模块名称,类型为bot时不填
- type: markdown
attributes:
value: |
感谢你的贡献,提交issue后将会进行自动审核。
81 changes: 81 additions & 0 deletions .github/actions_scripts/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""信息解析:1. 标题的type解析,如果不符合就报错 2. 提取name、module_name、pypi_name,如果不符合就报错 3. pypi_name在pip网站中检查,不存在则报错."""
from __future__ import annotations

import os
import re
import uuid
from pathlib import Path
from typing import Any

import requests

BASE_URL = "https://pypi.org/pypi"
CODE =404



def set_multiline_output(name:str, value:str)->None:
"""Set Github Outputs."""
with Path(os.environ["GITHUB_OUTPUT"]).open("a") as fh:
delimiter = uuid.uuid1()
print(f"{name}<<{delimiter}", file=fh)
print(value, file=fh)
print(delimiter, file=fh)

def get_response(name: str) -> dict[str, Any] | None:
"""Request response from PyPi API."""
target_url = f"{BASE_URL}/{name}/json"
response = requests.get(target_url, timeout=5)
if response.status_code == CODE:
return None
return response.json()

def check_pypi(name:str)->bool:
"""Check module filename for conflict."""
response = get_response(name)

if response:
module_name = response.get("info", {}).get("package_url", "").split("/")[-2]
return name.lower() == module_name.lower()

return False

def parse_title(title:str)->dict[str, Any]:
"""Prase Title."""
pattern = r"\[(plugin|adapter|bot)\]:\s*(.+)"
match = re.match(pattern, title)
if match:
return {"type": match.group(1), "name": match.group(2)}
msg = "标题格式错误"
raise ValueError(msg)

def main()->None:
"""信息解析:1. 标题的type解析,如果不符合就报错 2. 提取name、module_name、pypi_name,如果不符合就报错 3. pypi_name在pip网站中检查,不存在则报错."""
try:
title = os.environ["TITLE"]
except KeyError:
set_multiline_output("result", "error")
set_multiline_output("output", "Missing required input `TITLE`.")
return
try:
pypi_name = os.environ["PYPI_NAME"]
except KeyError:
set_multiline_output("result", "error")
set_multiline_output("output", "Missing required input `PYPI_NAME`.")
return
try:
parsed = parse_title(title)
except ValueError as e:
set_multiline_output("result", "error")
set_multiline_output("output", str(e))
return
if (check_pypi(pypi_name) is False):
set_multiline_output("result", "error")
set_multiline_output("output", "输入的pypi_name存在问题")
return
set_multiline_output("result", "sucess")
set_multiline_output("output", "")
set_multiline_output("type", parsed.get("type",""))
set_multiline_output("name", parsed.get("name",""))
return
main()
2 changes: 0 additions & 2 deletions .github/workflows/templates/validation-failed.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
Please fix the issues and check everything is ok locally.

Then you can trigger the validation commenting `/validate` when you are ready.

See [https://docs.docker.com/desktop/extensions-sdk/extensions/validate/](https://github.com/MarleneJiang/issue-ops/actions) for more information.
141 changes: 141 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
name: Marketplace Actions

on:
issues:
types:
- opened
- reopened
- edited
issue_comment:
types:
- created
- edited

jobs:
react-to-new-issue:
name: Post a comment to new issues
if: github.event_name == 'issues' && github.event.action == 'opened'
runs-on: ubuntu-latest
outputs:
comment-id: ${{ steps.comment.outputs.comment-id }}
steps:
- name: Add comment
id: comment
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
感谢你的提交.
自动验证将在几分钟内开始.
is-revalidation:
if: github.event.issue.state == 'open' && github.event_name == 'issue_comment'
name: Verify if the comment is a revalidation request
runs-on: ubuntu-latest
steps:
- name: Run /validate command
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
command=$(echo "$COMMENT_BODY" | head -1)
if [[ $command != "/validate"* ]]; then
echo "No /validate command found in first line of the comment \"${command}\", skipping" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Add reactions
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ github.event.comment.id }}
reactions: '+1'
parse-issue:
runs-on: ubuntu-latest
env:
REPO: ${{ github.repository }}
ISSUE_NUM: ${{ github.event.issue.number }}
ISSUE_TITLE: ${{ github.event.issue.title }}
outputs:
module_name: ${{ steps.set-output.outputs.module_name }}
pypi_name: ${{ steps.set-output.outputs.pypi_name }}
result: ${{ steps.run.outputs.result }}
output: ${{ steps.run.outputs.output }}
type: ${{ steps.run.outputs.type }}
name: ${{ steps.run.outputs.name }}
steps:
- name: Parse issue body
id: parse
uses: zentered/issue-forms-body-parser@v1.5.1

- name: Get Inputs
id: set-output
env:
JSON_DATA: ${{ steps.parse.outputs.data }}
run: |
module_name=$(echo $JSON_DATA | jq -r '.["module_name"].text' )
echo "module_name=$module_name" >> $GITHUB_OUTPUT
pypi_name=$(echo $JSON_DATA | jq -r '.["pypi_name"].text' )
echo "pypi_name=$pypi_name" >> $GITHUB_OUTPUT
# 启动py脚本环境,并传入module_name,pypi_name,ISSUE_TITLE
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Python script
id: run
env:
TITLE: ${{ env.ISSUE_TITLE }}
PYPI_NAME: ${{ steps.set-output.outputs.pypi_name }}
run: |
python .github/actions_scripts/parse.py
validation-failed:
runs-on: ubuntu-latest
needs: parse-issue
if: needs.parse-issue.outputs.result == 'error'
steps:
- uses: actions/checkout@v3.3.0

- name: Render template
id: render
uses: chuhlomin/render-template@v1.6
with:
template: .github/workflows/templates/validation-failed.md
vars: |
validation_output: ${{ needs.parse-issue.outputs.output }}
- name: Add comment
uses: peter-evans/create-or-update-comment@v3.0.2
with:
issue-number: ${{ github.event.issue.number }}
body: ${{ steps.render.outputs.result }}

- name: Remove label validation/succeeded
if: contains(github.event.issue.labels.*.name, 'validation/succeeded')
uses: actions/github-script@v6
with:
script: |
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: ["validation/succeeded"]
})
- name: Add label validation/failed
if: contains(github.event.issue.labels.*.name, 'validation/failed') == false
uses: actions/github-script@v6
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ["validation/failed"]
})
- name: Mark job as failed
run: exit 1
15 changes: 15 additions & 0 deletions adapters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"adapters": [
{
"module_name": "adapter_module",
"pypi_name": "adapter_pypi",
"name": "适配器插件",
"description": "这是一个适配器插件。",
"author": "作者名",
"license": "Apache",
"homepage": "http://github.com/adapter",
"tags": ["adapter"],
"is_official": true
}
]
}
20 changes: 20 additions & 0 deletions bots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bots": [
{
"name": "机器人1",
"description": "这是一个机器人示例。",
"author": "作者名",
"license": "GPL",
"homepage": "http://github.com/bot1",
"tags": ["bot", "example"],
"is_official": true
},
{
"name": "机器人2",
"description": "这是另一个机器人示例。",
"author": "作者名",
"tags": ["bot", "test"],
"is_official": false
}
]
}
File renamed without changes.
Loading

0 comments on commit 6aa090a

Please sign in to comment.