Skip to content

Commit

Permalink
Feat: fetch Github Installation Id from env0 (#934)
Browse files Browse the repository at this point in the history
* Feat: fetch Github Installation Id from env0

* modify test

* modify test

* formatting

* add example

* fix test

* fix typo
  • Loading branch information
TomerHeber authored Aug 21, 2024
1 parent 6e3153f commit 322bbc6
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 177 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ repos:
- id: go-vet
- id: go-imports
- id: go-mod-tidy
- repo: https://github.com/TekWizely/pre-commit-golang
rev: v1.0.0-beta.5
hooks:
- id: go-staticcheck-mod
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type ApiClientInterface interface {
CloudAccountDelete(id string) error
CloudAccount(id string) (*CloudAccount, error)
CloudAccounts() ([]CloudAccount, error)
VcsToken(vcsType string, repository string) (*VscToken, error)
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
Expand Down
16 changes: 16 additions & 0 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/http/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions client/vcs_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client_test

import (
. "github.com/env0/terraform-provider-env0/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)

var _ = Describe("VCSToken", func() {
vcsType := "github"
repository := "http://myrepo.com/"

mockVcsToken := VscToken{
Token: 12345,
}

Describe("get", func() {
var ret *VscToken
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Get("/vcs-token/"+vcsType, map[string]string{
"organizationId": organizationId,
"repository": repository,
}, gomock.Any()).
Do(func(path string, request interface{}, response *VscToken) {
*response = mockVcsToken
}).Times(1)

ret, err = apiClient.VcsToken(vcsType, repository)
})

It("should return vcs token", func() {
Expect(*ret).To(Equal(mockVcsToken))
})

It("should not return error", func() {
Expect(err).To(BeNil())
})
})

})
22 changes: 22 additions & 0 deletions client/vsc_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

type VscToken struct {
Token int `json:"token"`
}

func (client *ApiClient) VcsToken(vcsType string, repository string) (*VscToken, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, err
}

var result VscToken
if err := client.http.Get("/vcs-token/"+vcsType, map[string]string{
"organizationId": organizationId,
"repository": repository,
}, &result); err != nil {
return nil, err
}

return &result, nil
}
46 changes: 46 additions & 0 deletions env0/data_github_installation_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package env0

import (
"context"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataGithubInstallationId() *schema.Resource {
return &schema.Resource{
Description: "returns the github installation id of a git hub repositroy",

ReadContext: dataGithubInstallationIdRead,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Description: "the name of the repository",
Required: true,
},
"github_installation_id": {
Type: schema.TypeInt,
Description: "the github installation id",
Computed: true,
},
},
}
}

func dataGithubInstallationIdRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

repositroy := d.Get("repository").(string)

token, err := apiClient.VcsToken("github", repositroy)
if err != nil {
return diag.Errorf("failed to get github installation id: %v", err)
}

d.Set("github_installation_id", token.Token)
d.SetId(repositroy)

return nil
}
69 changes: 69 additions & 0 deletions env0/data_github_installation_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package env0

import (
"errors"
"regexp"
"strconv"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestGithubInstallationIdDataSource(t *testing.T) {
mockToken := client.VscToken{
Token: 12345,
}

mockRepositroy := "http://myrepo.com"

resourceType := "env0_github_installation_id"
resourceName := "test"
accessor := dataSourceAccessor(resourceType, resourceName)

getValidTestCase := func(repository string) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"repository": repository,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(mockToken.Token)),
),
},
},
}
}

getErrorTestCase := func(repository string, expectedError string) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"repository": repository,
}),
ExpectError: regexp.MustCompile(expectedError),
},
},
}
}

t.Run("get by repository", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(mockRepositroy),
func(mock *client.MockApiClientInterface) {
mock.EXPECT().VcsToken("github", mockRepositroy).Return(&mockToken, nil).AnyTimes()
},
)
})

t.Run("get by repository - failed", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(mockRepositroy, "failed to get github installation id: error"),
func(mock *client.MockApiClientInterface) {
mock.EXPECT().VcsToken("github", mockRepositroy).Return(nil, errors.New("error")).AnyTimes()
},
)
})
}
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_projects": dataProjects(),
"env0_module_testing_project": dataModuleTestingProject(),
"env0_variable_set": dataVariableSet(),
"env0_github_installation_id": dataGithubInstallationId(),
},
ResourcesMap: map[string]*schema.Resource{
"env0_project": resourceProject(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "env0_github_installation_id" "example" {
repository = "https://github.com/env0/templates"
}

output "github_installation_id" {
value = data.env0_github_installation_id.example.github_installation_id
}
22 changes: 11 additions & 11 deletions tests/integration/004_template/expected_outputs.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"github_template_type": "terraform",
"github_template_name": "Github Test-",
"github_template_repository": "https://github.com/env0/templates",
"gitlab_template_repository": "https://gitlab.com/env0/gitlab-vcs-integration-tests.git",
"github_template_path": "/second",
"tg_tg_version" : "0.35.0",
"data_github_template_type": "terraform",
"github_variables_name": "email",
"github_variables_value": "[email protected]"
}
{
"github_template_type": "terraform",
"github_template_name": "Github Test-",
"github_template_repository": "https://github.com/env0/templates",
"gitlab_template_repository": "https://gitlab.com/env0/gitlab-vcs-integration-tests.git",
"github_template_path": "/second",
"tg_tg_version": "0.35.0",
"data_github_template_type": "terraform",
"github_variables_name": "email",
"github_variables_value": "[email protected]"
}
Loading

0 comments on commit 322bbc6

Please sign in to comment.