-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathutil.go
More file actions
78 lines (69 loc) · 2.03 KB
/
util.go
File metadata and controls
78 lines (69 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package tagpr
import (
"fmt"
"os"
"strings"
"github.com/google/go-github/v83/github"
)
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func (tp *tagpr) setOutput(name, value string) error {
fpath, ok := os.LookupEnv("GITHUB_OUTPUT")
if !ok {
return nil
}
f, err := os.OpenFile(fpath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
_, err = fmt.Fprintf(f, "%s=%s\n", name, value)
return err
}
func showGHError(err error, resp *github.Response) {
title := "failed to request GitHub API"
message := err.Error()
if resp != nil {
respInfo := []string{
fmt.Sprintf("status:%d", resp.StatusCode),
}
for name := range resp.Header {
n := strings.ToLower(name)
if strings.HasPrefix(n, "x-ratelimit") || n == "x-github-request-id" || n == "retry-after" {
respInfo = append(respInfo, fmt.Sprintf("%s:%s", n, resp.Header.Get(name)))
}
}
message += " " + strings.Join(respInfo, ", ")
}
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
fmt.Printf("::error title=%s::%s\n", title, message)
}
// normalizeTagPrefix ensures consistent prefix format (with trailing slash).
// Matches gitsemvers behavior: strings.TrimSuffix(prefix, "/") + "/"
func normalizeTagPrefix(prefix string) string {
if prefix == "" {
return ""
}
return strings.TrimSuffix(prefix, "/") + "/"
}
// fullTag returns the tag with prefix (e.g., "tools/v1.2.3").
func fullTag(prefix, tag string) string {
if prefix == "" {
return tag
}
return prefix + tag
}
// branchSafePrefix converts tag prefix to branch-safe format.
// Replaces slashes with hyphens and ensures trailing hyphen.
// e.g., "tools/" -> "tools-", "backend/api/" -> "backend-api-"
func branchSafePrefix(normalizedPrefix string) string {
if normalizedPrefix == "" {
return ""
}
// Remove trailing slash and replace remaining slashes with hyphens
s := strings.TrimSuffix(normalizedPrefix, "/")
s = strings.ReplaceAll(s, "/", "-")
return s + "-"
}