Merge pull request 'feat: initial implementation' (#5) from main into release
All checks were successful
Release / Release (push) Successful in 56s

Reviewed-on: #5
This commit is contained in:
Jan K9f 2025-04-09 16:25:18 +00:00
commit e26143661c
Signed by:
GPG key ID: 944223E4D46B7412
6 changed files with 158 additions and 0 deletions

21
.gitea/workflows/pr.yml Normal file
View file

@ -0,0 +1,21 @@
name: "Lint Pull Request"
on:
pull_request:
types: [opened, edited, reopened, synchronize]
jobs:
lint:
name: Lint Pr Title
runs-on: ubuntu-latest
if: github.base_ref != 'release'
steps:
- name: Install go
uses: actions/setup-go@v5
with:
go-version: 1.24.2
- name: Run Pull Request Lint Action
uses: https://git.kjan.de/actions/pull-request-lint@main

5
action.yml Normal file
View file

@ -0,0 +1,5 @@
name: 'Pull Request Lint'
description: 'A simple Gitea action written in go'
runs:
using: 'go'
main: 'main.go'

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.kjan.de/actions/pull-request-lint
go 1.24.2

0
go.sum Normal file
View file

View file

@ -0,0 +1,83 @@
package validation
import (
"fmt"
"regexp"
"strings"
)
func ValidateConventionalCommit(commit string) error {
// Regex to match the commit format
// type(scope)!: description
// or
// type!: description
// or
// type: description
re := regexp.MustCompile(`^(?P<type>[a-z]+)(?P<scope>\([a-z]+\))?(?P<breaking>!)?: (?P<description>[a-z].+)$`)
match := re.FindStringSubmatch(commit)
if len(match) == 0 {
return fmt.Errorf("Invalid PR title")
}
typeIndex := re.SubexpIndex("type")
scopeIndex := re.SubexpIndex("scope")
breakingIndex := re.SubexpIndex("breaking")
descriptionIndex := re.SubexpIndex("description")
commitType := match[typeIndex]
scope := match[scopeIndex]
breaking := match[breakingIndex]
description := match[descriptionIndex]
// Type MUST be lowercase
if commitType != strings.ToLower(commitType) {
return fmt.Errorf("type must be lowercase")
}
// Description MUST start with lowercase
if description != strings.ToLower(description) {
return fmt.Errorf("description must start with lowercase")
}
// Scope MUST be lowercase
if scope != "" && scope != strings.ToLower(scope) {
return fmt.Errorf("scope must be lowercase")
}
// Check for breaking change indicator
hasBreakingChangeFooter := strings.Contains(commit, "BREAKING CHANGE:")
if breaking == "!" && hasBreakingChangeFooter {
return fmt.Errorf("breaking change indicator and footer are mutually exclusive")
}
lines := strings.Split(commit, "\n")
if len(lines) > 1 {
body := strings.Join(lines[1:], "\n")
// Check if body is separated from description by a blank line
if !strings.HasPrefix(body, "\n") {
return fmt.Errorf("body must be separated from description by a blank line")
}
}
// Check for footers
footerRegex := regexp.MustCompile(`(?m)^(?P<token>[A-Za-z-]+|BREAKING CHANGE): (?P<value>.*)$`)
footerMatches := footerRegex.FindAllStringSubmatch(commit, -1)
for _, footerMatch := range footerMatches {
tokenIndex := footerRegex.SubexpIndex("token")
token := footerMatch[tokenIndex]
// BREAKING-CHANGE MUST be synonymous with BREAKING CHANGE
if token == "BREAKING-CHANGE" {
continue
}
// Token MUST use - in place of whitespace characters, except for BREAKING CHANGE
if token != "BREAKING CHANGE" && strings.Contains(token, " ") {
return fmt.Errorf("footer token must use - in place of whitespace characters")
}
}
return nil
}

46
main.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"encoding/json"
"fmt"
"os"
"git.kjan.de/actions/pull-request-lint/internal/validation"
)
type GithubEvent struct {
PullRequest struct {
Title string `json:"title"`
} `json:"pull_request"`
}
func main() {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
if eventPath == "" {
fmt.Println("GITHUB_EVENT_PATH not set")
os.Exit(1)
}
eventFile, err := os.Open(eventPath)
if err != nil {
fmt.Printf("Error opening %s: %v\n", eventPath, err)
os.Exit(1)
}
defer eventFile.Close()
var event GithubEvent
decoder := json.NewDecoder(eventFile)
err = decoder.Decode(&event)
if err != nil {
fmt.Printf("Error decoding event.json: %v\n", err)
os.Exit(1)
}
prTitle := event.PullRequest.Title
semanticValidationErr := validation.ValidateConventionalCommit(prTitle)
if semanticValidationErr != nil {
fmt.Println(semanticValidationErr)
os.Exit(1)
}
os.Exit(0)
}