Skip to content

Commit

Permalink
feat (worker): gitTag, new parameters (#2656)
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <yvonnick.esnault@corp.ovh.com>
  • Loading branch information
yesnault authored and sguiheux committed May 11, 2018
1 parent 384816f commit fee8d8b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 33 deletions.
20 changes: 17 additions & 3 deletions engine/api/action/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,25 @@ Checkout a repository into a new directory.`
gittag := sdk.NewAction(sdk.GitTagAction)
gittag.Type = sdk.BuiltinAction
gittag.Description = `CDS Builtin Action.
Tag the current branch and push it.`
Tag the current branch and push it.
Semver used if fully compatible with https://semver.org/
`

gittag.Parameter(sdk.Parameter{
Name: "tagName",
Description: "Set the name of the tag. Must match semver. If empty CDS will make a patch version",
Name: "tagPrerelease",
Description: "Prerelase version of the tag. Example: alpha on a tag 1.0.0 will return 1.0.0-apha",
Value: "",
Type: sdk.StringParameter,
})
gittag.Parameter(sdk.Parameter{
Name: "tagLevel",
Description: "Set the level of the tag. Must be 'major' or 'minor' or 'patch'",
Value: "",
Type: sdk.StringParameter,
})
gittag.Parameter(sdk.Parameter{
Name: "tagMetadata",
Description: "Metadata of the tag. Example: cds.42 on a tag 1.0.0 will return 1.0.0+cds.42",
Value: "",
Type: sdk.StringParameter,
})
Expand Down
11 changes: 11 additions & 0 deletions engine/sql/092_gittag.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +migrate Up
UPDATE action set description = 'CDS Builtin Action. Tag the current branch and push it. Semver used if fully compatible with https://semver.org/' where name = 'GitTag';
UPDATE action_parameter SET name = 'tagMetadata', description = 'Metadata of the tag. Example: cds.42 on a tag 1.0.0 will return 1.0.0+cds.42' where name = 'tagName' and action_id = (select id from action where name = 'GitTag');
INSERT into action_parameter (action_id, name, description, type, value, worker_model_name) values((select id from action where name = 'GitTag'), 'tagPrerelease', 'Prerelase version of the tag. Example: alpha on a tag 1.0.0 will return 1.0.0-apha', 'string', '', '');
INSERT into action_parameter (action_id, name, description, type, value, worker_model_name) values((select id from action where name = 'GitTag'), 'tagLevel', 'Set the level of the tag. Must be ''major'' or ''minor'' or ''patch''', 'string', '', '');


-- +migrate Down
UPDATE action_parameter set name = 'tagName', description = 'Set the name of the tag. Must match semver. If empty CDS will make a patch version' where name = 'tagMetadata' and action_id = (select id from action where name = 'GitTag');
DELETE from action_parameter where name = 'tagPrerelease' and action_id = (select id from action where name = 'GitTag');
DELETE from action_parameter where name = 'tagLevel' and action_id = (select id from action where name = 'GitTag');
40 changes: 18 additions & 22 deletions engine/worker/builtin_gitclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,23 @@ func extractInfo(w *currentWorker, dir string, params *[]sdk.Parameter, branch,

smver, errT := semver.Make(info.GitDescribe)
if errT != nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("git describe %s is not semver compatible", info.GitDescribe),
}
sendLog(res.Reason)
return fmt.Errorf("git.describe invalid")
}

// Prerelease versions
// for 0.31.1-4-g595de235a, smver.Pre = 4-g595de235a
if len(smver.Pre) == 1 {
tuple := strings.Split(smver.Pre[0].String(), "-")
// we split 4-g595de235a, g595de235a is the sha1
if len(tuple) == 2 {
cdsSemver = fmt.Sprintf("%d.%d.%d-%s+%s.cds.%s",
smver.Major,
smver.Minor,
smver.Patch,
tuple[0],
tuple[1],
cdsVersion.Value,
)
sendLog(fmt.Sprintf("!! WARNING !! git describe %s is not semver compatible, we can't create cds.semver variable", info.GitDescribe))
} else {
// Prerelease versions
// for 0.31.1-4-g595de235a, smver.Pre = 4-g595de235a
if len(smver.Pre) == 1 {
tuple := strings.Split(smver.Pre[0].String(), "-")
// we split 4-g595de235a, g595de235a is the sha1
if len(tuple) == 2 {
cdsSemver = fmt.Sprintf("%d.%d.%d-%s+sha.%s.cds.%s",
smver.Major,
smver.Minor,
smver.Patch,
tuple[0],
tuple[1],
cdsVersion.Value,
)
}
}
}

Expand Down Expand Up @@ -264,6 +259,7 @@ func extractInfo(w *currentWorker, dir string, params *[]sdk.Parameter, branch,
}
sendLog(res.Reason)
}
sendLog(fmt.Sprintf("cds.semver: %s", cdsSemver))
}

