Skip to content

Commit

Permalink
Merge pull request drone#20 from ogarcia/commit-by-sha-gogs
Browse files Browse the repository at this point in the history
Fetch specific commit by sha for gogs, closes drone#7
  • Loading branch information
bradrydzewski authored Apr 10, 2019
2 parents 46ec180 + 1c47a13 commit 660f35e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
61 changes: 44 additions & 17 deletions scm/driver/gogs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Re
}

func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
path := fmt.Sprintf("api/v1/repos/%s/commits/%s", repo, ref)
out := new(commitDetail)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertCommit(out), res, err
}

func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
Expand Down Expand Up @@ -71,6 +74,21 @@ type (
Timestamp time.Time `json:"timestamp"`
}

// gogs commit detail object.
commitDetail struct {
Sha string `json:"sha"`
Commit commit `json:"commit"`
Committer committer `json:"committer"`
}

// gogs committer object.
committer struct {
AvatarURL string `json:"avatar_url"`
Login string `json:"login"`
Email string `json:"email"`
FullName string `json:"full_name"`
}

// gogs signature object.
signature struct {
Name string `json:"name"`
Expand Down Expand Up @@ -107,20 +125,29 @@ func convertBranch(src *branch) *scm.Reference {
// return dst
// }

// func convertCommit(src *commit) *scm.Commit {
// return &scm.Commit{
// Sha: src.ID,
// Link: src.URL,
// Message: src.Message,
// Author: convertSignature(src.Author),
// Committer: convertSignature(src.Committer),
// }
// }
func convertCommit(src *commitDetail) *scm.Commit {
return &scm.Commit{
Sha: src.Sha,
Link: src.Commit.URL,
Message: src.Commit.Message,
Author: convertSignature(src.Commit.Author),
Committer: convertCommitter(src.Committer),
}
}

// func convertSignature(src signature) scm.Signature {
// return scm.Signature{
// Login: src.Username,
// Email: src.Email,
// Name: src.Name,
// }
// }
func convertSignature(src signature) scm.Signature {
return scm.Signature{
Login: src.Username,
Email: src.Email,
Name: src.Name,
}
}

func convertCommitter(src committer) scm.Signature {
return scm.Signature{
Avatar: src.AvatarURL,
Login: src.Login,
Email: src.Email,
Name: src.FullName,
}
}
23 changes: 18 additions & 5 deletions scm/driver/gogs/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ import (
//

func TestCommitFind(t *testing.T) {
gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogs/gogs/commits/2c3e2b701e012294d457937e6bfbffd63dd8ae4f").
Reply(200).
Type("application/json").
File("testdata/commits.json")
client, _ := New("https://try.gogs.io")
_, _, err := client.Git.FindCommit(
got, _, err := client.Git.FindCommit(
context.Background(),
"gogits/gogs",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
"gogs/gogs",
"2c3e2b701e012294d457937e6bfbffd63dd8ae4f",
)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
if err != nil {
t.Error(err)
}
want := new(scm.Commit)
raw, _ := ioutil.ReadFile("testdata/commits.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

Expand Down
1 change: 1 addition & 0 deletions scm/driver/gogs/testdata/commits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"url":"https://try.gogs.io/api/v1/repos/gogs/gogs/commits/2c3e2b701e012294d457937e6bfbffd63dd8ae4f","sha":"2c3e2b701e012294d457937e6bfbffd63dd8ae4f","html_url":"https://try.gogs.io/gogs/gogs/commits/2c3e2b701e012294d457937e6bfbffd63dd8ae4f","commit":{"url":"https://try.gogs.io/api/v1/repos/gogs/gogs/commits/2c3e2b701e012294d457937e6bfbffd63dd8ae4f","author":{"name":"Stephen Lane-Walsh","email":"[email protected]","date":"2019-02-17T07:14:37Z"},"committer":{"name":"无闻","email":"[email protected]","date":"2019-02-17T07:14:37Z"},"message":"conf/gitignore: add Unreal Engine (#5623)","tree":{"url":"https://try.gogs.io/api/v1/repos/gogs/gogs/tree/2c3e2b701e012294d457937e6bfbffd63dd8ae4f","sha":"2c3e2b701e012294d457937e6bfbffd63dd8ae4f"}},"author":null,"committer":{"id":1,"username":"unknwon","login":"unknwon","full_name":"Unknwon","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/d8b2871cdac01b57bbda23716cc03b96?d=identicon"},"parents":[{"url":"https://try.gogs.io/api/v1/repos/gogs/gogs/commits/16f95123cd858a84fb5d4336d07d16cb3f7f1ec7","sha":"16f95123cd858a84fb5d4336d07d16cb3f7f1ec7"}]}
15 changes: 15 additions & 0 deletions scm/driver/gogs/testdata/commits.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"author": {
"name": "Stephen Lane-Walsh",
"email": "[email protected]"
},
"committer": {
"name": "Unknwon",
"login": "unknwon",
"email": "[email protected]",
"avatar": "https://secure.gravatar.com/avatar/d8b2871cdac01b57bbda23716cc03b96?d=identicon"
},
"message": "conf/gitignore: add Unreal Engine (#5623)",
"link": "https://try.gogs.io/api/v1/repos/gogs/gogs/commits/2c3e2b701e012294d457937e6bfbffd63dd8ae4f",
"sha": "2c3e2b701e012294d457937e6bfbffd63dd8ae4f"
}

0 comments on commit 660f35e

Please sign in to comment.