Skip to content

Commit a0e4dd3

Browse files
committed
Utilize an AppInstallation token to get the correct permissions
1 parent a3e35a4 commit a0e4dd3

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ bin/tabia github --help
6565
bin/tabia github repositories --help
6666
```
6767

68+
#### Authentication
69+
70+
Please note when using Github Authentication there are 2 options to authenticate.
71+
72+
1. Authenticate as a Github App (your app will have to be installed in the organization)
73+
- integration-id
74+
- private-key
75+
2. Authenticate using a Personal Access Token
76+
- token
77+
78+
> :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.
79+
6880
### Output - Grimoirelab
6981

7082
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.

cmd/cmd_github.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func newGithubClient(c *cli.Context) (*github.Client, error) {
161161
if err != nil {
162162
return nil, err
163163
}
164-
client, err := github.NewClientWithAppAuth(integrationID, string(privateKeyBytes), ghWriter)
164+
org := append(c.StringSlice("owner"), c.StringSlice("organization")...)
165+
166+
client, err := github.NewClientWithAppAuth(integrationID, string(privateKeyBytes), org[0], ghWriter)
165167
return client, nil
166168
}
167169

lib/github/app.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package github
22

33
import (
4+
"context"
5+
"fmt"
46
"io"
57
"net/http"
68
"time"
@@ -13,7 +15,7 @@ import (
1315

1416
// NewClientWithAppAuth creates a new client that authenticates using an app integration ID
1517
// and a app private key
16-
func NewClientWithAppAuth(integrationID int64, privateKey string, writer io.Writer) (*Client, error) {
18+
func NewClientWithAppAuth(integrationID int64, privateKey, organization string, writer io.Writer) (*Client, error) {
1719
config := new(githubapp.Config)
1820
config.App.IntegrationID = integrationID
1921
config.App.PrivateKey = privateKey
@@ -23,20 +25,29 @@ func NewClientWithAppAuth(integrationID int64, privateKey string, writer io.Writ
2325
cc, err := githubapp.NewDefaultCachingClientCreator(
2426
*config,
2527
githubapp.WithClientUserAgent("tabia"),
26-
githubapp.WithClientTimeout(3*time.Second),
28+
githubapp.WithClientTimeout(10*time.Second),
2729
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
2830
githubapp.WithClientMiddleware(ClientLogging(writer)),
2931
)
3032

31-
client, err := cc.NewAppV4Client()
33+
appClient, err := cc.NewAppClient()
3234
if err != nil {
3335
return nil, err
3436
}
35-
restClient, err := cc.NewAppClient()
37+
installation, _, err := appClient.Apps.FindOrganizationInstallation(context.Background(), organization)
3638
if err != nil {
3739
return nil, err
3840
}
41+
fmt.Println(installation)
3942

43+
client, err := cc.NewInstallationV4Client(*installation.ID)
44+
if err != nil {
45+
return nil, err
46+
}
47+
restClient, err := cc.NewInstallationClient(*installation.ID)
48+
if err != nil {
49+
return nil, err
50+
}
4051
return &Client{nil, restClient, client}, nil
4152
}
4253

0 commit comments

Comments
 (0)