Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
frasdav committed Nov 20, 2023
1 parent 42e2888 commit a2fd07a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
51 changes: 34 additions & 17 deletions azuredevops/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// CreateBuildDefinition creates a new BuildDefinition
func (a *AzureDevOps) CreateBuildDefinition(projectName string, repositoryId string, folderPath string, definitionName string, yamlFilename string) (*build.BuildDefinition, error) {
func (a *AzureDevOps) CreateBuildDefinition(projectName string, definitionName string, repositoryId string, folderPath string, yamlFilename string) (*build.BuildDefinition, error) {
client, err := build.NewClient(a.ctx, a.connection)
if err != nil {
return nil, err
Expand Down Expand Up @@ -82,14 +82,14 @@ func (a *AzureDevOps) GetBuild(projectName string, buildId int) (*build.Build, e
}

// GetBuildDefinitionByName gets a BuildDefinitionReference by name
func (a *AzureDevOps) GetBuildDefinitionByName(projectName string, name string) (*build.BuildDefinitionReference, error) {
func (a *AzureDevOps) GetBuildDefinitionByName(projectName string, definitionName string) (*build.BuildDefinition, error) {
client, err := build.NewClient(a.ctx, a.connection)
if err != nil {
return nil, err
}

getDefinitionsArgs := build.GetDefinitionsArgs{
Name: &name,
Name: &definitionName,
Project: &projectName,
}
definitions, err := client.GetDefinitions(a.ctx, getDefinitionsArgs)
Expand All @@ -98,27 +98,44 @@ func (a *AzureDevOps) GetBuildDefinitionByName(projectName string, name string)
}

if len(definitions.Value) == 0 {
// return nil, fmt.Errorf("build definition with name '%s' not found in project '%s'", name, projectName)
return nil, &BuildNotFoundError{
name: name,
projectName: projectName,
}
return nil, fmt.Errorf("build definition with name '%s' not found in project '%s'", definitionName, projectName)
}
if len(definitions.Value) > 1 {
return nil, fmt.Errorf("multiple build definitions with name '%s' found in project '%s'", name, projectName)
return nil, fmt.Errorf("multiple build definitions with name '%s' found in project '%s'", definitionName, projectName)
}

return &definitions.Value[0], nil
getDefinitionArgs := build.GetDefinitionArgs{
DefinitionId: definitions.Value[0].Id,
Project: &projectName,
}
return client.GetDefinition(a.ctx, getDefinitionArgs)
}

type CustomQueueBuildArgs struct {
Definition CustomDefinition `json:"definition"`
SourceBranch string `json:"sourceBranch"`
TemplateParameters map[string]string `json:"templateParameters"`
}
// GetOrCreateBuildDefinition gets or creates a build definition
func (a *AzureDevOps) GetOrCreateBuildDefinition(projectName string, definitionName string, repositoryId string, folderPath string, yamlFilename string) (*build.BuildDefinition, error) {
client, err := build.NewClient(a.ctx, a.connection)
if err != nil {
return nil, err
}

getDefinitionsArgs := build.GetDefinitionsArgs{
Name: &definitionName,
Project: &projectName,
}
definitions, err := client.GetDefinitions(a.ctx, getDefinitionsArgs)
if err != nil {
return nil, err
}

type CustomDefinition struct {
ID *int `json:"id"`
if len(definitions.Value) > 1 {
return nil, fmt.Errorf("multiple build definitions with name '%s' found in project '%s'", definitionName, projectName)
}

if len(definitions.Value) == 0 {
return a.CreateBuildDefinition(projectName, definitionName, repositoryId, folderPath, yamlFilename)
} else {
return a.GetBuildDefinitionByName(projectName, definitionName)
}
}

// QueueBuild queues and returns a new Build
Expand Down
12 changes: 6 additions & 6 deletions azuredevops/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azuredevops

import (
"context"
"fmt"

"github.com/microsoft/azure-devops-go-api/azuredevops"
)
Expand All @@ -12,11 +11,12 @@ type AzureDevOps struct {
ctx context.Context
}

type BuildNotFoundError struct {
name string
projectName string
type CustomQueueBuildArgs struct {
Definition CustomDefinition `json:"definition"`
SourceBranch string `json:"sourceBranch"`
TemplateParameters map[string]string `json:"templateParameters"`
}

func (b *BuildNotFoundError) Error() string {
return fmt.Sprintf("build definition with name '%s' not found in project '%s'", b.name, b.projectName)
type CustomDefinition struct {
ID *int `json:"id"`
}

0 comments on commit a2fd07a

Please sign in to comment.