@@ -23,11 +23,12 @@ import (
"github.com/drone/drone/core"

lru "github.com/hashicorp/golang-lru"
"github.com/sirupsen/logrus"
)

// cache key pattern used in the cache, comprised of the
// repository slug and commit sha.
const keyf = "%d/%s"
const keyf = "%d|%s|%s|%s|%s|%s"

// Memoize caches the conversion results for subsequent calls.
// This micro-optimization is intended for multi-pipeline
@@ -36,7 +37,7 @@ const keyf = "%d/%s"
func Memoize(base core.ConvertService) core.ConvertService {
// simple cache prevents the same yaml file from being
// requested multiple times in a short period.
cache, _ := lru.New(25)
cache, _ := lru.New(10)
return &memoize{base: base, cache: cache}
}

@@ -53,21 +54,33 @@ func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Con
}

// generate the key used to cache the converted file.
key := fmt.Sprintf(keyf, req.Repo.ID, req.Build.After)
key := fmt.Sprintf(keyf,
req.Repo.ID,
req.Build.Event,
req.Build.Action,
req.Build.Ref,
req.Build.After,
req.Repo.Config,
)

// some source control management systems (gogs) do not provide
// the commit sha for tag webhooks. If no commit sha is available
// the ref is used.
if req.Build.After == "" {
key = fmt.Sprintf(keyf, req.Repo.ID, req.Build.Ref)
}
logger := logrus.WithField("repo", req.Repo.Slug).
WithField("build", req.Build.Event).
WithField("action", req.Build.Action).
WithField("ref", req.Build.Ref).
WithField("rev", req.Build.After).
WithField("config", req.Repo.Config)

logger.Trace("extension: conversion: check cache")

// check the cache for the file and return if exists.
cached, ok := c.cache.Get(key)
if ok {
logger.Trace("extension: conversion: cache hit")
return cached.(*core.Config), nil
}

logger.Trace("extension: conversion: cache miss")

// else convert the configuration file.
config, err := c.base.Convert(ctx, req)
if err != nil {
@@ -82,7 +95,11 @@ func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Con
}

// if the configuration file was converted
// it is temporarily cached.
c.cache.Add(key, config)
// it is temporarily cached. Note that we do
// not cache if the commit sha is empty (gogs).
if req.Build.After != "" {
c.cache.Add(key, config)
}

return config, nil
}
@@ -0,0 +1,66 @@
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validator

import (
"context"
"path/filepath"

"github.com/drone/drone/core"
)

// Filter returns a validation service that skips
// pipelines that do not match the filter criteria.
func Filter(include, exclude []string) core.ValidateService {
return &filter{
include: include,
exclude: exclude,
}
}

type filter struct {
include []string
exclude []string
}

func (f *filter) Validate(ctx context.Context, in *core.ValidateArgs) error {
if len(f.include) > 0 {
for _, pattern := range f.include {
ok, _ := filepath.Match(pattern, in.Repo.Slug)
if ok {
return nil
}
}

// if the include list is specified, and the
// repository does not match any patterns in
// the include list, it should be skipped.
return core.ErrValidatorSkip
}

if len(f.exclude) > 0 {
for _, pattern := range f.exclude {
ok, _ := filepath.Match(pattern, in.Repo.Slug)
if ok {
// if the exclude list is specified, and
// the repository matches a pattern in the
// exclude list, it should be skipped.
return core.ErrValidatorSkip
}
}
}

return nil
}
@@ -0,0 +1,70 @@
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validator

import (
"testing"

"github.com/drone/drone/core"
)

func TestFilter_None(t *testing.T) {
f := Filter(nil, nil)
if err := f.Validate(noContext, nil); err != nil {
t.Error(err)
}
}

func TestFilter_Include(t *testing.T) {
args := &core.ValidateArgs{
Repo: &core.Repository{Slug: "octocat/hello-world"},
}

f := Filter([]string{"octocat/hello-world"}, nil)
if err := f.Validate(noContext, args); err != nil {
t.Error(err)
}

f = Filter([]string{"octocat/*"}, nil)
if err := f.Validate(noContext, args); err != nil {
t.Error(err)
}

f = Filter([]string{"spaceghost/*"}, nil)
if err := f.Validate(noContext, args); err != core.ErrValidatorSkip {
t.Errorf("Expect ErrValidatorSkip, got %s", err)
}
}

func TestFilter_Exclude(t *testing.T) {
args := &core.ValidateArgs{
Repo: &core.Repository{Slug: "octocat/hello-world"},
}

f := Filter(nil, []string{"octocat/hello-world"})
if err := f.Validate(noContext, args); err != core.ErrValidatorSkip {
t.Errorf("Expect ErrValidatorSkip, got %s", err)
}

f = Filter(nil, []string{"octocat/*"})
if err := f.Validate(noContext, args); err != core.ErrValidatorSkip {
t.Errorf("Expect ErrValidatorSkip, got %s", err)
}

f = Filter(nil, []string{"spaceghost/*"})
if err := f.Validate(noContext, args); err != nil {
t.Error(err)
}
}