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

FTR-93: Refactor cross-project approach #11

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Utils Changelog

## v0.0.10

* Refactor Azure DevOps cross-project approach.

## v0.0.9

* Added support for creating pipelines in Azure DevOps via API.
Expand Down
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"`
}