...

Source file src/github.com/palantir/go-githubapp/githubapp/context.go

Documentation: github.com/palantir/go-githubapp/githubapp

     1  // Copyright 2018 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package githubapp
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/google/go-github/v47/github"
    21  	"github.com/rs/zerolog"
    22  )
    23  
    24  const (
    25  	LogKeyEventType       string = "github_event_type"
    26  	LogKeyDeliveryID      string = "github_delivery_id"
    27  	LogKeyRepositoryName  string = "github_repository_name"
    28  	LogKeyRepositoryOwner string = "github_repository_owner"
    29  	LogKeyPRNum           string = "github_pr_num"
    30  	LogKeyInstallationID  string = "github_installation_id"
    31  )
    32  
    33  // PrepareRepoContext adds information about a repository to the logger in a
    34  // context and returns the modified context and logger.
    35  func PrepareRepoContext(ctx context.Context, installationID int64, repo *github.Repository) (context.Context, zerolog.Logger) {
    36  	logctx := zerolog.Ctx(ctx).With()
    37  
    38  	logctx = attachInstallationLogKeys(logctx, installationID)
    39  	logctx = attachRepoLogKeys(logctx, repo)
    40  
    41  	logger := logctx.Logger()
    42  	return logger.WithContext(ctx), logger
    43  }
    44  
    45  // PreparePRContext adds information about a pull request to the logger in a
    46  // context and returns the modified context and logger.
    47  func PreparePRContext(ctx context.Context, installationID int64, repo *github.Repository, number int) (context.Context, zerolog.Logger) {
    48  	logctx := zerolog.Ctx(ctx).With()
    49  
    50  	logctx = attachInstallationLogKeys(logctx, installationID)
    51  	logctx = attachRepoLogKeys(logctx, repo)
    52  	logctx = attachPullRequestLogKeys(logctx, number)
    53  
    54  	logger := logctx.Logger()
    55  	return logger.WithContext(ctx), logger
    56  }
    57  
    58  func attachInstallationLogKeys(logctx zerolog.Context, installID int64) zerolog.Context {
    59  	if installID > 0 {
    60  		return logctx.Int64(LogKeyInstallationID, installID)
    61  	}
    62  	return logctx
    63  }
    64  
    65  func attachRepoLogKeys(logctx zerolog.Context, repo *github.Repository) zerolog.Context {
    66  	if repo != nil {
    67  		return logctx.
    68  			Str(LogKeyRepositoryOwner, repo.GetOwner().GetLogin()).
    69  			Str(LogKeyRepositoryName, repo.GetName())
    70  	}
    71  	return logctx
    72  }
    73  
    74  func attachPullRequestLogKeys(logctx zerolog.Context, number int) zerolog.Context {
    75  	if number > 0 {
    76  		return logctx.Int(LogKeyPRNum, number)
    77  	}
    78  	return logctx
    79  }
    80  

View as plain text