|
|
@@ -15,11 +15,13 @@ limitations under the License.
|
|
|
package github
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"context"
|
|
|
"crypto/rsa"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "io"
|
|
|
"net/http"
|
|
|
"time"
|
|
|
|
|
|
@@ -37,11 +39,13 @@ type Generator struct {
|
|
|
}
|
|
|
|
|
|
type Github struct {
|
|
|
- HTTP *http.Client
|
|
|
- Kube client.Client
|
|
|
- Namespace string
|
|
|
- URL string
|
|
|
- InstallTkn string
|
|
|
+ HTTP *http.Client
|
|
|
+ Kube client.Client
|
|
|
+ Namespace string
|
|
|
+ URL string
|
|
|
+ InstallTkn string
|
|
|
+ Repositories []string
|
|
|
+ Permissions map[string]string
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
@@ -80,8 +84,27 @@ func (g *Generator) generate(
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
|
}
|
|
|
+
|
|
|
+ payload := make(map[string]interface{})
|
|
|
+ if gh.Permissions != nil {
|
|
|
+ payload["permissions"] = gh.Permissions
|
|
|
+ }
|
|
|
+ if len(gh.Repositories) > 0 {
|
|
|
+ payload["repositories"] = gh.Repositories
|
|
|
+ }
|
|
|
+
|
|
|
+ var body io.Reader = http.NoBody
|
|
|
+ if len(payload) > 0 {
|
|
|
+ bodyBytes, err := json.Marshal(payload)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error marshaling payload: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ body = bytes.NewReader(bodyBytes)
|
|
|
+ }
|
|
|
+
|
|
|
// Github api expects POST request
|
|
|
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, gh.URL, http.NoBody)
|
|
|
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, gh.URL, body)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
|
}
|
|
|
@@ -120,7 +143,13 @@ func newGHClient(ctx context.Context, k client.Client, n string, hc *http.Client
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf(errParseSpec, err)
|
|
|
}
|
|
|
- gh := &Github{Kube: k, Namespace: n, HTTP: hc}
|
|
|
+ gh := &Github{
|
|
|
+ Kube: k,
|
|
|
+ Namespace: n,
|
|
|
+ HTTP: hc,
|
|
|
+ Repositories: res.Spec.Repositories,
|
|
|
+ Permissions: res.Spec.Permissions,
|
|
|
+ }
|
|
|
|
|
|
ghPath := fmt.Sprintf("/app/installations/%s/access_tokens", res.Spec.InstallID)
|
|
|
gh.URL = defaultGithubAPI + ghPath
|