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 4 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
61 changes: 61 additions & 0 deletions .github/workflows/ferretdb_go_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
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:
- main
- ferretdb
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
231 changes: 231 additions & 0 deletions ferretdb_packaging/defineversion/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// 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.")
}

version, err := controlVersion(*controlFileF)

debugEnv(action)

res, err := define(version, action.Getenv)
if err != nil {
action.Fatalf("%s", err)
}

setResults(action, res)
}

// controlDefaultVer matches the default_version field in control file,
// see pg_documentdb_core/documentdb_core.control.
var controlDefaultVer = regexp.MustCompile(`default_version\s*=\s*'([0-9]+\.[0-9]+-[0-9]+)'`)

// documentDBVer is the version syntax used by documentdb and tagging releases.
// For documentdb.control file, the version is in the format of `0.100-0`.
// For release tags, it has a leading `v` such as `v0.100-0`
// It accepts documentdb format version like `0.100-0` and SemVer format version like `0.100.0`.
// It may contain specific target such as `v0.100.0-ferretdb`,
// and may specify specific target SemVer such as `v0.100.0-ferretdb-2.0.1`.
//
//nolint:lll // for readibility
var documentDBVer = regexp.MustCompile(`^v?(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)[-\.](?P<patch>0|[1-9]\d*)-?(?P<target>[0-9a-zA-Z]+)?-?(?P<targetSemVer>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*))?$`)

// upstreamVer matches not allowed characters of upstream version,
// see https://www.debian.org/doc/debian-policy/ch-controlfields.html#version.
var upstreamVer = 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)
}
}

// controlVersion returns the default_version field from the control file,
// see pg_documentdb_core/documentdb_core.control.
func controlVersion(f string) (string, error) {
b, err := os.ReadFile(f)
if err != nil {
return "", err
}

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

version := string(match[1])

return version, nil
}

// define builds the upstream version number for debian package using
// the environment variables of GitHub Actions.
// If the release tag is set, it checks the tag matches the control version
// and returns an error on mismatch.
//
// The debian upstream version does not allow `-` character and replaced with `~`.
//
// See upstream version in https://www.debian.org/doc/debian-policy/ch-controlfields.html#version.
func define(controlDefaultVersion string, getenv githubactions.GetenvFunc) (string, error) {
version, err := parseVersion(controlDefaultVersion)
if err != nil {
return "", err
}

var upstreamVersion string

switch event := getenv("GITHUB_EVENT_NAME"); event {
case "pull_request", "pull_request_target":
branch := strings.ToLower(getenv("GITHUB_HEAD_REF"))
upstreamVersion = defineForPR(version, branch)

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

switch refType := strings.ToLower(getenv("GITHUB_REF_TYPE")); refType {
case "branch":
upstreamVersion, err = defineForBranch(version, refName)

case "tag":
upstreamVersion, err = defineForTag(version, 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 upstreamVersion == "" {
panic("both upstreamVersion and err are nil")
}

return upstreamVersion, nil
}

// defineForPR defines debian upstream version number for pull requests.
// It replaces special characters in branch name with character `~`.
func defineForPR(controlVersion, branch string) string {
// for branches like "dependabot/submodules/XXX"
parts := strings.Split(branch, "/")
branch = parts[len(parts)-1]
branch = upstreamVer.ReplaceAllString(branch, "~")

return fmt.Sprintf("%s~pr~%s", controlVersion, branch)
}

// defineForBranch defines debian upstream version number for branches.
func defineForBranch(version, branch string) (string, error) {
switch branch {
case "main", "ferretdb":
return fmt.Sprintf("%s~%s", version, branch), nil
default:
return "", fmt.Errorf("unhandled branch %q", branch)
}
}

// defineForTag defines debian upstream number for release builds.
// It returns an error if tag version does not match the control version.
func defineForTag(controlVersion string, tag string) (string, error) {
tagVersion, err := parseVersion(tag)
if err != nil {
return "", err
}

if !strings.HasPrefix(tagVersion, controlVersion) {
return "", fmt.Errorf("version in control file and release tag mismatch control:%s tag:%s", controlVersion, tagVersion)
}

return tagVersion, nil
}

// parseVersion parses the version string and returns valid debian upstream version.
//
// If version contain specific target such as `v0.100.0-ferretdb`,
// `0.100.0~ferretdb` is returned with `~` replacing not allowed `-`.
// If target contains specific version such as `v0.100.0-ferretdb-2.0.1`,
// it returns `0.100.0~ferretdb~2.0.1`.
func parseVersion(version string) (string, error) {
match := documentDBVer.FindStringSubmatch(version)
if match == nil || len(match) != documentDBVer.NumSubexp()+1 {
return "", fmt.Errorf("unexpected version syntax %q", version)
}

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

semVer := fmt.Sprintf("%s.%s.%s", major, minor, patch)

target := match[documentDBVer.SubexpIndex("target")]
if target == "" {
return semVer, nil
}

targetSemVer := match[documentDBVer.SubexpIndex("targetSemVer")]
if targetSemVer == "" {
return fmt.Sprintf("%s~%s", semVer, target), nil
}

return fmt.Sprintf("%s~%s~%s", semVer, target, targetSemVer), 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