From 3a13118bc8eb41167c6264d193a51a7e75ff511f Mon Sep 17 00:00:00 2001 From: windsonsea Date: Fri, 19 Jan 2024 16:00:51 +0800 Subject: [PATCH] add size label trigger --- .github/workflows/label_pr_size.yml | 27 +++++++++++++++++++ .github/workflows/size_label.py | 41 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 .github/workflows/label_pr_size.yml create mode 100644 .github/workflows/size_label.py diff --git a/.github/workflows/label_pr_size.yml b/.github/workflows/label_pr_size.yml new file mode 100644 index 0000000000..74a4547a10 --- /dev/null +++ b/.github/workflows/label_pr_size.yml @@ -0,0 +1,27 @@ +name: Label PR size + +on: + pull_request: + types: [opened, synchronize] + +jobs: + label_pr: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install Dependencies + run: pip install PyGithub + + - name: Run Script + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }} + run: python .github/workflows/size_label.py diff --git a/.github/workflows/size_label.py b/.github/workflows/size_label.py new file mode 100644 index 0000000000..e468c666be --- /dev/null +++ b/.github/workflows/size_label.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Jan 19 14:21:11 2024 +@author: Fan-Lin +""" + +import os +from github import Github + +# 获取环境变量 +TOKEN = os.environ['GITHUB_TOKEN'] +REPO_NAME = os.environ['GITHUB_REPOSITORY'] +PR_NUMBER = os.environ['PULL_REQUEST_NUMBER'] + +# 初始化Github对象 +g = Github(TOKEN) +repo = g.get_repo(REPO_NAME) +pr = repo.get_pull(int(PR_NUMBER)) + +# 获取PR的更改行数 +additions = pr.additions +deletions = pr.deletions +total_changes = additions + deletions + +# 根据更改行数设置标签 +label = '' +if total_changes <= 9: + label = 'size/XS' +elif 10 <= total_changes <= 29: + label = 'size/S' +elif 30 <= total_changes <= 99: + label = 'size/M' +elif 100 <= total_changes <= 499: + label = 'size/L' +elif 500 <= total_changes <= 999: + label = 'size/XL' +else: + label = 'size/XXL' + +# 设置标签 +pr.set_labels(label)