Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add versioning tool #3

Merged
merged 23 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/ferretdb_go_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: Go
on:
pull_request:
types:
- unlabeled # if GitHub Actions stuck, add and remove "not ready" label to force rebuild
- opened
- reopened
- synchronize
push:
branches:
- ferretdb
tags:
- "*"
schedule:
- cron: "12 0 * * *"

env:
GOPATH: /home/runner/go
GOCACHE: /home/runner/go/cache
GOLANGCI_LINT_CACHE: /home/runner/go/cache/lint
GOMODCACHE: /home/runner/go/mod
GOPROXY: https://proxy.golang.org
GOTOOLCHAIN: local

jobs:
test:
name: Test
# https://www.ubicloud.com/docs/about/pricing#github-actions
# https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#per-minute-rates-for-larger-runners
runs-on: ubicloud-standard-4

timeout-minutes: 15

# Do not run this job in parallel for any PR change or branch push.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}
cancel-in-progress: true

if: github.event_name != 'pull_request' || !contains(github.event.pull_request.labels.*.name, 'not ready')

steps:
# TODO https://github.com/FerretDB/github-actions/issues/211
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: FerretDB/github-actions/setup-go@main

- name: Run tests
run: |
cd ferretdb_packaging
go mod tidy
go mod verify
go test ./...

- name: Check dirty
if: always()
run: |
git status --untracked-files --ignored
git status
git diff --exit-code
2 changes: 2 additions & 0 deletions ferretdb_packaging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# FerretDB packaging
!*go.mod
210 changes: 210 additions & 0 deletions ferretdb_packaging/defineversion/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main defines debian version number of DocumentDB for CI builds.
package main

import (
"flag"
"fmt"
"os"
"regexp"
"slices"
"strings"

"github.com/sethvargo/go-githubactions"
)

func main() {
controlFileF := flag.String("control-file", "../pg_documentdb/documentdb.control", "pg_documentdb/documentdb.control file path")

flag.Parse()

action := githubactions.New()

if *controlFileF == "" {
action.Fatalf("%s", "-control-file flag is empty.")
}

controlDefaultVersion, err := getControlDefaultVersion(*controlFileF)
if err != nil {
action.Fatalf("%s", err)
}

debugEnv(action)

packageVersion, err := definePackageVersion(controlDefaultVersion, action.Getenv)
if err != nil {
action.Fatalf("%s", err)
}

setResults(action, packageVersion)
}

// controlDefaultVer matches major, minor and "patch" from default_version field in control file,
// see pg_documentdb_core/documentdb_core.control.
var controlDefaultVer = regexp.MustCompile(`(?m)^default_version = '(?P<major>[0-9]+)\.(?P<minor>[0-9]+)-(?P<patch>[0-9]+)'$`)

