Skip to content

Commit

Permalink
Merge pull request #24 from boringtools/dev-nikhil
Browse files Browse the repository at this point in the history
Added unauthenticated scan feature
  • Loading branch information
c0d3G33k authored Apr 26, 2024
2 parents cf48b00 + 8202c72 commit 5122f61
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ brew install boringtools/tap/git-alerts
go install github.com/boringtools/git-alerts@main
```

Setup GitHub personal access token [(PAT)](https://github.com/boringtools/git-alerts/blob/main/docs/github.md) as the environment variable
Setup GitHub personal access token [(PAT)](https://github.com/boringtools/git-alerts/blob/main/docs/github.md) as the environment variable, without PAT GitHub will only allow `60` request per hour.

```bash
export GITHUB_PAT=YOUR_GITHUB_PAT
Expand Down
9 changes: 6 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/boringtools/git-alerts/logger"
)

var (
Auth bool
)

func GetTime() (date string) {
time := time.Now()
date = time.Format("Mon Jan 2 15:04:05 MST 2006")
Expand All @@ -25,9 +29,8 @@ func StartChecks() {
_, isGitHubPat := os.LookupEnv("GITHUB_PAT")
_, isSlackHook := os.LookupEnv("SLACK_HOOK")

if !isGitHubPat {
logger.LogERR("GITHUB_PAT is not configured in ENV variable")
os.Exit(1)
if isGitHubPat {
Auth = true
}

if os.Getenv("slack") == "true" {
Expand Down
5 changes: 3 additions & 2 deletions gh/get_org_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strconv"

"github.com/boringtools/git-alerts/common"
"github.com/boringtools/git-alerts/config"
"github.com/boringtools/git-alerts/logger"
)
Expand Down Expand Up @@ -32,7 +33,7 @@ func GetUsers() (jsonData []byte) {
parameters := map[string]string{
"per_page": "100",
}
ghResponse, pageLength := GetResponse(url, parameters)
ghResponse, pageLength := GetResponse(url, common.Auth, parameters)

if pageLength == 0 {

Expand All @@ -47,7 +48,7 @@ func GetUsers() (jsonData []byte) {
} else {
for i := 1; i <= pageLength; i++ {
parameters["page"] = strconv.Itoa(i)
ghResponse, _ := GetResponse(url, parameters)
ghResponse, _ := GetResponse(url, common.Auth, parameters)

json.Unmarshal(ghResponse, &user)
allUsers = append(allUsers, user...)
Expand Down
10 changes: 6 additions & 4 deletions gh/get_pat_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/boringtools/git-alerts/common"
"github.com/boringtools/git-alerts/config"
"github.com/boringtools/git-alerts/logger"
)
Expand All @@ -24,7 +25,7 @@ func CheckPatLimit() {
url := config.GhUrls()[1]
parameters := map[string]string{}

ghResponse, _ := GetResponse(url, parameters)
ghResponse, _ := GetResponse(url, common.Auth, parameters)
json.Unmarshal(ghResponse, &limit)

remaining := limit.Rate.Remaining
Expand All @@ -33,12 +34,13 @@ func CheckPatLimit() {
date := time.Format("Mon Jan 2 15:04:05 MST 2006")

if remaining <= 10 {
logger.LogERR("GitHub PAT limit exceeded")
logger.LogERR("Please try once the limit reset")
logger.LogERR("GitHub request limit exceeded")
logger.LogERR("Please try again once the limit reset or try with GitHub PAT")
logger.LogERRP("Remaining request limit : ", remaining)
logger.LogERRP("Reset time : ", date)
os.Exit(1)
}

logger.LogP("Remaining PAT limit : ", remaining)
logger.LogP("Remaining request limit : ", remaining)

}
9 changes: 6 additions & 3 deletions gh/get_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
pageLength int
)

func GetResponse(ghURL string, params map[string]string) ([]byte, int) {
func GetResponse(ghURL string, auth bool, params map[string]string) ([]byte, int) {

parameters := url.Values{}

Expand All @@ -28,14 +28,17 @@ func GetResponse(ghURL string, params map[string]string) ([]byte, int) {
client := &http.Client{}

request, errRequest := http.NewRequest("GET", fullURL, nil)
pat := "token " + os.Getenv("GITHUB_PAT")

if errRequest != nil {
logger.LogERR("GetResponse - Error in making HTTP calls to GitHub")
panic(errRequest)
}

request.Header.Add("Authorization", pat)
if auth {
pat := "token " + os.Getenv("GITHUB_PAT")
request.Header.Add("Authorization", pat)
}

response, errResponse := client.Do(request)

if errResponse != nil {
Expand Down
2 changes: 1 addition & 1 deletion gh/get_users_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetUsersRepos() (jsonData []byte) {
}
for _, value := range rURL {

usersRepo, _ := GetResponse(value.URL, parameters)
usersRepo, _ := GetResponse(value.URL, common.Auth, parameters)

json.Unmarshal(usersRepo, &repos)
allRepos = append(allRepos, repos...)
Expand Down

0 comments on commit 5122f61

Please sign in to comment.