Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging of commit information #58

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions policy/approval/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"sort"
"strings"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -128,15 +129,20 @@ func (r *Rule) IsApproved(ctx context.Context, prctx pull.Context) (bool, string
if err != nil {
return false, "", err
}

lastCommitTime := commits[len(commits)-1].CreatedAt
lastCommit := commits[len(commits)-1]

var allowedCandidates []*common.Candidate
for _, candidate := range candidates {
if candidate.CreatedAt.After(lastCommitTime) {
if candidate.CreatedAt.After(lastCommit.CreatedAt) {
allowedCandidates = append(allowedCandidates, candidate)
}
}

log.Debug().Msgf("discarded %d candidates invalidated by push of %s at %s",
len(candidates)-len(allowedCandidates),
lastCommit.SHA,
Copy link
Contributor

@jamestoyer jamestoyer Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the full SHA of the commit or just substring of it? If it's the former, do we need to have the full SHA? May places like GitHub use the first 7-8 characters

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the full SHA: it comes from the API, which I believe only returns full values. Since this is for debugging, I don't mind having the full value, even if it makes the message longer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, makes sense in this case

lastCommit.CreatedAt.Format(time.RFC3339))

candidates = allowedCandidates
}

Expand Down
11 changes: 10 additions & 1 deletion server/handler/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,20 @@ func (b *Base) PostStatus(ctx context.Context, client *github.Client, pr *github

func (b *Base) postGitHubRepoStatus(ctx context.Context, client *github.Client, owner, repo, ref string, status *github.RepoStatus) error {
logger := zerolog.Ctx(ctx)
logger.Info().Msgf("Setting status context=%s state=%s description=%s target_url=%s", status.GetContext(), status.GetState(), status.GetDescription(), status.GetTargetURL())
logger.Info().Msgf("Setting %q status on %s to %s: %s", status.GetContext(), ref, status.GetState(), status.GetDescription())
_, _, err := client.Repositories.CreateStatus(ctx, owner, repo, ref, status)
return err
}

func (b *Base) PreparePRContext(ctx context.Context, installationID int64, pr *github.PullRequest) (context.Context, zerolog.Logger) {
ctx, logger := githubapp.PreparePRContext(ctx, installationID, pr.GetBase().GetRepo(), pr.GetNumber())

logger = logger.With().Str("github_sha", pr.GetHead().GetSHA()).Logger()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to put this here instead of in githubapp because there are situations when you might take action in response to a PR event that have nothing to do with the current SHA. It's also unclear if you would always want to log the head SHA.

ctx = logger.WithContext(ctx)

return ctx, logger
}

func (b *Base) Evaluate(ctx context.Context, mbrCtx pull.MembershipContext, client *github.Client, v4client *githubv4.Client, pr *github.PullRequest) error {
fetchedConfig, err := b.ConfigFetcher.ConfigForPR(ctx, client, pr)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions server/handler/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/alexedwards/scs"
"github.com/bluekeyes/templatetree"
"github.com/google/go-github/github"
"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
"goji.io/pat"

Expand Down Expand Up @@ -107,7 +106,7 @@ func (h *Details) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
data.PullRequest = pr
data.User = user

ctx, _ = githubapp.PreparePRContext(ctx, installation.ID, pr.GetBase().GetRepo(), number)
ctx, _ = h.PreparePRContext(ctx, installation.ID, pr)

config, err := h.ConfigFetcher.ConfigForPR(ctx, client, pr)
data.PolicyURL = getPolicyURL(pr, config)
Expand Down
6 changes: 3 additions & 3 deletions server/handler/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ func (h *IssueComment) Handle(ctx context.Context, eventType, deliveryID string,
number := event.GetIssue().GetNumber()
installationID := githubapp.GetInstallationIDFromEvent(&event)

ctx, logger := githubapp.PreparePRContext(ctx, installationID, event.GetRepo(), number)

if !event.GetIssue().IsPullRequest() {
logger.Debug().Msg("Issue comment event is not for a pull request")
zerolog.Ctx(ctx).Debug().Msg("Issue comment event is not for a pull request")
return nil
}

Expand All @@ -68,6 +66,8 @@ func (h *IssueComment) Handle(ctx context.Context, eventType, deliveryID string,
return errors.Wrapf(err, "failed to get pull request %s/%s#%d", repo.GetOwner().GetLogin(), repo.GetName(), number)
}

ctx, logger := h.PreparePRContext(ctx, installationID, pr)

fetchedConfig, err := h.ConfigFetcher.ConfigForPR(ctx, client, pr)
if err != nil {
return errors.Wrap(err, "failed to fetch configuration")
Expand Down
2 changes: 1 addition & 1 deletion server/handler/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (h *PullRequest) Handle(ctx context.Context, eventType, deliveryID string,
return err
}

ctx, _ = githubapp.PreparePRContext(ctx, installationID, event.GetRepo(), event.GetNumber())
ctx, _ = h.PreparePRContext(ctx, installationID, event.GetPullRequest())

switch event.GetAction() {
case "opened", "reopened", "synchronize", "edited":
Expand Down
2 changes: 1 addition & 1 deletion server/handler/pull_request_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (h *PullRequestReview) Handle(ctx context.Context, eventType, deliveryID st
return err
}

ctx, _ = githubapp.PreparePRContext(ctx, installationID, event.GetRepo(), event.GetPullRequest().GetNumber())
ctx, _ = h.PreparePRContext(ctx, installationID, event.GetPullRequest())

mbrCtx := NewCrossOrgMembershipContext(ctx, client, event.GetRepo().GetOwner().GetLogin(), h.Installations, h.ClientCreator)
return h.Evaluate(ctx, mbrCtx, client, v4client, event.GetPullRequest())
Expand Down