Skip to content

Commit

Permalink
Added a Pipelines API to give original integrations response without …
Browse files Browse the repository at this point in the history
…unmarshalling (#897)
  • Loading branch information
bhanurp committed Feb 4, 2024
1 parent dd3f704 commit d218b0e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
- [Get Integration by Id](#get-integration-by-id)
- [Get Integration by Name](#get-integration-by-name)
- [Get All Integrations](#get-all-integrations)
- [Get All Raw Integrations](#get-all-raw-integrations)
- [Delete Integration](#delete-integration)
- [Add Pipeline Source](#add-pipeline-source)
- [Get Recent Pipeline Run Status](#get-recent-pipeline-run-status)
Expand Down Expand Up @@ -2305,6 +2306,12 @@ integration, err := pipelinesManager.GetIntegrationByName("integrationName")
integrations, err := pipelinesManager.GetAllIntegrations()
```
#### Get All Raw Integrations
```go
integrations, err := pipelinesManager.GetAllRawIntegrations()
```
#### Delete Integration
```go
Expand Down
6 changes: 6 additions & 0 deletions pipelines/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func (sm *PipelinesServicesManager) GetAllIntegrations() ([]services.Integration
return integrationsService.GetAllIntegrations()
}

func (sm *PipelinesServicesManager) GetAllRawIntegrations() ([]byte, error) {
integrationsService := services.NewIntegrationsService(sm.client)
integrationsService.ServiceDetails = sm.config.GetServiceDetails()
return integrationsService.GetAllRawIntegrations()
}

func (sm *PipelinesServicesManager) DeleteIntegration(integrationId int) error {
integrationsService := services.NewIntegrationsService(sm.client)
integrationsService.ServiceDetails = sm.config.GetServiceDetails()
Expand Down
16 changes: 13 additions & 3 deletions pipelines/services/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ func (is *IntegrationsService) GetIntegrationByName(name string) (*Integration,
}

func (is *IntegrationsService) GetAllIntegrations() ([]Integration, error) {
rawIntegrations, err := is.GetAllRawIntegrations()
if err != nil {
return nil, err
}
integrations := &[]Integration{}
err = json.Unmarshal(rawIntegrations, integrations)
return *integrations, errorutils.CheckError(err)
}

// GetAllRawIntegrations returns the response returned from integrations api without unmarshalling
// into Integration struct.
func (is *IntegrationsService) GetAllRawIntegrations() ([]byte, error) {
log.Debug("Fetching all integrations...")
httpDetails := is.ServiceDetails.CreateHttpClientDetails()
url := is.ServiceDetails.GetUrl() + integrationsRestApi
Expand All @@ -250,9 +262,7 @@ func (is *IntegrationsService) GetAllIntegrations() ([]Integration, error) {
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return nil, err
}
integrations := &[]Integration{}
err = json.Unmarshal(body, integrations)
return *integrations, errorutils.CheckError(err)
return body, errorutils.CheckError(err)
}

type IntegrationAlreadyExistsError struct {
Expand Down
26 changes: 26 additions & 0 deletions tests/pipelinesintegrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestPipelinesIntegrations(t *testing.T) {
t.Run(services.BitbucketServerName, testCreateBitbucketServerIntegration)
t.Run(services.GitlabName, testCreateGitlabIntegration)
t.Run(services.ArtifactoryName, testCreateArtifactoryIntegration)
t.Run("GetAllIntegrations", getAllIntegrationAndAssert)
}

func testCreateGithubIntegrationAndGetByName(t *testing.T) {
Expand Down Expand Up @@ -104,6 +105,31 @@ func getIntegrationAndAssert(t *testing.T, id int, name, integrationType string)
assert.Equal(t, integrationType, integration.MasterIntegrationName)
}

func getAllIntegrationAndAssert(t *testing.T) {
// Create Artifactory Integration
name := getUniqueIntegrationName(services.ArtifactoryName)
id, err := testsPipelinesIntegrationsService.CreateArtifactoryIntegration(name, testsDummyRtUrl, testsDummyUser, testsDummyApiKey)
if !assert.NoError(t, err) {
return
}
defer deleteIntegrationAndAssert(t, id)

// Create Gitlab Integration
name = getUniqueIntegrationName(services.GitlabName)
gitlabIntegrationId, err := testsPipelinesIntegrationsService.CreateGitlabIntegration(name, testsDummyVcsUrl, testsDummyToken)
if !assert.NoError(t, err) {
return
}
defer deleteIntegrationAndAssert(t, gitlabIntegrationId)

integrations, err := testsPipelinesIntegrationsService.GetAllIntegrations()
if !assert.NoError(t, err) {
return
}
assert.NotNil(t, integrations)
assert.True(t, len(integrations) > 2)
}

func getUniqueIntegrationName(integrationType string) string {
return strings.Join([]string{integrationNamesPrefix, integrationType, getCustomRunId('_')}, "_")
}
Expand Down

0 comments on commit d218b0e

Please sign in to comment.