Skip to content

Commit 5dc6af9

Browse files
authored
feat: use gh to derive the namespace (#3)
1 parent ac2199a commit 5dc6af9

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed

pkg/cmd/copykeycloakadminpassword.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github"
89
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/k8s"
910
"github.com/spf13/cobra"
1011
"golang.design/x/clipboard"
11-
"k8s.io/apimachinery/pkg/apis/meta/v1"
12+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
)
1314

1415
// Flags
@@ -27,6 +28,38 @@ var copyKeycloakAdminPasswordCmd = &cobra.Command{
2728
func runCopyKeycloakAdminPassword(cmd *cobra.Command, args []string) {
2829
ctx := context.Background()
2930

31+
if namespace == "" {
32+
cli, err := github.NewGitHubCLI("")
33+
if err != nil {
34+
fmt.Println(err)
35+
os.Exit(1)
36+
}
37+
38+
repo, err := cli.GetCurrentRepository()
39+
if err != nil {
40+
fmt.Println(err)
41+
os.Exit(1)
42+
}
43+
fmt.Printf("Repo: %s", repo)
44+
fmt.Println()
45+
46+
prNumber, err := cli.GetCurrentPullRequest()
47+
if err != nil {
48+
fmt.Println(err)
49+
os.Exit(1)
50+
}
51+
fmt.Printf("Pull request: %d", prNumber)
52+
fmt.Println()
53+
54+
namespace, err = github.DeriveK8sNamespace(repo, prNumber)
55+
if err != nil {
56+
fmt.Println(err)
57+
os.Exit(1)
58+
}
59+
fmt.Printf("Derived namespace: %s", namespace)
60+
fmt.Println()
61+
}
62+
3063
clients, err := k8s.GetClientset()
3164
if err != nil {
3265
fmt.Println(err)
@@ -56,7 +89,7 @@ func runCopyKeycloakAdminPassword(cmd *cobra.Command, args []string) {
5689
}
5790

5891
func init() {
59-
copyKeycloakAdminPasswordCmd.Flags().StringVarP(&namespace, "namespace", "n", "renku", "k8s namespace")
92+
copyKeycloakAdminPasswordCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "k8s namespace")
6093
copyKeycloakAdminPasswordCmd.Flags().StringVar(&secretName, "secret-name", "keycloak-password-secret", "secret name")
6194
copyKeycloakAdminPasswordCmd.Flags().StringVar(&secretKey, "secret-key", "KEYCLOAK_ADMIN_PASSWORD", "secret key")
6295
}

pkg/github/cli.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
type GitHubCLI struct {
9+
gh string
10+
}
11+
12+
func NewGitHubCLI(gh string) (*GitHubCLI, error) {
13+
if gh == "" {
14+
gh = "gh"
15+
}
16+
17+
path, err := exec.LookPath(gh)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
fmt.Printf("Found GitHub CLI: %s", path)
23+
fmt.Println()
24+
return &GitHubCLI{gh: gh}, nil
25+
}
26+
27+
func (cli *GitHubCLI) RunCmd(arg ...string) ([]byte, error) {
28+
cmd := exec.Command(cli.gh, arg...)
29+
return cmd.Output()
30+
}

pkg/github/pullrequest.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package github
2+
3+
import "encoding/json"
4+
5+
func (cli *GitHubCLI) GetCurrentPullRequest() (int, error) {
6+
out, err := cli.RunCmd("pr", "view", "--json", "number")
7+
if err != nil {
8+
return 0, err
9+
}
10+
11+
var res gitHubPRViewOutput
12+
err = json.Unmarshal(out, &res)
13+
if err != nil {
14+
return 0, err
15+
}
16+
17+
return res.Number, nil
18+
}
19+
20+
type gitHubPRViewOutput struct {
21+
Number int `json:"number"`
22+
}

pkg/github/repository.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package github
2+
3+
import "encoding/json"
4+
5+
func (cli *GitHubCLI) GetCurrentRepository() (string, error) {
6+
out, err := cli.RunCmd("repo", "view", "--json", "nameWithOwner")
7+
if err != nil {
8+
return "", err
9+
}
10+
11+
var res gitHubRepoViewOutput
12+
err = json.Unmarshal(out, &res)
13+
if err != nil {
14+
return "", err
15+
}
16+
17+
return res.NameWithOwner, nil
18+
}
19+
20+
type gitHubRepoViewOutput struct {
21+
NameWithOwner string `json:"nameWithOwner"`
22+
}

pkg/github/utils.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// Maps repositories to namespace templates
6+
var repoToNamespaceTemplateMap map[string]string
7+
8+
func DeriveK8sNamespace(repo string, pr int) (string, error) {
9+
tpl, found := repoToNamespaceTemplateMap[repo]
10+
if found {
11+
return fmt.Sprintf(tpl, pr), nil
12+
}
13+
return "", fmt.Errorf("Could not derive namespace from repository: %s", repo)
14+
}
15+
16+
func init() {
17+
initRepoToNamespaceTemplateMap()
18+
}
19+
20+
func initRepoToNamespaceTemplateMap() {
21+
repoToNamespaceTemplateMap = map[string]string{
22+
"SwissDataScienceCenter/renku": "ci-renku-%d",
23+
"SwissDataScienceCenter/renku-data-services": "renku-ci-ds-%d",
24+
"SwissDataScienceCenter/renku-ui": "renku-ci-ui-%d",
25+
}
26+
}

0 commit comments

Comments
 (0)