Skip to content

Commit

Permalink
feat: add a spec struct / spec validation
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Mar 8, 2022
1 parent 89cb1cb commit fa4d760
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 151 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Philipp Heuer
Copyright (c) 2022 Philipp Heuer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/go-git/go-git/v5 v5.4.2
github.com/go-playground/validator/v10 v10.10.0
github.com/google/go-github/v35 v35.3.0
github.com/gosimple/slug v1.12.0
github.com/rs/zerolog v1.26.1
Expand All @@ -21,6 +22,8 @@ require (
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
Expand All @@ -29,13 +32,15 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.1 // indirect
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
Expand Down
19 changes: 18 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -167,24 +175,31 @@ github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
Expand All @@ -197,6 +212,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xanzy/go-gitlab v0.54.3 h1:fPfZ3Jcu5dPc3xyIYtAALZsEgoyKNFNuULD+TdJ7Zvk=
Expand Down Expand Up @@ -344,6 +360,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
91 changes: 57 additions & 34 deletions pkg/azuredevops/azuredevops.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package azuredevops

import (
"github.com/cidverse/normalizeci/pkg/ncispec"
"github.com/cidverse/normalizeci/pkg/projectdetails"
"github.com/cidverse/normalizeci/pkg/vcsrepository"
"github.com/gosimple/slug"
Expand Down Expand Up @@ -34,66 +35,88 @@ func (n Normalizer) Check(env map[string]string) bool {

// Normalize normalizes the environment variables into the common format
func (n Normalizer) Normalize(env map[string]string) map[string]string {
data := make(map[string]string)
nci := ncispec.OfMap(env)

// common
data["NCI"] = "true"
data["NCI_VERSION"] = n.version
data["NCI_SERVICE_NAME"] = n.name
data["NCI_SERVICE_SLUG"] = n.slug
nci.NCI = "true"
nci.NCI_VERSION = n.version
nci.NCI_SERVICE_NAME = n.name
nci.NCI_SERVICE_SLUG = n.slug

// worker
data["NCI_WORKER_ID"] = env["AGENT_ID"]
data["NCI_WORKER_NAME"] = env["AGENT_MACHINENAME"]
data["NCI_WORKER_VERSION"] = env["AGENT_VERSION"]
data["NCI_WORKER_ARCH"] = runtime.GOOS + "/" + runtime.GOARCH
nci.NCI_WORKER_ID = env["AGENT_ID"]
nci.NCI_WORKER_NAME = env["AGENT_MACHINENAME"]
nci.NCI_WORKER_VERSION = env["AGENT_VERSION"]
nci.NCI_WORKER_ARCH = runtime.GOOS + "/" + runtime.GOARCH

// pipeline
if env["BUILD_REASON"] == "Manual" {
data["NCI_PIPELINE_TRIGGER"] = "manual"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerManual
} else if env["BUILD_REASON"] == "IndividualCI" || env["BUILD_REASON"] == "BatchedCI" {
data["NCI_PIPELINE_TRIGGER"] = "push"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerPush
} else if env["BUILD_REASON"] == "Schedule" {
data["NCI_PIPELINE_TRIGGER"] = "schedule"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerSchedule
} else if env["BUILD_REASON"] == "PullRequest" {
data["NCI_PIPELINE_TRIGGER"] = "pull_request"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerPullRequest
} else if env["BUILD_REASON"] == "BuildCompletion" {
data["NCI_PIPELINE_TRIGGER"] = "build"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerBuild
} else {
data["NCI_PIPELINE_TRIGGER"] = "unknown"
nci.NCI_PIPELINE_TRIGGER = ncispec.PipelineTriggerUnknown
}
data["NCI_PIPELINE_STAGE_NAME"] = env["SYSTEM_STAGENAME"] // SYSTEM_STAGEDISPLAYNAME
data["NCI_PIPELINE_STAGE_SLUG"] = slug.Make(env["SYSTEM_STAGENAME"])
data["NCI_PIPELINE_JOB_NAME"] = env["SYSTEM_JOBNAME"] // SYSTEM_JOBDISPLAYNAME
data["NCI_PIPELINE_JOB_SLUG"] = slug.Make(env["SYSTEM_JOBNAME"])
nci.NCI_PIPELINE_STAGE_NAME = env["SYSTEM_STAGENAME"] // SYSTEM_STAGEDISPLAYNAME
nci.NCI_PIPELINE_STAGE_SLUG = slug.Make(env["SYSTEM_STAGENAME"])
nci.NCI_PIPELINE_JOB_NAME = env["SYSTEM_JOBNAME"] // SYSTEM_JOBDISPLAYNAME
nci.NCI_PIPELINE_JOB_SLUG = slug.Make(env["SYSTEM_JOBNAME"])

// repository
projectDir := vcsrepository.FindRepositoryDirectory(common.GetWorkingDirectory())
addData, addDataErr := vcsrepository.GetVCSRepositoryInformation(projectDir)
vcsData, addDataErr := vcsrepository.GetVCSRepositoryInformation(projectDir)
if addDataErr != nil {
panic(addDataErr)
}
for addKey, addElement := range addData {
data[addKey] = addElement
}
nci.NCI_REPOSITORY_KIND = vcsData[ncispec.NCI_REPOSITORY_KIND]
nci.NCI_REPOSITORY_REMOTE = vcsData[ncispec.NCI_REPOSITORY_REMOTE]
nci.NCI_COMMIT_REF_TYPE = vcsData[ncispec.NCI_COMMIT_REF_TYPE]
nci.NCI_COMMIT_REF_NAME = vcsData[ncispec.NCI_COMMIT_REF_NAME]
nci.NCI_COMMIT_REF_PATH = vcsData[ncispec.NCI_COMMIT_REF_PATH]
nci.NCI_COMMIT_REF_SLUG = vcsData[ncispec.NCI_COMMIT_REF_SLUG]
nci.NCI_COMMIT_REF_VCS = vcsData[ncispec.NCI_COMMIT_REF_VCS]
nci.NCI_COMMIT_REF_RELEASE = vcsData[ncispec.NCI_COMMIT_REF_RELEASE]
nci.NCI_COMMIT_SHA = vcsData[ncispec.NCI_COMMIT_SHA]
nci.NCI_COMMIT_SHA_SHORT = vcsData[ncispec.NCI_COMMIT_SHA_SHORT]
nci.NCI_COMMIT_TITLE = vcsData[ncispec.NCI_COMMIT_TITLE]
nci.NCI_COMMIT_DESCRIPTION = vcsData[ncispec.NCI_COMMIT_DESCRIPTION]
nci.NCI_COMMIT_AUTHOR_NAME = vcsData[ncispec.NCI_COMMIT_AUTHOR_NAME]
nci.NCI_COMMIT_AUTHOR_EMAIL = vcsData[ncispec.NCI_COMMIT_AUTHOR_EMAIL]
nci.NCI_COMMIT_COMMITTER_NAME = vcsData[ncispec.NCI_COMMIT_COMMITTER_NAME]
nci.NCI_COMMIT_COMMITTER_EMAIL = vcsData[ncispec.NCI_COMMIT_COMMITTER_EMAIL]
nci.NCI_COMMIT_COUNT = vcsData[ncispec.NCI_COMMIT_COUNT]

// project details
projectData := projectdetails.GetProjectDetails(data["NCI_REPOSITORY_KIND"], data["NCI_REPOSITORY_REMOTE"])
projectData := projectdetails.GetProjectDetails(nci.NCI_REPOSITORY_KIND, nci.NCI_REPOSITORY_REMOTE)
if projectData != nil {
for addKey, addElement := range projectData {
data[addKey] = addElement
}
nci.NCI_PROJECT_ID = projectData[ncispec.NCI_PROJECT_ID]
nci.NCI_PROJECT_NAME = projectData[ncispec.NCI_PROJECT_NAME]
nci.NCI_PROJECT_SLUG = projectData[ncispec.NCI_PROJECT_SLUG]
nci.NCI_PROJECT_DESCRIPTION = projectData[ncispec.NCI_PROJECT_DESCRIPTION]
nci.NCI_PROJECT_TOPICS = projectData[ncispec.NCI_PROJECT_TOPICS]
nci.NCI_PROJECT_ISSUE_URL = projectData[ncispec.NCI_PROJECT_ISSUE_URL]
nci.NCI_PROJECT_STARGAZERS = projectData[ncispec.NCI_PROJECT_STARGAZERS]
nci.NCI_PROJECT_FORKS = projectData[ncispec.NCI_PROJECT_FORKS]
}
data["NCI_PROJECT_DIR"] = projectDir
nci.NCI_PROJECT_DIR = projectDir

// container registry
data["NCI_CONTAINERREGISTRY_HOST"] = ""
data["NCI_CONTAINERREGISTRY_REPOSITORY"] = slug.Make(common.GetDirectoryNameFromPath(filepath.Join(vcsrepository.FindRepositoryDirectory(common.GetWorkingDirectory())+string(os.PathSeparator), "file")))
data["NCI_CONTAINERREGISTRY_USERNAME"] = ""
data["NCI_CONTAINERREGISTRY_PASSWORD"] = ""
data["NCI_CONTAINERREGISTRY_TAG"] = data["NCI_COMMIT_REF_RELEASE"]
nci.NCI_CONTAINERREGISTRY_HOST = ""
nci.NCI_CONTAINERREGISTRY_REPOSITORY = slug.Make(common.GetDirectoryNameFromPath(filepath.Join(vcsrepository.FindRepositoryDirectory(common.GetWorkingDirectory())+string(os.PathSeparator), "file")))
nci.NCI_CONTAINERREGISTRY_USERNAME = ""
nci.NCI_CONTAINERREGISTRY_PASSWORD = ""
nci.NCI_CONTAINERREGISTRY_TAG = nci.NCI_COMMIT_REF_RELEASE

// control
nci.NCI_DEPLOY_FREEZE = "false"

return data
return ncispec.ToMap(nci)
}

func (n Normalizer) Denormalize(env map[string]string) map[string]string {
Expand Down
11 changes: 11 additions & 0 deletions pkg/azuredevops/azuredevops_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package azuredevops

import (
"github.com/cidverse/normalizeci/pkg/ncispec"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"os"
Expand Down Expand Up @@ -184,3 +185,13 @@ func TestEnvironmentNormalizer(t *testing.T) {
assert.Equal(t, "Build", normalized["NCI_PIPELINE_JOB_NAME"])
assert.Equal(t, "build", normalized["NCI_PIPELINE_JOB_SLUG"])
}

func TestValidateSpec(t *testing.T) {
var normalizer = NewNormalizer()
var normalized = normalizer.Normalize(common.GetEnvironmentFrom(testEnvironment))

nci := ncispec.OfMap(normalized)

err := nci.Validate()
assert.Emptyf(t, err, "there shouldn't be any validation errors")
}
Loading

0 comments on commit fa4d760

Please sign in to comment.