CI: add the grabpl build-backend command into the repo (#52673)
* Move the grabpl build-backend command and clean it up a bit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
// Package droneutil provides utility functions for working with Drone.
|
||||
package droneutil
|
||||
@@ -0,0 +1,34 @@
|
||||
package droneutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Lookup is the equivalent of os.LookupEnv, but also accepts a list of strings rather than only checking os.Environ()
|
||||
func Lookup(values []string, val string) (string, bool) {
|
||||
for _, v := range values {
|
||||
prefix := val + "="
|
||||
if strings.HasPrefix(v, prefix) {
|
||||
return strings.TrimPrefix(v, prefix), true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetDroneEvent looks for the "DRONE_BUILD_EVENT" in the provided env list and returns the value.
|
||||
// if it was not found, then an error is returned.
|
||||
func GetDroneEvent(env []string) (string, error) {
|
||||
event, ok := Lookup(env, "DRONE_BUILD_EVENT")
|
||||
if !ok {
|
||||
return "", fmt.Errorf("failed to get DRONE_BUILD_EVENT environmental variable")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
// GetDroneEventFromEnv returns the value of DRONE_BUILD_EVENT from os.Environ()
|
||||
func GetDroneEventFromEnv() (string, error) {
|
||||
return GetDroneEvent(os.Environ())
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package droneutil_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/build/droneutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetDroneEvent(t *testing.T) {
|
||||
t.Run("Should return the Drone Event", func(t *testing.T) {
|
||||
env := []string{"DRONE_BUILD_EVENT=pull_request"}
|
||||
droneEvent, err := droneutil.GetDroneEvent(env)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, droneEvent, "pull_request")
|
||||
})
|
||||
t.Run("Should return error, Drone Event env var is missing", func(t *testing.T) {
|
||||
droneEvent, err := droneutil.GetDroneEvent([]string{})
|
||||
require.Error(t, err)
|
||||
require.Empty(t, droneEvent)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
env := []string{"", "EXAMPLE_KEY=value", "EXAMPLE_KEY"}
|
||||
t.Run("A valid lookup should return a string and no error", func(t *testing.T) {
|
||||
val, ok := droneutil.Lookup(env, "EXAMPLE_KEY")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, val, "value")
|
||||
})
|
||||
|
||||
t.Run("An invalid lookup should return an error", func(t *testing.T) {
|
||||
_, ok := droneutil.Lookup(env, "EXAMPLE_KEY_DOES_NOT_EXIST")
|
||||
require.False(t, ok)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user