Skip to content

Commit

Permalink
Merge branch 'main' into fix-checks-app-id
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Oct 11, 2023
2 parents 5fa6c3b + ce9034e commit b30064a
Show file tree
Hide file tree
Showing 121 changed files with 2,347 additions and 2,403 deletions.
15 changes: 11 additions & 4 deletions github/data_source_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{}
opts.Ref = branch.(string)
}

fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
fc, dc, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
if err != nil {
if err, ok := err.(*github.ErrorResponse); ok {
if err.Response.StatusCode == http.StatusNotFound {
Expand All @@ -107,15 +107,22 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{}
return err
}

d.Set("repository", repo)
d.SetId(fmt.Sprintf("%s/%s", repo, file))
d.Set("file", file)

// If the repo is a directory, then there is nothing else we can include in
// the schema.
if dc != nil {
return nil
}

content, err := fc.GetContent()
if err != nil {
return err
}

d.SetId(fmt.Sprintf("%s/%s", repo, file))
d.Set("content", content)
d.Set("repository", repo)
d.Set("file", file)
d.Set("sha", fc.GetSHA())

parsedUrl, err := url.Parse(fc.GetURL())
Expand Down
64 changes: 64 additions & 0 deletions github/data_source_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,68 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) {
})

})

repoContentDirectoryRespBody := marshal(t, []github.RepositoryContent{
{
Encoding: &enc,
Content: &b64FileContent,
SHA: &sha,
URL: &apiUrl,
},
})

t.Run("extract only non-file data if the path is for a directory", func(t *testing.T) {
// test setup
repositoryFullName := fmt.Sprintf("%s/%s", org, repo)

expectedID := fmt.Sprintf("%s/%s", repo, fileName)
expectedRepo := "test-repo"

ts := githubApiMock([]*mockResponse{
{
ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch),
ResponseBody: repoContentDirectoryRespBody,
StatusCode: http.StatusOK,
},
})
defer ts.Close()

httpCl := http.DefaultClient
httpCl.Transport = http.DefaultTransport

client := github.NewClient(httpCl)
u, _ := url.Parse(ts.URL + "/")
client.BaseURL = u

meta := &Owner{
name: owner,
v3client: client,
}

testSchema := map[string]*schema.Schema{
"repository": {Type: schema.TypeString},
"file": {Type: schema.TypeString},
"branch": {Type: schema.TypeString},
"commit_sha": {Type: schema.TypeString},
"content": {Type: schema.TypeString},
"id": {Type: schema.TypeString},
}

schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{
"repository": repositoryFullName,
"file": fileName,
"branch": branch,
"commit_sha": sha,
})

// actual call
err := dataSourceGithubRepositoryFileRead(schema, meta)

// assertions
assert.Nil(t, err)
assert.Equal(t, expectedRepo, schema.Get("repository"))
assert.Equal(t, expectedID, schema.Get("id"))
assert.Equal(t, "", schema.Get("content"))
assert.Equal(t, nil, schema.Get("sha"))
})
}
1 change: 1 addition & 0 deletions github/resource_github_repository_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf
return nil
}
}
return err
}

d.Set("repository", repoName)
Expand Down
5 changes: 4 additions & 1 deletion github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ func resourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{})
if branch, ok := d.GetOk("branch"); ok {
log.Printf("[DEBUG] Using explicitly set branch: %s", branch.(string))
if err := checkRepositoryBranchExists(client, owner, repo, branch.(string)); err != nil {
return err
log.Printf("[INFO] Removing repository path %s/%s/%s from state because the branch no longer exists in GitHub",
owner, repo, file)
d.SetId("")
return nil
}
opts.Ref = branch.(string)
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.13.0
golang.org/x/oauth2 v0.12.0
golang.org/x/crypto v0.14.0
golang.org/x/oauth2 v0.13.0
gopkg.in/square/go-jose.v2 v2.6.0
)

Expand Down Expand Up @@ -198,9 +198,9 @@ require (
go.opencensus.io v0.24.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
Loading

0 comments on commit b30064a

Please sign in to comment.