▍ humdrum codex / custard v0.3.0
license AGPL-3.0
1.5 KB raw
 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
package server

import (
	"strings"

	"custard/internal/release"
)

// couldBeRelease is a cheap, prefix-agnostic prefilter for the webhook: it
// accepts a semver tag or any branch whose final path segment is a release
// semver, so ordinary main/feature pushes never spawn a publish goroutine.
// The exact per-repo mode + prefix are enforced later by releasePlan.
func couldBeRelease(ref string) bool {
	if tag, ok := strings.CutPrefix(ref, "refs/tags/"); ok {
		return release.IsReleaseTag(tag)
	}
	if br, ok := strings.CutPrefix(ref, "refs/heads/"); ok {
		seg := br
		if i := strings.LastIndexByte(br, '/'); i >= 0 {
			seg = br[i+1:]
		}
		return release.IsReleaseTag(seg)
	}
	return false
}

// releasePlan decides, for a repo's config and a pushed ref, what to archive and
// publish. A non-empty skip means "ignore this push" with a logged reason.
func releasePlan(cfg release.RepoConfig, ref string) (archiveRef, version, skip string) {
	switch cfg.ReleaseSource() {
	case release.SourceBookmark:
		br, ok := strings.CutPrefix(ref, "refs/heads/")
		if !ok {
			return "", "", "bookmark mode: not a branch ref"
		}
		ver, ok := release.BookmarkVersion(br, cfg.Prefix())
		if !ok {
			return "", "", "bookmark mode: not a " + cfg.Prefix() + "vX.Y.Z bookmark"
		}
		return br, ver, ""
	default: // SourceTag
		tag, ok := strings.CutPrefix(ref, "refs/tags/")
		if !ok {
			return "", "", "tag mode: not a tag ref"
		}
		if !release.IsReleaseTag(tag) {
			return "", "", "tag mode: not a semver release tag"
		}
		return tag, tag, ""
	}
}