From 97677a524adad8dd72df33e43a633a37bf3f6538 Mon Sep 17 00:00:00 2001 From: levivannoort Date: Mon, 7 Apr 2025 21:38:25 +0200 Subject: [PATCH 1/3] chore: prevent error on parantheses after colon --- main.go | 57 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 4d5474d..31e5685 100644 --- a/main.go +++ b/main.go @@ -88,7 +88,7 @@ func fetchTitle(logger *slog.Logger, githubEventPath string) string { if eventData, err = os.ReadFile(githubEventPath); err != nil { logger.Error("Problem reading the event JSON file", slog.String("path", githubEventPath), slog.Any("error", err)) - os.Exit(1) // You might want to return an empty string or handle this error upstream instead. + os.Exit(1) } if err = json.Unmarshal(eventData, &event); err != nil { @@ -100,30 +100,51 @@ func fetchTitle(logger *slog.Logger, githubEventPath string) string { } func splitTitle(logger *slog.Logger, title string) (titleType string, titleScope string, titleMessage string) { - if index := strings.Index(title, "("); strings.Contains(title, "(") { - titleType = title[:index] - } else if index := strings.Index(title, ":"); strings.Contains(title, ":") { - titleType = title[:index] - } else { - logger.Error("No type was included in the pull request title.", slog.String("desired format", desiredFormat)) + // Split title into prefix (type/scope) and message parts using colon as separator + prefix, message, found := strings.Cut(title, ":") + if !found { + logger.Error("Title must include a message after the colon", + slog.String("desired format", desiredFormat), + slog.String("title", title)) os.Exit(1) } - if strings.Contains(title, "(") && strings.Contains(title, ")") { - scope := regexp.MustCompile(`\(([^)]+)\)`) - if matches := scope.FindStringSubmatch(title); len(matches) > 1 { - titleScope = matches[1] - } - } + // Clean up the message part + titleMessage = strings.TrimSpace(message) - if strings.Contains(title, ":") { - titleMessage = strings.SplitAfter(title, ":")[1] - titleMessage = strings.TrimSpace(titleMessage) - } else { - logger.Error("No message was included in the pull request title.", slog.String("desired format", desiredFormat)) + // Extract type and scope from the prefix + titleType, titleScope = extractTypeAndScope(prefix) + + // Validate that we found a type + if titleType == "" { + logger.Error("Title must include a type", + slog.String("desired format", desiredFormat), + slog.String("title", title)) os.Exit(1) } + return titleType, titleScope, titleMessage +} + +func extractTypeAndScope(prefix string) (titleType string, titleScope string) { + prefix = strings.TrimSpace(prefix) + + // Check if prefix contains a scope in parentheses + if strings.Contains(prefix, "(") && strings.Contains(prefix, ")") { + + // Extract scope using regex + scopeRegex := regexp.MustCompile(`\(([^)]+)\)`) + + // + if matches := scopeRegex.FindStringSubmatch(prefix); len(matches) > 1 { + titleScope = matches[1] + titleType = strings.TrimSpace(strings.Split(prefix, "(")[0]) + return + } + } + + // If no scope found or invalid format, use entire prefix as type + titleType = prefix return } From b645602f8411a25ab36065e20e56fbae0ec86de6 Mon Sep 17 00:00:00 2001 From: levivannoort Date: Mon, 7 Apr 2025 21:42:58 +0200 Subject: [PATCH 2/3] chore(ci): silent fail on no contributer changes --- .github/workflows/update-contributors.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/update-contributors.yaml b/.github/workflows/update-contributors.yaml index 1847009..2cce89f 100644 --- a/.github/workflows/update-contributors.yaml +++ b/.github/workflows/update-contributors.yaml @@ -19,7 +19,18 @@ jobs: owner: kontrolplane repository: pull-request-title-validator + - name: check-for-changes + id: check + run: | + if git diff --quiet README.md; then + echo "No changes to commit" + echo "changes=false" >> $GITHUB_OUTPUT + exit 0 + fi + echo "changes=true" >> $GITHUB_OUTPUT + - name: open-pull-request + if: steps.check.outputs.changes == 'true' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | From c96365235b75c058fb5870a047f4100662f1b9bb Mon Sep 17 00:00:00 2001 From: levivannoort Date: Mon, 7 Apr 2025 22:06:52 +0200 Subject: [PATCH 3/3] chore: bump version in examples --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dbf4473..40bcb00 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ jobs: runs-on: ubuntu-latest steps: - name: validate pull request title - uses: kontrolplane/pull-request-title-validator@v1.3.2 + uses: kontrolplane/pull-request-title-validator@v1.4.1 ``` ### Custom types @@ -62,7 +62,7 @@ jobs: runs-on: ubuntu-latest steps: - name: validate pull request title - uses: kontrolplane/pull-request-title-validator@v1.3.2 + uses: kontrolplane/pull-request-title-validator@v1.4.1 with: types: "fix,feat,chore" ``` @@ -90,7 +90,7 @@ jobs: runs-on: ubuntu-latest steps: - name: validate pull request title - uses: kontrolplane/pull-request-title-validator@v1.3.2 + uses: kontrolplane/pull-request-title-validator@v1.4.1 with: scopes: "api,lang,parser,package/.+" ```