Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth as app #88

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export TABIA_BITBUCKET_USER=
export TABIA_BITBUCKET_TOKEN=
export TABIA_GITHUB_USER=
export TABIA_GITHUB_TOKEN=
export TABIA_GITHUB_APP_INTEGRATION_ID=
export TABIA_GITHUB_APP_PRIVATE_KEY=
export TABIA_GITLAB_INSTANCE=https://gitlab.your.instance.com/
export TABIA_GITLAB_TOKEN=
# when requests should go via proxy
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tabia means characteristic in Swahili. Tabia is giving us insights on the charac

## Setup

Copy `.env.example` to `.env` and fill out the bitbucket token. This environment variable is read by the CLI and tests. Also vscode will read the variable when running tests or starting debugger.
Copy `.env.example` to `.env` and fill out the credentials. This environment variable is read by the CLI and tests. Also vscode will read the variable when running tests or starting debugger.

```bash
cp .env.example .env
Expand Down Expand Up @@ -65,6 +65,18 @@ bin/tabia github --help
bin/tabia github repositories --help
```

#### Authentication

Please note when using Github Authentication there are 2 options to authenticate.

1. Authenticate as a Github App (your app will have to be installed in the organization)
- integration-id
- private-key
2. Authenticate using a Personal Access Token
- token

> :warning: When authenticating as a *GitHub App* please be informed you can only fetch information from **one** organization at a time as the client will be bound to that organizations App installation. To support multiple organizations we require a refactor using a Github client per organization.

### Output - Grimoirelab

To expose the repositories in [Grimoirelab projects.json](https://github.com/chaoss/grimoirelab-sirmordred#projectsjson-) format, you can optionally provide a json file to map repositories to projects. By default the project will be mapped to the owner of the repository. Anything not matching the rules will fall back to this default.
Expand Down
56 changes: 49 additions & 7 deletions cmd/cmd_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ func createGithub() *cli.Command {
Aliases: []string{"t"},
Usage: "Calls the api using the given `TOKEN`",
EnvVars: []string{"TABIA_GITHUB_TOKEN"},
Required: true,
Required: false,
},
&cli.StringFlag{
Name: "integration-id",
Aliases: []string{"app-id"},
Usage: "Authenticates to Github using the given `APP_INTEGRATION_ID`",
EnvVars: []string{"TABIA_GITHUB_APP_INTEGRATION_ID"},
Required: false,
},
&cli.PathFlag{
Name: "private-key",
Usage: "Authenticates to Github using the given `APP_PRIVATE_KEY`",
EnvVars: []string{"TABIA_GITHUB_APP_PRIVATE_KEY"},
Required: false,
TakesFile: true,
},
&cli.BoolFlag{
Name: "verbose",
Expand Down Expand Up @@ -130,24 +144,46 @@ func createGithub() *cli.Command {
}
}

func newGithubClient(c *cli.Context) *github.Client {
func newGithubClient(c *cli.Context) (*github.Client, error) {
verbose := c.Bool("verbose")
token := c.String("token")

var ghWriter io.Writer
if verbose {
ghWriter = c.App.Writer
}

return github.NewClientWithTokenAuth(token, ghWriter)
if c.IsSet("integration-id") && c.IsSet("private-key") {
integrationID := c.Int64("integration-id")
privateKey := c.Path("private-key")

privateKeyBytes, err := os.ReadFile(privateKey)
if err != nil {
return nil, err
}
org := append(c.StringSlice("owner"), c.StringSlice("organization")...)

client, err := github.NewClientWithAppAuth(integrationID, string(privateKeyBytes), org[0], ghWriter)
return client, nil
}

if !c.IsSet("token") {
return nil, fmt.Errorf("no `integration-id` and `private-key` or `token` provided")
}

token := c.String("token")
return github.NewClientWithTokenAuth(token, ghWriter), nil
}

func githubMembers(c *cli.Context) error {
owners := c.StringSlice("organization")
format := c.String("format")
filter := c.String("filter")

client := newGithubClient(c)
client, err := newGithubClient(c)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(c.Context)
defer cancel()

Expand Down Expand Up @@ -198,7 +234,10 @@ func githubRepositories(c *cli.Context) error {
format := c.String("format")
filter := c.String("filter")

client := newGithubClient(c)
client, err := newGithubClient(c)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

Expand Down Expand Up @@ -274,7 +313,10 @@ func githubContents(c *cli.Context) error {
filePath := c.String("file")
output := c.Path("output")

client := newGithubClient(c)
client, err := newGithubClient(c)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"text/tabwriter"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -111,7 +111,7 @@ func gitlabRepositories(c *cli.Context) error {
}

templateFile := c.Path("template")
tmplContent, err := ioutil.ReadFile(templateFile)
tmplContent, err := os.ReadFile(templateFile)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ require (
github.com/antonmedv/expr v1.8.9
github.com/google/go-github/v33 v33.0.0
github.com/goreleaser/goreleaser v0.157.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/palantir/go-githubapp v0.6.0
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/stretchr/testify v1.7.0
Expand Down
Loading