Skip to content

Commit

Permalink
consistency in returning sha
Browse files Browse the repository at this point in the history
  • Loading branch information
TP Honey committed Apr 7, 2021
1 parent a300876 commit 453c612
Show file tree
Hide file tree
Showing 27 changed files with 1,151 additions and 38 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/drone/go-scm
require (
github.com/google/go-cmp v0.2.0
github.com/h2non/gock v1.0.9
github.com/mitchellh/mapstructure v1.4.1
)

go 1.13
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
12 changes: 5 additions & 7 deletions scm/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import "context"
type (
// Content stores the contents of a repository file.
Content struct {
Path string
Data []byte

// the hash of the blob, sometimes referred to
// as the blob id or blob sha. this is the equivalent
// to running the git hash-object command.
Hash string
Path string
Data []byte
Sha string
BlobID string
}

// ContentParams provide parameters for creating and
Expand All @@ -26,6 +23,7 @@ type (
Message string
Data []byte
Sha string
BlobID string
Signature Signature
}

Expand Down
4 changes: 1 addition & 3 deletions scm/driver/bitbucket/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
metaOut := new(metaContent)
metaRes, metaErr := s.client.do(ctx, "GET", metaEndpoint, nil, metaOut)
if metaErr == nil {
content.Hash = metaOut.Commit.Hash
content.Sha = metaOut.Commit.Hash
return content, metaRes, metaErr
} else {
// do not risk that returning an error if getting the meta fails.
Expand All @@ -46,7 +46,6 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
Message: params.Message,
Branch: params.Branch,
Content: params.Data,
Sha: params.Sha,
Author: fmt.Sprintf("%s <%s>", params.Signature.Name, params.Signature.Email),
}
res, err := s.client.do(ctx, "POST", endpoint, in, nil)
Expand Down Expand Up @@ -94,7 +93,6 @@ type content struct {
type metaContent struct {
Path string `json:"path"`
Commit struct {
// commit hash not the blob sha
Hash string `json:"hash"`
} `json:"commit"`
}
Expand Down
31 changes: 31 additions & 0 deletions scm/driver/bitbucket/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ func TestContentUpdate(t *testing.T) {
}
}

func TestContentUpdateBadCommitID(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Post("/2.0/repositories/atlassian/atlaskit/src").
Reply(400).
Type("application/json").
File("testdata/content_update.json.fail")

params := &scm.ContentParams{
Message: "my commit message",
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
Sha: "bad commit",
Signature: scm.Signature{
Name: "Monalisa Octocat",
Email: "octocat@github.com",
},
}

client := NewDefault()
_, err := client.Contents.Update(
context.Background(),
"atlassian/atlaskit",
"test/hello",
params,
)
if err.Error() != "parents: Commit not found: 1a7eba6c-d4fe-47b7-b767-859abc660efc" {
t.Errorf("Expecting 'parents: Commit not found: 1a7eba6c-d4fe-47b7-b767-859abc660efc'")
}
}

func TestContentDelete(t *testing.T) {
content := new(contentService)
_, err := content.Delete(context.Background(), "atlassian/atlaskit", "README", "master")
Expand Down
32 changes: 31 additions & 1 deletion scm/driver/bitbucket/testdata/content.json
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
{"mimetype":null,"links":{"self":{"href":"https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/src/0846a192175701903ddd9264ece31922d8437c1a/README.md"},"meta":{"href":"https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/src/0846a192175701903ddd9264ece31922d8437c1a/README.md?format=meta"},"history":{"href":"https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/filehistory/0846a192175701903ddd9264ece31922d8437c1a/README.md"}},"escaped_path":"README.md","path":"README.md","commit":{"type":"commit","hash":"0846a192175701903ddd9264ece31922d8437c1a","links":{"self":{"href":"https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/commit/0846a192175701903ddd9264ece31922d8437c1a"},"html":{"href":"https://bitbucket.org/tphoney/scm-test/commits/0846a192175701903ddd9264ece31922d8437c1a"}}},"attributes":[],"type":"commit_file","size":39}
{
"mimetype": null,
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/src/0846a192175701903ddd9264ece31922d8437c1a/README.md"
},
"meta": {
"href": "https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/src/0846a192175701903ddd9264ece31922d8437c1a/README.md?format=meta"
},
"history": {
"href": "https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/filehistory/0846a192175701903ddd9264ece31922d8437c1a/README.md"
}
},
"escaped_path": "README.md",
"path": "README.md",
"commit": {
"type": "commit",
"hash": "0846a192175701903ddd9264ece31922d8437c1a",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/tphoney/scm-test/commit/0846a192175701903ddd9264ece31922d8437c1a"
},
"html": {
"href": "https://bitbucket.org/tphoney/scm-test/commits/0846a192175701903ddd9264ece31922d8437c1a"
}
}
},
"attributes": [],
"type": "commit_file",
"size": 39
}
2 changes: 1 addition & 1 deletion scm/driver/bitbucket/testdata/content.json.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Path": "README",
"Data": "SEVMTE8gV09STEQK",
"Hash": "0846a192175701903ddd9264ece31922d8437c1a"
"Sha": "0846a192175701903ddd9264ece31922d8437c1a"
}
7 changes: 6 additions & 1 deletion scm/driver/bitbucket/testdata/content_fail.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{"type":"error","error":{"message":"No such file or directory: README.md.asdas"}}
{
"type": "error",
"error": {
"message": "No such file or directory: README.md.asdas"
}
}
Loading

0 comments on commit 453c612

Please sign in to comment.