Skip to content

Commit

Permalink
Merge pull request #149 from tphoney/commit_add_path
Browse files Browse the repository at this point in the history
(feat) add path support for list commits on github and gitlab
  • Loading branch information
TP Honey authored Feb 9, 2022
2 parents dafaf7e + 62e8881 commit d2b32a6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 52 deletions.
108 changes: 60 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Doc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](http://godoc.org/github.com/drone/go-scm/scm)

Package scm provides a unified interface to multiple source code management systems including GitHub, GitHub Enterprise, Bitbucket, Bitbucket Server, Gitea and Gogs.
Package scm provides a unified interface to multiple source code management systems including GitHub, GitHub Enterprise, Bitbucket, Bitbucket Server, Gitee, Gitea and Gogs.

## Getting Started

Expand All @@ -12,21 +12,21 @@ Create a GitHub client:
package main

import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
)

func main() {
client := github.NewDefault()
client := github.NewDefault()
}
```

Create a GitHub Enterprise client:

```Go
import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
)

func main() {
Expand All @@ -38,8 +38,8 @@ Create a Bitbucket client:

```Go
import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/bitbucket"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/bitbucket"
)

func main() {
Expand All @@ -51,8 +51,8 @@ Create a Bitbucket Server (Stash) client:

```Go
import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/stash"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/stash"
)

func main() {
Expand All @@ -64,25 +64,25 @@ Create a Gitea client:

```Go
import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/gitea"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/gitea"
)

func main() {
client, err := gitea.New("https://gitea.company.com")
client, err := gitea.New("https://gitea.company.com")
}
```

Create a Gitee client:

```Go
import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/gitee"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/gitee"
)

func main() {
client, err := gitee.New("https://gitee.com/api/v5")
client, err := gitee.New("https://gitee.com/api/v5")
}
```

Expand All @@ -94,35 +94,35 @@ The scm client does not directly handle authentication. Instead, when creating a
package main

import (
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
"github.com/drone/go-scm/scm/transport"
"github.com/drone/go-scm/scm/transport/oauth2"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
"github.com/drone/go-scm/scm/transport"
"github.com/drone/go-scm/scm/transport/oauth2"
)

func main() {
client := github.NewDefault()

// provide a custom http.Client with a transport
// that injects the oauth2 token.
client.Client = &http.Client{
Transport: &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&scm.Token{
Token: "ecf4c1f9869f59758e679ab54b4",
},
),
},
}

// provide a custom http.Client with a transport
// that injects the private GitLab token through
// the PRIVATE_TOKEN header variable.
client.Client = &http.Client{
Transport: &transport.PrivateToken{
Token: "ecf4c1f9869f59758e679ab54b4",
},
}
client := github.NewDefault()

// provide a custom http.Client with a transport
// that injects the oauth2 token.
client.Client = &http.Client{
Transport: &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&scm.Token{
Token: "ecf4c1f9869f59758e679ab54b4",
},
),
},
}

// provide a custom http.Client with a transport
// that injects the private GitLab token through
// the PRIVATE_TOKEN header variable.
client.Client = &http.Client{
Transport: &transport.PrivateToken{
Token: "ecf4c1f9869f59758e679ab54b4",
},
}
}
```

Expand All @@ -136,14 +136,14 @@ Example code to get an issue:
issue, _, err := client.Issues.Find(ctx, "octocat/Hello-World", 1)
```

Example code to get a list of issues:
Example code to get a list of issues:

```Go
opts := scm.IssueListOptions{
Page: 1,
Size: 30,
Open: true,
Closed: false,
Page: 1,
Size: 30,
Open: true,
Closed: false,
}

issues, _, err := client.Issues.List(ctx, "octocat/Hello-World", opts)
Expand All @@ -153,12 +153,24 @@ Example code to create an issue comment:

```Go
in := &scm.CommentInput{
Body: "Found a bug",
Body: "Found a bug",
}

comment, _, err := client.Issues.CreateComment(ctx, "octocat/Hello-World", 1, in)
```

## Useful links

Here are some useful links to providers API documentation:

Bitbucket cloud API: `https://developer.atlassian.com/cloud/bitbucket/rest/intro/`
Bitbucket server/Stash API: `https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html`
Gitea API: `https://gitea.com/api/swagger/#/`
Gitee API: `https://gitee.com/api/swagger/#/`
Github API: `https://docs.github.com/en/rest/reference`
Gitlab API: `https://docs.gitlab.com/ee/api/api_resources.html`
Gogs API: `https://github.com/gogs/docs-api`

## Release procedure

Run the changelog generator.
Expand Down
5 changes: 4 additions & 1 deletion scm/driver/github/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func encodeCommitListOptions(opts scm.CommitListOptions) string {
if opts.Ref != "" {
params.Set("ref", opts.Ref)
}
if opts.Path != "" {
params.Set("path", opts.Path)
}
return params.Encode()
}

Expand Down Expand Up @@ -98,4 +101,4 @@ func encodeReleaseListOptions(opts scm.ReleaseListOptions) string {
params.Set("state", "closed")
}
return params.Encode()
}
}
3 changes: 2 additions & 1 deletion scm/driver/github/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func Test_encodeCommitListOptions(t *testing.T) {
Page: 10,
Size: 30,
Ref: "master",
Path: "readme.md",
}
want := "page=10&per_page=30&ref=master"
want := "page=10&path=readme.md&per_page=30&ref=master"
got := encodeCommitListOptions(opts)
if got != want {
t.Errorf("Want encoded commit list options %q, got %q", want, got)
Expand Down
5 changes: 4 additions & 1 deletion scm/driver/gitlab/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func encodeCommitListOptions(opts scm.CommitListOptions) string {
if opts.Ref != "" {
params.Set("ref_name", opts.Ref)
}
if opts.Path != "" {
params.Set("path", opts.Path)
}
return params.Encode()
}

Expand Down Expand Up @@ -111,4 +114,4 @@ func encodeMilestoneListOptions(opts scm.MilestoneListOptions) string {
params.Set("state", "closed")
}
return params.Encode()
}
}
3 changes: 2 additions & 1 deletion scm/driver/gitlab/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func Test_encodeCommitListOptions(t *testing.T) {
Page: 10,
Size: 30,
Ref: "master",
Path: "Readme.md",
}
want := "page=10&per_page=30&ref_name=master"
want := "page=10&path=Readme.md&per_page=30&ref_name=master"
got := encodeCommitListOptions(opts)
if got != want {
t.Errorf("Want encoded commit list options %q, got %q", want, got)
Expand Down

0 comments on commit d2b32a6

Please sign in to comment.