Skip to content

Commit

Permalink
GitHub server (#72)
Browse files Browse the repository at this point in the history
* Add github server support

* add github server documentation

Co-authored-by: Lebel <[email protected]>
  • Loading branch information
nesth and Lebel authored Nov 12, 2021
1 parent 0b3ff61 commit dcbefcb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ DRONE_CONVERT_PLUGIN_ENDPOINT=http://1.2.3.4:3000
DRONE_CONVERT_PLUGIN_SECRET=bea26a2221fd8090ea38720fc445eca6
```

## Github Server

1. Create a github token via https://your-github-server-address/settings/token with the scope of `repo`

2. Create a shares secret:

```console
$ openssl rand -hex 16
bea26a2221fd8090ea38720fc445eca6
```

3. Download ran run the plugin:

```console
$ docker run -d \
--publish=3000:3000 \
--env=DRONE_DEBUG=true \
--env=DRONE_SECRET=bea26a2221fd8090ea38720fc445eca6 \
--env=TOKEN=9e6eij3ckzvpe9mrhnqcis6zf8dhopmm46e3pi96 \
--env=PROVIDER=github \
--env=GITHUB_SERVER=https://your-github-server-address
--restart=always \
--name=converter meltwater/drone-convert-pathschanged
```

4. Update your Drone server configuration to include the plugin address and the shared secret.

```text
DRONE_CONVERT_PLUGIN_ENDPOINT=http://1.2.3.4:3000
DRONE_CONVERT_PLUGIN_SECRET=bea26a2221fd8090ea38720fc445eca6
```

## Bitbucket Server

_Bitbucket Server support is currently considered experimental_
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type spec struct {
Provider string `envconfig:"PROVIDER"`
Token string `envconfig:"TOKEN"`
BitBucketAddress string `envconfig:"BB_ADDRESS"`
GithubServer string `envconfig:"GITHUB_SERVER"`
}

func contains(s []string, str string) bool {
Expand Down Expand Up @@ -77,6 +78,7 @@ func main() {
plugin.New(
spec.Token,
spec.Provider,
spec.GithubServer,
),
spec.Secret,
logrus.StandardLogger(),
Expand Down
10 changes: 6 additions & 4 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
token string
provider string
bitbucketAddress string
githubAddress string
}

resource struct {
Expand Down Expand Up @@ -104,10 +105,11 @@ func marshal(in []*resource) ([]byte, error) {
}

// New returns a new conversion plugin.
func New(token string, provider string) converter.Plugin {
func New(token string, provider string, githubAddress string) converter.Plugin {
return &plugin{
token: token,
provider: provider,
token: token,
provider: provider,
githubAddress: githubAddress,
}
}

Expand Down Expand Up @@ -148,7 +150,7 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co

switch p.provider {
case "github":
changedFiles, err = providers.GetGithubFilesChanged(req.Repo, req.Build, p.token)
changedFiles, err = providers.GetGithubFilesChanged(req.Repo, req.Build, p.token, p.githubAddress)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNewEmptyPipeline(t *testing.T) {
}

for _, provider := range providers {
plugin := New("invalidtoken", provider)
plugin := New("invalidtoken", provider, "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ this_is_invalid_yaml
},
}

plugin := New("invalidtoken", "")
plugin := New("invalidtoken", "", "")

_, err := plugin.Convert(noContext, req)
if err == nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ steps:
},
}

plugin := New("invalidtoken", "unsupported")
plugin := New("invalidtoken", "unsupported", "")

_, err := plugin.Convert(noContext, req)
if err == nil {
Expand Down Expand Up @@ -163,7 +163,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -236,7 +236,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -306,7 +306,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -379,7 +379,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

func GetGithubFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string, error) {
func GetGithubFilesChanged(repo drone.Repo, build drone.Build, token string, githubAddress string) ([]string, error) {
newctx := context.Background()
client := github.NewDefault()
var client *scm.Client
var err error

if githubAddress == "" {
client = github.NewDefault()
} else {
client, err = github.New(githubAddress + "/api/v3")
}

client.Client = &http.Client{
Transport: &transport.BearerToken{
Token: token,
Expand All @@ -23,7 +31,6 @@ func GetGithubFilesChanged(repo drone.Repo, build drone.Build, token string) ([]

var changes []*scm.Change
var result *scm.Response
var err error

if build.Before == "" || build.Before == scm.EmptyCommit {
changes, result, err = client.Git.ListChanges(newctx, repo.Slug, build.After, scm.ListOptions{})
Expand Down
4 changes: 2 additions & 2 deletions providers/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetGithubFilesChangedCommit(t *testing.T) {
},
}

got, err := GetGithubFilesChanged(req.Repo, req.Build, "invalidtoken")
got, err := GetGithubFilesChanged(req.Repo, req.Build, "invalidtoken", "")
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestGetGithubFilesChangedCompare(t *testing.T) {
},
}

got, err := GetGithubFilesChanged(req.Repo, req.Build, "invalidtoken")
got, err := GetGithubFilesChanged(req.Repo, req.Build, "invalidtoken", "")
if err != nil {
t.Error(err)
return
Expand Down

0 comments on commit dcbefcb

Please sign in to comment.