// semVerTag is a https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string,
// but with a leading `v`.
var semVerTag = regexp.MustCompile(`^v(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)

// disallowedVer matches disallowed characters of Debian `upstream_version` when used without `debian_revision`.
// See https://www.debian.org/doc/debian-policy/ch-controlfields.html#version.
var disallowedVer = regexp.MustCompile(`[^A-Za-z0-9~.+]`)

// debugEnv logs all environment variables that start with `GITHUB_` or `INPUT_`
// in debug level.
func debugEnv(action *githubactions.Action) {
res := make([]string, 0, 30)

for _, l := range os.Environ() {
if strings.HasPrefix(l, "GITHUB_") || strings.HasPrefix(l, "INPUT_") {
res = append(res, l)
}
}

slices.Sort(res)

action.Debugf("Dumping environment variables:")

for _, l := range res {
action.Debugf("\t%s", l)
}
}

// getControlDefaultVersion returns the default_version field from the control file
// in SemVer format (0.100-0 -> 0.100.0).
func getControlDefaultVersion(f string) (string, error) {
b, err := os.ReadFile(f)
if err != nil {
return "", err
}

match := controlDefaultVer.FindSubmatch(b)
if match == nil || len(match) != controlDefaultVer.NumSubexp()+1 {
return "", fmt.Errorf("control file did not find default_version: %s", f)
}

major := match[controlDefaultVer.SubexpIndex("major")]
minor := match[controlDefaultVer.SubexpIndex("minor")]
patch := match[controlDefaultVer.SubexpIndex("patch")]

return fmt.Sprintf("%s.%s.%s", major, minor, patch), nil
}

// definePackageVersion returns valid Debian package version,
// based on `default_version` in the control file and environment variables set by GitHub Actions.
//
// See https://www.debian.org/doc/debian-policy/ch-controlfields.html#version.
// We use `upstream_version` only.
// For that reason, we can't use `-`, so we replace it with `~`.
func definePackageVersion(controlDefaultVersion string, getenv githubactions.GetenvFunc) (string, error) {
var packageVersion string
var err error

switch event := getenv("GITHUB_EVENT_NAME"); event {
case "pull_request", "pull_request_target":
branch := strings.ToLower(getenv("GITHUB_HEAD_REF"))
packageVersion = definePackageVersionForPR(controlDefaultVersion, branch)

case "push", "schedule", "workflow_run":
refName := strings.ToLower(getenv("GITHUB_REF_NAME"))

switch refType := strings.ToLower(getenv("GITHUB_REF_TYPE")); refType {
case "branch":
packageVersion, err = definePackageVersionForBranch(controlDefaultVersion, refName)

case "tag":
packageVersion, err = definePackagerVersionForTag(refName)

default:
err = fmt.Errorf("unhandled ref type %q for event %q", refType, event)
}

default:
err = fmt.Errorf("unhandled event type %q", event)
}

if err != nil {
return "", err
}

if packageVersion == "" {
return "", fmt.Errorf("both packageVersion and err are nil")
}

return packageVersion, nil
}

// definePackageVersionForPR returns valid Debian package version for PR.
// See [definePackageVersion].
func definePackageVersionForPR(controlDefaultVersion, branch string) string {
// for branches like "dependabot/submodules/XXX"
parts := strings.Split(branch, "/")
branch = parts[len(parts)-1]
res := fmt.Sprintf("%s-pr-%s", controlDefaultVersion, branch)

return disallowedVer.ReplaceAllString(res, "~")
}

// definePackageVersionForBranch returns valid Debian package version for branch.
// See [definePackageVersion].
func definePackageVersionForBranch(controlDefaultVersion, branch string) (string, error) {
switch branch {
case "ferretdb":
return fmt.Sprintf("%s~branch~%s", controlDefaultVersion, branch), nil
default:
return "", fmt.Errorf("unhandled branch %q", branch)
}
}

// definePackagerVersionForTag returns valid Debian package version for tag.
// See [definePackageVersion].
func definePackagerVersionForTag(tag string) (string, error) {
match := semVerTag.FindStringSubmatch(tag)
if match == nil || len(match) != semVerTag.NumSubexp()+1 {
return "", fmt.Errorf("unexpected tag syntax %q", tag)
}

major := match[semVerTag.SubexpIndex("major")]
minor := match[semVerTag.SubexpIndex("minor")]
patch := match[semVerTag.SubexpIndex("patch")]
prerelease := match[semVerTag.SubexpIndex("prerelease")]
buildmetadata := match[semVerTag.SubexpIndex("buildmetadata")]

if prerelease == "" {
return "", fmt.Errorf("prerelease is empty")
}

if !strings.Contains(prerelease, "ferretdb") {
return "", fmt.Errorf("prerelease %q should include `ferretdb`", prerelease)
}

if buildmetadata != "" {
return "", fmt.Errorf("buildmetadata %q is present", buildmetadata)
}

res := fmt.Sprintf("%s.%s.%s-%s", major, minor, patch, prerelease)
return disallowedVer.ReplaceAllString(res, "~"), nil
}

// setResults sets action output parameters, summary, etc.
func setResults(action *githubactions.Action, res string) {
output := fmt.Sprintf("version: %s", res)

action.AddStepSummary(output)
action.Infof("%s", output)
action.SetOutput("version", res)
}
Loading
Loading