Code Issues Pull Requests Actions Packages Projects Releases 15 Wiki Activity Security Code Quality
3 Commits
4 changed files with 175 additions and 0 deletions

No files matched your search

File renamed without changes.
@@ -0,0 +1,61 @@
# resolve-branch-for-tag
Tìm nhánh chứa commit của tag đang checkout. Output `name` là tên nhánh đó.
Tag không thuộc về một nhánh. Action nhìn các ref trên remote, ưu tiên nhánh có tip đúng commit của tag. Không có nhánh nào trùng tip thì lấy nhánh chứa commit đó. Trong danh sách ứng viên, tên trong `prefer` được chọn trước. Không tìm thấy nhánh nào thì dùng `fallback`.
## Usage
Checkout full history trước. Clone nông làm `--contains` thiếu commit.
```yaml
- uses: actions/checkout@v7
with:
fetch-depth: 0
- id: branch
uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/resolve-branch-for-tag@main
- run: echo "branch is ${{ steps.branch.outputs.name }}"
```
Cùng repo, sau khi checkout:
```yaml
- id: branch
uses: ./.gitea/actions/resolve-branch-for-tag
```
Scoped workflow trên repo khác dùng URL đầy đủ. `uses: ./` chỉ thấy checkout của repo đang chạy.
Mặc định ưu tiên `main`, rồi `develop`. Đổi thứ tự hoặc nhánh dự phòng:
```yaml
- id: branch
uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/resolve-branch-for-tag@main
with:
prefer: develop main
fallback: develop
```
## Inputs
| Input | Bắt buộc | Mặc định | Ý nghĩa |
|---|---|---|---|
| `prefer` | không | `main develop` | Tên nhánh, cách nhau bởi khoảng trắng. Tên nào có trong danh sách ứng viên thì được chọn trước. |
| `fallback` | không | `develop` | Dùng khi không có nhánh remote nào chứa commit. |
| `remote` | không | `origin` | Remote giữ ref của các nhánh. `actions/checkout` tạo `origin`. |
## Output
| Output | Ý nghĩa |
|---|---|
| `name` | Tên nhánh đã chọn. Đọc bằng `${{ steps.<id>.outputs.name }}`. |
## Cách chọn
1. Nhánh có tip đúng commit của tag (`--points-at`).
2. Nếu không có, nhánh chứa commit đó (`--contains`).
3. Trong các ứng viên, lấy tên đầu tiên của `prefer` khớp chính xác.
4. Không khớp tên nào thì lấy ứng viên đầu tiên.
5. Không có ứng viên thì lấy `fallback` và in `No branch contains <sha>`.
@@ -0,0 +1,62 @@
name: Resolve branch for tag
description: Find the branch that contains the checked-out tag commit.
inputs:
prefer:
description: Space-separated branch names. The first one present in the candidate list wins.
required: false
default: main develop
fallback:
description: Branch used when no remote branch contains the commit.
required: false
default: develop
remote:
description: Git remote that holds the branch refs.
required: false
default: origin
outputs:
name:
description: Resolved branch name.
value: ${{ steps.tag-branch.outputs.name }}
runs:
using: composite
steps:
- name: Resolve branch for tag
id: tag-branch
shell: bash
env:
PREFER: ${{ inputs.prefer }}
FALLBACK: ${{ inputs.fallback }}
REMOTE: ${{ inputs.remote }}
run: |
set -euo pipefail
git fetch "$REMOTE" "+refs/heads/*:refs/remotes/${REMOTE}/*"
strip="s|^${REMOTE}/||"
exact=$(git for-each-ref --points-at HEAD --format='%(refname:short)' "refs/remotes/${REMOTE}" \
| sed "$strip" \
| grep -v '^HEAD$' || true)
if [[ -n "$exact" ]]; then
candidates="$exact"
else
candidates=$(git for-each-ref --contains HEAD --format='%(refname:short)' "refs/remotes/${REMOTE}" \
| sed "$strip" \
| grep -v '^HEAD$' || true)
fi
branch=""
for preferred in $PREFER; do
branch=$(printf '%s\n' "$candidates" | grep -x "$preferred" || true)
if [[ -n "$branch" ]]; then
break
fi
done
if [[ -z "$branch" ]]; then
branch=$(printf '%s\n' "$candidates" | head -n 1)
fi
if [[ -z "$branch" ]]; then
branch="$FALLBACK"
echo "No branch contains ${GITHUB_SHA}; using ${branch}"
fi
echo "Ref ${GITHUB_REF_NAME} is on branch ${branch}"
echo "name=${branch}" >> "$GITHUB_OUTPUT"
@@ -0,0 +1,52 @@
name: Check PR & Commit Length
on:
pull_request:
types: [opened, edited, synchronize, reopened]
jobs:
validate-length:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check PR Title, Description & Commits
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
MAX_TITLE: 72
MAX_DESC: 500
run: |
# 1. Check PR Title & Description (important when using Squash Merge)
if [ ${#PR_TITLE} -gt $MAX_TITLE ]; then
echo "❌ PR Title too long (${#PR_TITLE}/$MAX_TITLE characters)."
exit 1
fi
if [ ${#PR_BODY} -gt $MAX_DESC ]; then
echo "❌ PR Description too long (${#PR_BODY}/$MAX_DESC characters)."
exit 1
fi
# 2. Check each commit inside PR (important when using Merge Commit / Rebase)
for commit in $(git rev-list "$BASE_SHA..$HEAD_SHA"); do
c_title=$(git log -n 1 --format=%s "$commit")
c_body=$(git log -n 1 --format=%b "$commit")
if [ ${#c_title} -gt $MAX_TITLE ]; then
echo "❌ Commit $commit has Title too long ${#c_title}/$MAX_TITLE characters."
exit 1
fi
if [ ${#c_body} -gt $MAX_DESC ]; then
echo "❌ Commit $commit has Description too long ${#c_body}/$MAX_DESC characters."
exit 1
fi
done
echo "✅ Passed the length requirements!"