Skip to content

Commit

Permalink
undo version check
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow committed Feb 18, 2025
1 parent 55f6221 commit 9d51b2d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 93 deletions.
59 changes: 8 additions & 51 deletions ferretdb_packaging/defineversion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"
Expand All @@ -30,9 +29,6 @@ import (
func main() {
controlFileF := flag.String("control-file", "../pg_documentdb/documentdb.control", "pg_documentdb/documentdb.control file path")

// https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#list-matching-references
tagsF := flag.String("tags", "", "release tags of microsoft/documentdb in JSON format")

flag.Parse()

action := githubactions.New()
Expand All @@ -41,23 +37,14 @@ func main() {
action.Fatalf("%s", "-control-file flag is empty.")
}

if *tagsF == "" {
action.Fatalf("%s", "tags is empty.")
}

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

tags, err := releaseTags(*tagsF)
if err != nil {
action.Fatalf("%s", err)
}

debugEnv(action)

res, err := define(version, tags, action.Getenv)
res, err := define(version, action.Getenv)
if err != nil {
action.Fatalf("%s", err)
}
Expand All @@ -83,12 +70,6 @@ var documentDBVer = regexp.MustCompile(`^v?(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1
// see https://www.debian.org/doc/debian-policy/ch-controlfields.html#version.
var disallowedVer = regexp.MustCompile(`[^A-Za-z0-9~.+]`)

// tag represents git tags references in JSON format specified in
// https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#list-matching-references.
type tag struct {
Ref string `json:"ref"`
}

// debugEnv logs all environment variables that start with `GITHUB_` or `INPUT_`
// in debug level.
func debugEnv(action *githubactions.Action) {
Expand Down Expand Up @@ -127,23 +108,6 @@ func controlVersion(f string) (string, error) {
return version, nil
}

// releaseTags returns tags released by microsoft/documentdb.
func releaseTags(tagsStr string) ([]string, error) {
tags := []tag{}

err := json.Unmarshal([]byte(tagsStr), &tags)
if err != nil {
return nil, err
}

res := make([]string, len(tags))
for i, t := range tags {
res[i] = strings.TrimLeft(t.Ref, "refs/tags/")
}

return res, nil
}

// define returns the upstream version for debian package using
// the environment variables of GitHub Actions.
// If the release tag is set, it checks the tag matches the control version
Expand All @@ -152,7 +116,7 @@ func releaseTags(tagsStr string) ([]string, error) {
// The 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, documentDBTags []string, getenv githubactions.GetenvFunc) (string, error) {
func define(controlDefaultVersion string, getenv githubactions.GetenvFunc) (string, error) {
version, err := parseVersion(controlDefaultVersion)
if err != nil {
return "", err
Expand All @@ -173,7 +137,7 @@ func define(controlDefaultVersion string, documentDBTags []string, getenv github
upstreamVersion, err = defineForBranch(version, refName)

case "tag":
upstreamVersion, err = defineForTag(documentDBTags, refName)
upstreamVersion, err = defineForTag(version, refName)

default:
err = fmt.Errorf("unhandled ref type %q for event %q", refType, event)
Expand Down Expand Up @@ -216,25 +180,18 @@ func defineForBranch(version, branch string) (string, error) {
}

// defineForTag defines debian upstream version for release builds.
// It returns an error if tag version for documentdb is not already released by microsoft/documentdb.
func defineForTag(documentDBTags []string, tag string) (string, error) {
// It returns an error if tag does not contain default version.
func defineForTag(defaultVersion string, tag string) (string, error) {
tagVersion, err := parseVersion(tag)
if err != nil {
return "", err
}

for _, t := range documentDBTags {
documentDBTag, err := parseVersion(t)
if err != nil {
return "", err
}

if strings.HasPrefix(tagVersion, documentDBTag) {
return tagVersion, nil
}
if !strings.HasPrefix(tagVersion, defaultVersion) {
return "", fmt.Errorf("release tag %s does not match control file default version %s", tagVersion, defaultVersion)
}

return "", fmt.Errorf("release tag %s does not match existing DocumentDB tags [%s]", tagVersion, strings.Join(documentDBTags, ","))
return tagVersion, nil
}

// parseVersion parses the version string and returns valid debian upstream version.
Expand Down
43 changes: 1 addition & 42 deletions ferretdb_packaging/defineversion/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestDefine(t *testing.T) {
for name, tc := range map[string]struct {
env map[string]string
defaultVersion string // pg_documentdb/documentdb.control file's default_version field
tags []string
expected string
}{
"pull_request": {
Expand All @@ -52,7 +51,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "branch",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~pr~define~docker~tag",
},

Expand All @@ -64,7 +62,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "branch",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~pr~mongo~go~driver~29d768e",
},

Expand All @@ -76,7 +73,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "branch",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~pr~define~docker~tag",
},

Expand All @@ -88,7 +84,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "branch",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~main",
},
"push/ferretdb": {
Expand All @@ -99,7 +94,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "branch",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~ferretdb",
},
"push/other": {
Expand All @@ -119,7 +113,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "tag",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0",
},
"push/tag/ferretdb": {
Expand All @@ -130,7 +123,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "tag",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~ferretdb",
},
"push/tag/ferretdb-specific-version": {
Expand All @@ -141,7 +133,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_TYPE": "tag",
},
defaultVersion: "0.100-0",
tags: []string{"v0.100-0", "v0.101-0"},
expected: "0.100.0~ferretdb~2.0.1",
},

Expand All @@ -152,7 +143,6 @@ func TestDefine(t *testing.T) {
"GITHUB_REF_NAME": "v0.100-1", // default version and tag mismatch
"GITHUB_REF_TYPE": "tag",
},
tags: []string{"v0.100-0", "v0.101-0"},
defaultVersion: "0.100-0",
},

Expand All @@ -174,7 +164,6 @@ func TestDefine(t *testing.T) {
},
defaultVersion: "0.100-0",
expected: "0.100.0~main",
tags: []string{"v0.100-0", "v0.101-0"},
},

"workflow_run": {
Expand All @@ -186,11 +175,10 @@ func TestDefine(t *testing.T) {
},
defaultVersion: "0.100-0",
expected: "0.100.0~main",
tags: []string{"v0.100-0", "v0.101-0"},
},
} {
t.Run(name, func(t *testing.T) {
actual, err := define(tc.defaultVersion, tc.tags, getEnvFunc(t, tc.env))
actual, err := define(tc.defaultVersion, getEnvFunc(t, tc.env))
if tc.expected == "" {
require.Error(t, err)
return
Expand All @@ -202,35 +190,6 @@ func TestDefine(t *testing.T) {
}
}

func TestReleaseTags(t *testing.T) {
tagStr := `[
{
"ref": "refs/tags/v0.100-0",
"node_id": "REF_kwDONuj1jLJyZWZzL3RhZ3MvdjAuMTAwLTA",
"url": "https://api.github.com/repos/FerretDB/documentdb/git/refs/tags/v0.100-0",
"object": {
"sha": "ae4b8438ab8f9f29d158c6fcd3c25d4f3d772fb0",
"type": "commit",
"url": "https://api.github.com/repos/FerretDB/documentdb/git/commits/ae4b8438ab8f9f29d158c6fcd3c25d4f3d772fb0"
}
},
{
"ref": "refs/tags/v0.101-0",
"node_id": "REF_kwDONuj1jLJyZWZzL3RhZ3MvdjAuMTAxLTA",
"url": "https://api.github.com/repos/FerretDB/documentdb/git/refs/tags/v0.101-0",
"object": {
"sha": "013056de0549cbe673879da0967c490a5184be0d",
"type": "tag",
"url": "https://api.github.com/repos/FerretDB/documentdb/git/tags/013056de0549cbe673879da0967c490a5184be0d"
}
}
]`

tags, err := releaseTags(tagStr)
require.NoError(t, err)
require.Equal(t, []string{"v0.100-0", "v0.101-0"}, tags)
}

func TestResults(t *testing.T) {
dir := t.TempDir()

Expand Down

0 comments on commit 9d51b2d

Please sign in to comment.