if branch == "" || branch == "{{.git.branch}}" {
Expand Down
74 changes: 66 additions & 8 deletions engine/worker/builtin_gittag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io/ioutil"
"regexp"
"time"

"github.com/blang/semver"
Expand All @@ -16,14 +17,23 @@ import (

func runGitTag(w *currentWorker) BuiltInAction {
return func(ctx context.Context, a *sdk.Action, buildID int64, params *[]sdk.Parameter, sendLog LoggerFunc) sdk.Result {
tagName := sdk.ParameterFind(&a.Parameters, "tagName")
tagPrerelease := sdk.ParameterFind(&a.Parameters, "tagPrerelease")
tagMetadata := sdk.ParameterFind(&a.Parameters, "tagMetadata")
tagLevel := sdk.ParameterFind(&a.Parameters, "tagLevel")
tagMessage := sdk.ParameterFind(&a.Parameters, "tagMessage")
path := sdk.ParameterFind(&a.Parameters, "path")

if tagName == nil || tagName.Value == "" {
tagLevelValid := true
if tagLevel == nil || tagLevel.Value == "" {
tagLevelValid = false
} else if tagLevel.Value != "major" && tagLevel.Value != "minor" && tagLevel.Value != "patch" {
tagLevelValid = false
}

if !tagLevelValid {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: "Tag name is not set. Nothing to perform.",
Reason: "Tag level is mandatory. It must be: 'major' or 'minor' or 'patch'",
}
sendLog(res.Reason)
return res
Expand All @@ -44,17 +54,65 @@ func runGitTag(w *currentWorker) BuiltInAction {
msg = tagMessage.Value
}

v, errT := semver.Make(tagName.Value)
cdsSemver := sdk.ParameterFind(params, "cds.semver")
if cdsSemver == nil || cdsSemver.Value == "" {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("cds.semver is empty"),
}
sendLog(res.Reason)
return res
}

smver, errT := semver.Make(cdsSemver.Value)
if errT != nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: "Tag name is not semver compatible",
Reason: fmt.Sprintf("cds.version '%s' is not semver compatible", cdsSemver.Value),
}
sendLog(res.Reason)
return res
}
v.Build = nil
v.Pre = nil
smver.Build = nil
smver.Pre = nil

switch tagLevel.Value {
case "major":
smver.Major++
case "minor":
smver.Minor++
default:
smver.Patch++
}

r, _ := regexp.Compile("^([0-9A-Za-z\\-.]+)$")
// prerelease version notes: example: alpha, rc-1, ...
if tagPrerelease != nil && tagPrerelease.Value != "" {
if !r.MatchString(tagPrerelease.Value) {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("tagPrerelease '%s' must comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-.].", tagPrerelease.Value),
}
sendLog(res.Reason)
return res
} else {
smver.Pre = []semver.PRVersion{{VersionStr: tagPrerelease.Value}}
}
}

// metadata: this content is after '+'
if tagMetadata != nil && tagMetadata.Value != "" {
if !r.MatchString(tagMetadata.Value) {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("tagMetadata '%s' must comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-.].", tagMetadata.Value),
}
sendLog(res.Reason)
return res
} else {
smver.Build = []string{tagMetadata.Value}
}
}

var userTag string
userTrig := sdk.ParameterFind(params, "cds.triggered_by.username")
Expand All @@ -79,7 +137,7 @@ func runGitTag(w *currentWorker) BuiltInAction {
//Prepare all options - tag options
var tagOpts = &git.TagOpts{
Message: msg,
Name: v.String(),
Name: smver.String(),
Username: userTag,
}

Expand Down

0 comments on commit fee8d8b

Please sign in to comment.