Skip to content

Commit

Permalink
Adressing error logs (#506)
Browse files Browse the repository at this point in the history
* some fixes

* handling push events with "ghost" SHA

* handling uninstalled orgs

* lowered loglevel of find latest orb version because it's usually rate limit

* extracted delete repos to own  method for clarity
  • Loading branch information
Andrei-Predoiu authored Sep 19, 2024
1 parent fa6c2a4 commit 439865b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 16 deletions.
13 changes: 12 additions & 1 deletion api/config_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *ConfigCheckHandler) Handles() []string {
}

// Handle has ALL the logic! ;)
func (h *ConfigCheckHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
func (h *ConfigCheckHandler) Handle(ctx context.Context, _, _ string, payload []byte) error {
start := time.Now()
var event github.PushEvent
if err := json.Unmarshal(payload, &event); err != nil {
Expand All @@ -58,6 +58,17 @@ func (h *ConfigCheckHandler) Handle(ctx context.Context, eventType, deliveryID s
return err
}

// Sometimes the commit is not found, we don't know why but should exit early
_, _, err = client.Git.GetRef(ctx, Githubinfo.Owner, Githubinfo.RepoName, commitSHA)
if err != nil {
var acceptedError *github.AcceptedError
// AcceptedError is returned when the commit is not "ready"
if !errors.As(err, &acceptedError) {
log.Warn().Err(err).Msg("event.GetAfter() SHA not found")
return nil // we drop the error
}
}

// get content
content, _, err := gh.GetRepoFileBytes(ctx, client, Githubinfo.Owner, Githubinfo.RepoName, ".github/dependabot-circleci.yml", commitSHA)
if err != nil {
Expand Down
31 changes: 23 additions & 8 deletions api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ func controllerHandler(w http.ResponseWriter, _ *http.Request) {
return
}

err = PostJSON(fmt.Sprintf("%s/start", config.EnvVars.WorkerURL), payloadBytes)
resp, err := PostJSON(fmt.Sprintf("%s/start", config.EnvVars.WorkerURL), payloadBytes)
if err != nil {
log.Error().Err(err).Msgf("error triggering worker for org %s", org)
if resp != nil && resp.StatusCode == http.StatusNotAcceptable {
log.Warn().Err(err).Msgf("dependabot-circleci not installed on org %s, cleaning up repos", org)
deleteRepos(&repos)
} else {
log.Error().Err(err).Msgf("error triggering worker for org %s", org)
}
} else {
log.Debug().Msgf("Dependency check has started for org: %s", org)
}
Expand All @@ -76,6 +81,17 @@ func controllerHandler(w http.ResponseWriter, _ *http.Request) {
wg.Wait()
log.Debug().Msg("All workers finished")
}

func deleteRepos(repos *[]db.RepoData) {
for _, repo := range *repos {
err := db.DeleteRepo(repo, context.Background())
if err != nil {
log.Error().Err(err).Msgf("error deleting repo %s from db", repo.Repo)
} else {
log.Info().Msgf("repo %s deleted from db", repo.Repo)
}
}
}
func shouldRun(schedule string) bool {
// check if an update should be run
t := time.Now()
Expand All @@ -93,8 +109,7 @@ func shouldRun(schedule string) bool {
}

// PostJSON posts the structs as json to the specified url
func PostJSON(url string, payload []byte) error {

func PostJSON(url string, payload []byte) (*http.Response, error) {
var myClient *http.Client
clientWithAuth, err := idtoken.NewClient(context.Background(), url)
if err != nil {
Expand All @@ -107,23 +122,23 @@ func PostJSON(url string, payload []byte) error {

req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("unable to create request: %s", err)
return nil, fmt.Errorf("unable to create request: %s", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.AppConfig.HTTP.Token))
r, err := myClient.Do(req)
if err != nil {
return fmt.Errorf("unable to do request: %s", err)
return nil, fmt.Errorf("unable to do request: %s", err)
}
defer r.Body.Close()

// check response code
if r.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(r.Body)
bodyString := string(bodyBytes)
return fmt.Errorf("request failed, expected status: %d got: %d, error message: %s", http.StatusOK, r.StatusCode, bodyString)
return r, fmt.Errorf("request failed, expected status: %d got: %d, error message: %s", http.StatusOK, r.StatusCode, bodyString)
}
return nil
return r, nil
}

func pullRepos() (repos map[string][]db.RepoData, err error) {
Expand Down
9 changes: 8 additions & 1 deletion api/dep_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/BESTSELLER/dependabot-circleci/config"
Expand Down Expand Up @@ -34,13 +35,19 @@ func dependencyHandler(w http.ResponseWriter, r *http.Request) {

client, err := gh.GetSingleOrganizationClient(cc, workerPayload.Org)
if err != nil {
if strings.HasPrefix(err.Error(), "no installation found for") {
log.Warn().Err(err).Msg("Dependency Handler called for an organization that has no installation")
w.WriteHeader(http.StatusNotAcceptable)
_, _ = fmt.Fprintf(w, "no installation found for organization %s", workerPayload.Org)
return
}
http.Error(w, "failed to register organization client", http.StatusInternalServerError)
log.Fatal().Err(err).Msg("failed to register organization client (gh.GetSingleOrganizationClient)")
}

// Release client and do magic in the background
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "Depedency check has started, please check github for incomming pull requests!")
_, _ = fmt.Fprintln(w, "Depedency check has started, please check github for incomming pull requests!")

// do our magic
dependabot.Start(context.Background(), client, workerPayload.Org, workerPayload.Repos)
Expand Down
2 changes: 1 addition & 1 deletion circleci/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func findNewestOrbVersion(currentVersion string, parameters *map[string]*yaml.No
// if requests fails, return current version
orbInfo, err := api.OrbInfo(client, orbName)
if err != nil {
log.Error().Err(err).Msgf("error finding latests orb version failed for orb: %s", orbSplitString[0])
log.Warn().Err(err).Msgf("error finding latests orb version failed for orb: %s", orbSplitString[0])
return orbName, currentTag, currentTag
}
highestVersion, err := version.NewVersion(orbInfo.Orb.HighestVersion)
Expand Down
13 changes: 12 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"

"github.com/BESTSELLER/dependabot-circleci/config"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
Expand Down Expand Up @@ -70,3 +69,15 @@ func GetRepos(ctx context.Context) (repos []RepoData, err error) {
}
return repos, nil
}

func DeleteRepo(repo RepoData, ctx context.Context) error {
db := DBClient()
_, err := db.NewDelete().
Model(&repo).
Where("id = ?", repo.ID).
Exec(ctx)
if err != nil {
return err
}
return nil
}
6 changes: 5 additions & 1 deletion dependabot/dependabot.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ func gatherUpdates(wg *sync.WaitGroup, entityCount *atomic.Int32, ctx context.Co
log.Info().Msgf("Processing: %s", pathInRepo)
// 1. Get directory contents
options := &github.RepositoryContentGetOptions{Ref: repoInfo.targetBranch}
fileContent, directoryContent, _, err := client.Repositories.GetContents(context.Background(), repoInfo.repoOwner, repoInfo.repoName, pathInRepo, options)
fileContent, directoryContent, resp, err := client.Repositories.GetContents(context.Background(), repoInfo.repoOwner, repoInfo.repoName, pathInRepo, options)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Error().Err(err).Msgf("file not found %s", repoInfo.repoName)
return
}
log.Error().Err(err).Msgf("could not parseRepoContent %s", repoInfo.repoName)
return
}
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "2"

services:
worker:
environment:
Expand Down
2 changes: 1 addition & 1 deletion gh/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func CreatePR(ctx context.Context, client *github.Client, repoOwner string, repo
if len(singleReviewers) > 0 || len(teamReviewers) > 0 {
_, _, err = client.PullRequests.RequestReviewers(ctx, repoOwner, repoName, pr.GetNumber(), github.ReviewersRequest{Reviewers: singleReviewers, TeamReviewers: teamReviewers})
if err != nil {
log.Error().Str("repo_name", repoName).Err(err).Msg("Failed to request reviewers")
log.Warn().Str("repo_name", repoName).Err(err).Msg("Failed to request reviewers")
}
}

Expand Down

0 comments on commit 439865b

Please sign in to comment.