Skip to content

Commit

Permalink
using od_advanced
Browse files Browse the repository at this point in the history
  • Loading branch information
henrychan-opendoor committed Oct 24, 2023
1 parent 6b19cc8 commit 9c49547
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 77 deletions.
8 changes: 6 additions & 2 deletions buildAndPushDockerhub.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
export PUSH_FLAG="--push"
export BUILD_FLAG="buildx build --platform linux/amd64"
#export TAG=2.0.0
export TAG=dev
export IMAGE=opendoor/telia-oss-github-pr-resource
if [ "$#" -gt 0 ]
then
PUSH_FLAG=""
BUILD_FLAG="build"
echo To run locally
echo Sample request.in.json and sample.check.json in e2e-opendoor
echo "docker run -it --entrypoint=/bin/sh opendoor/telia-oss-github-pr-resource:dev"
echo "docker run -it --entrypoint=/bin/sh $IMAGE:$TAG"
echo "cd /opt/resources"
echo "cat <request.in.json|request.check.json> | ./in .| ./out .| ./check"
fi
# docker login --username=$DOCKER_USERNAME --password=$DOCKER_PASSWORD
docker $BUILD_FLAG -t opendoor/telia-oss-github-pr-resource:dev . $PUSH_FLAG
docker $BUILD_FLAG -t $IMAGE:$TAG . $PUSH_FLAG
echo Built $IMAGE:$TAG
4 changes: 2 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// print the request json coming in to "in|out|check"
func PrintDebugInput(s Source, obj any) {
if s.Debug {
if s.OdAdvanced.Debug {
jsonBytes, _ := json.Marshal(obj)
log.Printf("input jsonStr : %s\n", string(jsonBytes))
log.Printf("Debig Tip1: run this docker image locally: docker run -it --entrypoint=/bin/sh opendoor/telia-oss-github-pr-resource:dev\n")
Expand All @@ -20,7 +20,7 @@ func PrintDebugInput(s Source, obj any) {
}

func PrintDebugOutput(s Source, obj any) {
if s.Debug {
if s.OdAdvanced.Debug {
jsonBytes, _ := json.Marshal(obj)
log.Printf("output jsonStr : %s\n", string(jsonBytes))
}
Expand Down
42 changes: 20 additions & 22 deletions e2e-opendoor/pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
################
# YAML anchors #
################
opendoor_yaml_definitions:
access_token_additional: &access_token_additional
- ((github-token-from-app-1))
- ((github-token-from-app-2))
- ((github-token-from-app-3))
- ((github-token-from-app-4))
- ((github-token-from-app-5))
- ((github-token-from-app-6))
- ((github-token-from-app-7))
- ((github-token-from-app-8))
- ((github-token-from-app-9))
- ((github-token-from-app-10))
opendoor_yaml_definitions:
od_advanced: &od_advanced
access_token_additional:
- ((github-token-from-app-1))
- ((github-token-from-app-2))
- ((github-token-from-app-3))
- ((github-token-from-app-4))
- ((github-token-from-app-5))
- ((github-token-from-app-6))
- ((github-token-from-app-7))
- ((github-token-from-app-8))
- ((github-token-from-app-9))
- ((github-token-from-app-10))
min_remaining_threshold_before_using_access_token_additional : 20000
# WIP metrics ... placeholder for now
datadog_api_key: ((datadog-api-key))
datadog_app_key: ((datadog-app-key))
debug: true
image_resource: &dev-tools
name: ""
source:
Expand All @@ -31,7 +37,7 @@ resource_types:
repository: opendoor/telia-oss-github-pr-resource
# stored in vault concourse/shared
# most likely 'dev' if you are doing a code review ;)
tag: ((od-github-pr-resource-tag))
tag: dev

###############
## Resources ##
Expand All @@ -41,16 +47,8 @@ resources:
type: od-pull-request
source:
access_token: ((github-token))
access_token_additional: *access_token_additional
# for testing purposes ... realistically, we should never set min_remaining_threshold_before_using_access_token_additional
# and use the default defined in git.go
min_remaining_threshold_before_using_access_token_additional : 20000
# WIP metrics ... placeholder for now
datadog_api_key: ((datadog-api-key))
datadog_app_key: ((datadog-app-key))
base_branch: master
repository: opendoor-labs/code
debug: true
od_advanced: *od_advanced

##########
## Jobs ##
Expand Down
2 changes: 2 additions & 0 deletions e2e-opendoor/request.README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
note that the ghs_XXXX tokens expire every now ... if you see this in the git repo
its not a security issue
14 changes: 7 additions & 7 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func NewGitClient(source *Source, dir string, output io.Writer) (*GitClient, err
}
return &GitClient{
AccessToken: &source.AccessToken,
AccessTokenAdditional: source.AccessTokenAdditional,
MinRemainingThresholdBeforeUsingAccessTokenAdditional: source.MinRemainingThresholdBeforeUsingAccessTokenAdditional,
DataDogApiKey: source.DataDogApiKey,
DataDogAppKey: source.DataDogAppKey,
DataDogMetricName: source.DataDogMetricName,
DataDogResourcesName: source.DataDogResourcesName,
DataDogResourcesValue: source.DataDogResourcesValue,
AccessTokenAdditional: source.OdAdvanced.AccessTokenAdditional,
MinRemainingThresholdBeforeUsingAccessTokenAdditional: source.OdAdvanced.MinRemainingThresholdBeforeUsingAccessTokenAdditional,
DataDogApiKey: source.OdAdvanced.DataDogApiKey,
DataDogAppKey: source.OdAdvanced.DataDogAppKey,
DataDogMetricName: source.OdAdvanced.DataDogMetricName,
DataDogResourcesName: source.OdAdvanced.DataDogResourcesName,
DataDogResourcesValue: source.OdAdvanced.DataDogResourcesValue,
Directory: dir,
Output: output,
}, nil
Expand Down
38 changes: 19 additions & 19 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ func NewGithubClient(s *Source) (*GithubClient, error) {
minRemaining = graphqlRemaining
}
var minRemainingThresholdBeforeUsingAccessTokenAdditional = DefaultMinRemainingBeforeUsingAccessTokenAdditional
if s.MinRemainingThresholdBeforeUsingAccessTokenAdditional == 0 {
if s.OdAdvanced.MinRemainingThresholdBeforeUsingAccessTokenAdditional == 0 {
log.Printf("source.min_remaining_threshold_before_using_access_token_additional was not supplied in pipeline ... "+
"using DefaultMinRemainingBeforeUsingAccessTokenAdditional : %d\n", DefaultMinRemainingBeforeUsingAccessTokenAdditional)
} else {
log.Printf("using source.min_remaining_threshold_before_using_access_token_additional : %d\n", s.MinRemainingThresholdBeforeUsingAccessTokenAdditional)
minRemainingThresholdBeforeUsingAccessTokenAdditional = s.MinRemainingThresholdBeforeUsingAccessTokenAdditional
log.Printf("using source.min_remaining_threshold_before_using_access_token_additional : %d\n", s.OdAdvanced.MinRemainingThresholdBeforeUsingAccessTokenAdditional)
minRemainingThresholdBeforeUsingAccessTokenAdditional = s.OdAdvanced.MinRemainingThresholdBeforeUsingAccessTokenAdditional
}
if s.AccessTokenAdditional == nil {
if s.OdAdvanced.AccessTokenAdditional == nil {
log.Printf("No AccessTokenAdditional, therefore will ALWAYS use the AccessToken supplied\n")
} else {
log.Printf("Detected that the length of AccessTokenAdditional list is %d\n",
len(s.AccessTokenAdditional))
len(s.OdAdvanced.AccessTokenAdditional))
log.Printf("minRemaining : %d, minRemainingThresholdBeforeUsingAccessTokenAdditional : %d\n",
minRemaining, minRemainingThresholdBeforeUsingAccessTokenAdditional)
if minRemaining < minRemainingThresholdBeforeUsingAccessTokenAdditional {
Expand All @@ -94,7 +94,7 @@ func NewGithubClient(s *Source) (*GithubClient, error) {
// TODO altho we are passing a list of AccessTokenAdditional, we will only consider the first element as it is already sorted
// by highest remaining ... in the future consider the rest of the list, altho this TODO is a low priority
log.Printf("old AccessToken : %s_REDACTED\n", s.AccessToken[0:10])
s.AccessToken = s.AccessTokenAdditional[0]
s.AccessToken = s.OdAdvanced.AccessTokenAdditional[0]
log.Printf("new AccessToken : %s_REDACTED\n", s.AccessToken[0:10])
PrintCurrentRateLimit(*s)
sendToDataDog(s)
Expand Down Expand Up @@ -160,23 +160,23 @@ func NewGithubClient(s *Source) (*GithubClient, error) {

// sending metrics to datadog
func sendToDataDog(s *Source) {
if (s.DataDogApiKey != "") && (s.DataDogAppKey != "") {
if (s.OdAdvanced.DataDogApiKey != "") && (s.OdAdvanced.DataDogAppKey != "") {
log.Printf("DataDogApiKey and DataDogAppKey were supplied\n")
if s.DataDogMetricName == "" {
s.DataDogMetricName = DefaultDataDogMetricName
if s.OdAdvanced.DataDogMetricName == "" {
s.OdAdvanced.DataDogMetricName = DefaultDataDogMetricName
}
if s.DataDogResourcesName == "" {
s.DataDogResourcesName = DefaultDataDogResourcesName
if s.OdAdvanced.DataDogResourcesName == "" {
s.OdAdvanced.DataDogResourcesName = DefaultDataDogResourcesName
}
if s.DataDogResourcesValue == "" {
s.DataDogResourcesValue = DefaultDataDogResourcesValue
if s.OdAdvanced.DataDogResourcesValue == "" {
s.OdAdvanced.DataDogResourcesValue = DefaultDataDogResourcesValue
}
log.Printf("DataDogMetricName : %s, DataDogResourcesName : %s, DataDogResourcesValue : %s\n", s.DataDogMetricName, s.DataDogResourcesName, s.DataDogResourcesValue)
log.Printf("DataDogMetricName : %s, DataDogResourcesName : %s, DataDogResourcesValue : %s\n", s.OdAdvanced.DataDogMetricName, s.OdAdvanced.DataDogResourcesName, s.OdAdvanced.DataDogResourcesValue)
// code borrowed from: https://docs.datadoghq.com/api/latest/metrics/?code-lang=go
body := datadogV2.MetricPayload{
Series: []datadogV2.MetricSeries{
{
Metric: s.DataDogMetricName,
Metric: s.OdAdvanced.DataDogMetricName,
Type: datadogV2.METRICINTAKETYPE_UNSPECIFIED.Ptr(),
Points: []datadogV2.MetricPoint{
{
Expand All @@ -186,8 +186,8 @@ func sendToDataDog(s *Source) {
},
Resources: []datadogV2.MetricResource{
{
Name: datadog.PtrString(s.DataDogResourcesName),
Type: datadog.PtrString(s.DataDogResourcesValue),
Name: datadog.PtrString(s.OdAdvanced.DataDogResourcesName),
Type: datadog.PtrString(s.OdAdvanced.DataDogResourcesValue),
},
},
},
Expand All @@ -198,10 +198,10 @@ func sendToDataDog(s *Source) {
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: s.DataDogApiKey,
Key: s.OdAdvanced.DataDogApiKey,
},
"appKeyAuth": {
Key: s.DataDogAppKey,
Key: s.OdAdvanced.DataDogAppKey,
},
},
)
Expand Down
54 changes: 29 additions & 25 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@ type StatusFilter struct {

// Source represents the configuration for the resource.
type Source struct {
Repository string `json:"repository"`
AccessToken string `json:"access_token"`
AccessTokenAdditional []string `json:"access_token_additional"`
MinRemainingThresholdBeforeUsingAccessTokenAdditional int `json:"min_remaining_threshold_before_using_access_token_additional"`
DataDogApiKey string `json:"datadog_api_key"`
DataDogAppKey string `json:"datadog_app_key"`
DataDogMetricName string `json:"datadog_metric_name"`
DataDogResourcesName string `json:"datadog_resources_name"`
DataDogResourcesValue string `json:"datadog_resources_value"`
Debug bool `json:"debug"`
V3Endpoint string `json:"v3_endpoint"`
V4Endpoint string `json:"v4_endpoint"`
Paths []string `json:"paths"`
IgnorePaths []string `json:"ignore_paths"`
DisableCISkip bool `json:"disable_ci_skip"`
DisableGitLFS bool `json:"disable_git_lfs"`
SkipSSLVerification bool `json:"skip_ssl_verification"`
DisableForks bool `json:"disable_forks"`
IgnoreDrafts bool `json:"ignore_drafts"`
GitCryptKey string `json:"git_crypt_key"`
BaseBranch string `json:"base_branch"`
RequiredReviewApprovals int `json:"required_review_approvals"`
Labels []string `json:"labels"`
States []githubv4.PullRequestState `json:"states"`
StatusFilters []StatusFilter `json:"status_filters"`
Repository string `json:"repository"`
AccessToken string `json:"access_token"`
OdAdvanced OdAdvanced `json:"od_advanced"`
V3Endpoint string `json:"v3_endpoint"`
V4Endpoint string `json:"v4_endpoint"`
Paths []string `json:"paths"`
IgnorePaths []string `json:"ignore_paths"`
DisableCISkip bool `json:"disable_ci_skip"`
DisableGitLFS bool `json:"disable_git_lfs"`
SkipSSLVerification bool `json:"skip_ssl_verification"`
DisableForks bool `json:"disable_forks"`
IgnoreDrafts bool `json:"ignore_drafts"`
GitCryptKey string `json:"git_crypt_key"`
BaseBranch string `json:"base_branch"`
RequiredReviewApprovals int `json:"required_review_approvals"`
Labels []string `json:"labels"`
States []githubv4.PullRequestState `json:"states"`
StatusFilters []StatusFilter `json:"status_filters"`
}

type OdAdvanced struct {
AccessTokenAdditional []string `json:"access_token_additional"`
MinRemainingThresholdBeforeUsingAccessTokenAdditional int `json:"min_remaining_threshold_before_using_access_token_additional"`
DataDogApiKey string `json:"datadog_api_key"`
DataDogAppKey string `json:"datadog_app_key"`
DataDogMetricName string `json:"datadog_metric_name"`
DataDogResourcesName string `json:"datadog_resources_name"`
DataDogResourcesValue string `json:"datadog_resources_value"`
Debug bool `json:"debug"`
}

// Validate the source configuration.
Expand Down

0 comments on commit 9c49547

Please sign in to comment.