Skip to content

Commit

Permalink
Merge pull request #128 from devppratik/sop-alerts
Browse files Browse the repository at this point in the history
OSD-16136 : In-built SOP Feature Added
  • Loading branch information
openshift-merge-robot authored May 5, 2023
2 parents 9b246fb + bb2517d commit 89b906c
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 117 deletions.
41 changes: 40 additions & 1 deletion cmd/kite/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/openshift/pagerduty-short-circuiter/cmd/kite/teams"
Expand All @@ -31,7 +32,8 @@ import (
)

var loginArgs struct {
apiKey string
apiKey string
accessToken string
}

var Cmd = &cobra.Command{
Expand All @@ -51,6 +53,12 @@ func init() {
"",
"Access API key/token generated from "+constants.APIKeyURL+"\nUse this option to overwrite the existing API key.",
)
Cmd.Flags().StringVar(
&loginArgs.accessToken,
"access-token",
"",
"GitHub Personal Access Token generated from "+constants.AccessTokenURL+"\nUse this option to overwrite the existing Access Token.",
)
}

// loginHandler handles the login flow into kite.
Expand Down Expand Up @@ -92,6 +100,17 @@ func loginHandler(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
}

// API key is not found in the config file
if len(cfg.AccessToken) == 0 {

// Create a new API key and store it in the config file
err = generateNewAccessToken(cfg)

if err != nil {
return err
}

// Save the key in the config file
err = config.Save(cfg)
Expand Down Expand Up @@ -159,6 +178,26 @@ func generateNewKey(cfg *config.Config) (err error) {
return nil
}

// generateNewKey prompts the user to create a new API key and saves it to the config file.
func generateNewAccessToken(cfg *config.Config) (err error) {
//prompts the user to generate an API Key
fmt.Println("\nIn order to view SOP it is mandatory to provide an GitHub Access Token.\nThe recommended way is to generate a token via: " + constants.AccessTokenURL)

//Takes standard input from the user and stores it in a variable
reader := bufio.NewReader(os.Stdin)

fmt.Print("GitHub Access Token: ")

cfg.AccessToken, err = reader.ReadString('\n')
cfg.AccessToken = strings.TrimSuffix(cfg.AccessToken, "\n")

if err != nil {
return err
}

return nil
}

// Login handles PagerDuty REST API authentication via an user API token.
// Requests that cannot be authenticated will return a `401 Unauthorized` error response.
// It returns the username of the currently logged in user.
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ require (
github.com/PagerDuty/go-pagerduty v1.4.1
github.com/gdamore/tcell/v2 v2.5.3
github.com/golang/mock v1.6.0
github.com/gomarkdown/markdown v0.0.0-20230322041520-c84983bdbf2a
github.com/google/go-github/v50 v50.2.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.27.1
github.com/openshift-online/ocm-cli v0.1.66
github.com/openshift-online/ocm-sdk-go v0.1.334
github.com/rivo/tview v0.0.0-20220916081518-2e69b7385a37
github.com/spf13/cobra v1.6.1
golang.org/x/net v0.8.0
golang.org/x/oauth2 v0.6.0
)

require (
git.sr.ht/~rockorager/tcell-term v0.1.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/creack/pty v1.1.17 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
Expand Down Expand Up @@ -48,11 +54,11 @@ require (
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
24 changes: 17 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/PagerDuty/go-pagerduty v1.4.1 h1:Mid8VpTj6eoECwuJ5xMcnVPxMxYGhZtMkENxYBOnbyk=
github.com/PagerDuty/go-pagerduty v1.4.1/go.mod h1:W5hSIIPrzSgAkNBDiuymWN5g9yQVzimL7BUBL44f3RY=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand All @@ -53,6 +55,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand All @@ -63,6 +66,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down Expand Up @@ -141,6 +146,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/gomarkdown/markdown v0.0.0-20230322041520-c84983bdbf2a h1:AWZzzFrqyjYlRloN6edwTLTUbKxf5flLXNuTBDm3Ews=
github.com/gomarkdown/markdown v0.0.0-20230322041520-c84983bdbf2a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand All @@ -155,6 +162,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v50 v50.2.0 h1:j2FyongEHlO9nxXLc+LP3wuBSVU9mVxfpdYUexMpIfk=
github.com/google/go-github/v50 v50.2.0/go.mod h1:VBY8FB6yPIjrtKhozXv4FQupxKLS6H4m6xFZlT43q8Q=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
Expand Down Expand Up @@ -375,17 +384,12 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -422,7 +426,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -506,6 +511,8 @@ golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -568,6 +575,7 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -674,6 +682,8 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down
48 changes: 41 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ limitations under the License.
package config

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/google/go-github/v50/github"
"github.com/openshift/pagerduty-short-circuiter/pkg/constants"
"golang.org/x/oauth2"
)

// Configuration struct to store user configuration.
type Config struct {
ApiKey string `json:"api_key,omitempty"`
TeamID string `json:"team_id,omitempty"`
Team string `json:"team,omitempty"`
Terminal string `json:"terminal,omitempty"`
ApiKey string `json:"api_key,omitempty"`
AccessToken string `json:"gh_token,omitempty"`
TeamID string `json:"team_id,omitempty"`
Team string `json:"team,omitempty"`
Terminal string `json:"terminal,omitempty"`
}

// Find returns the pdcli configuration filepath.
Expand Down Expand Up @@ -74,6 +77,13 @@ func Save(cfg *Config) error {
return err
}

// Check if the GitHub key is valid
cfg.AccessToken, err = validateGHToken(cfg.AccessToken)

if err != nil {
return err
}

if cfg.TeamID != "" {

// Check if the team ID is valid
Expand All @@ -98,7 +108,7 @@ func Save(cfg *Config) error {
return fmt.Errorf("cannot marshal configuration file: %v", err)
}

err = ioutil.WriteFile(file, data, 0600)
err = os.WriteFile(file, data, 0600)

if err != nil {
return fmt.Errorf("cannot save configuration file '%s': %v", file, err)
Expand All @@ -122,7 +132,7 @@ func Load() (config *Config, err error) {
return nil, err
}

configData, err := ioutil.ReadFile(configFile)
configData, err := os.ReadFile(configFile)

if err != nil {
err = fmt.Errorf("cannot read config file")
Expand All @@ -149,6 +159,12 @@ func Load() (config *Config, err error) {
return nil, err
}

_, err = validateGHToken(config.AccessToken)

if err != nil {
return nil, err
}

return config, nil
}

Expand Down Expand Up @@ -178,3 +194,21 @@ func validateTeamID(teamID string) (string, error) {

return teamID, nil
}

// Validate Access of GH Token to ops-sop
func validateGHToken(ghToken string) (string, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

_, _, err := client.Repositories.Get(ctx, "openshift", "ops-sop")

if err != nil {
return "", err
}
return ghToken, nil
}
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
ConfigFilepath = "kite/config.json"

APIKeyURL = "https://support.pagerduty.com/docs/generating-api-keys#generating-a-personal-rest-api-key"
AccessTokenURL = "https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token"
OcmContainerURL = "https://github.com/openshift/ocm-container"
OcmContainer = "ocm-container"
Shell = "SHELL"
Expand Down
4 changes: 3 additions & 1 deletion pkg/ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (tui *TUI) SetAlertsTableEvents(alerts []pdcli.Alert) {
alertData = pdcli.ParseAlertMetaData(alert)
tui.ClusterName = alert.ClusterName
tui.ClusterID = alert.ClusterID
tui.SOPLink = alert.Sop
break
}
}
Expand All @@ -32,7 +33,8 @@ func (tui *TUI) SetAlertsTableEvents(alerts []pdcli.Alert) {

// Do not prompt for cluster login if there's no cluster ID associated with the alert (v3 clusters)
if tui.ClusterID != "N/A" && tui.ClusterID != "" && alertData != "" {
tui.InitAlertDataSecondaryView()
secondaryWindowText := fmt.Sprintf("Press 'Y' to log into the cluster: %s\nPress 'S' to view the SOP", tui.ClusterName)
tui.SecondaryWindow.SetText(secondaryWindowText).SetTextColor(PromptTextColor)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ func (tui *TUI) setupAlertDetailsPageInput() {
tui.fetchClusterServiceLogs()
}

if event.Rune() == 'S' || event.Rune() == 's' {
ViewAlertSOP(tui, tui.SOPLink)
}

return event
})
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/ui/sop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ui

import (
"fmt"
"strconv"

"github.com/gdamore/tcell/v2"
"github.com/openshift/pagerduty-short-circuiter/pkg/utils"
"github.com/rivo/tview"
)

// App Setup
func ViewAlertSOP(tui *TUI, URL string) {
textView := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetChangedFunc(func() {
tui.App.Draw()
})
utils.FetchHTMLContent(URL, textView)
name := "SOP - " + utils.GetReadmePath(URL)
textView.Highlight("0").SetBorder(true).SetTitle(name)
AddSOPSlide(name, textView, tui)
// Input Handling
textView.SetDoneFunc(func(key tcell.Key) {
currentSelection := textView.GetHighlights()
if len(currentSelection) > 0 {
index, _ := strconv.Atoi(currentSelection[0])
if key == tcell.KeyEnter {
// TODO: Update with the Link
url := textView.GetRegionText(currentSelection[0])
utils.FetchHTMLContent(url, textView)
fmt.Println(url)
}
if key == tcell.KeyTab {
index = (index + 1) % tui.NumLinks
} else if key == tcell.KeyBacktab {
index = (index - 1 + tui.NumLinks) % tui.NumLinks
} else {
return
}
textView.Highlight(strconv.Itoa(index)).ScrollToHighlight()
}
})

}
Loading

0 comments on commit 89b906c

Please sign in to comment.