initial commit

This commit is contained in:
Brad Rydzewski
2018-08-08 14:43:53 -07:00
commit 0f4c512f91
17 changed files with 686 additions and 0 deletions

56
posix/clone Executable file
View File

@ -0,0 +1,56 @@
#!/bin/sh
if [[ ! -z "${DRONE_WORKSPACE}" ]]; then
cd ${DRONE_WORKSPACE}
fi
# if the netrc enviornment variables exist, write
# the netrc file.
if [[ ! -z "${DRONE_NETRC_MACHINE}" ]]; then
cat <<EOF > /root/.netrc
machine ${DRONE_NETRC_MACHINE}
login ${DRONE_NETRC_USERNAME}
password ${DRONE_NETRC_PASSWORD}
EOF
fi
# if the ssh_key environment variable exists, write
# the ssh key and add the netrc machine to the
# known hosts file.
if [[ ! -z "${SSH_KEY}" ]]; then
mkdir /root/.ssh
echo -n "$SSH_KEY" > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa
touch /root/.ssh/known_hosts
chmod 600 /root/.ssh/known_hosts
ssh-keyscan -H ${DRONE_NETRC_MACHINE} > /etc/ssh/ssh_known_hosts 2> /dev/null
fi
# configure git global behavior and parameters via the
# following environment variables:
export GIT_AUTHOR_NAME=${DRONE_COMMIT_AUTHOR_NAME=drone}
export GIT_AUTHOR_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL=drone@localhost}
export GIT_COMMITTER_NAME=${GIT_AUTHOR_NAME}
export GIT_COMMITTER_EMAIL=${GIT_AUTHOR_EMAIL}
# GIT_SSL_NO_VERIFY=
# invoke the sub-script based on the drone event type.
# TODO we should ultimately look at the ref, since
# we need something compatible with deployment events.
case $DRONE_BUILD_EVENT in
pull_request)
clone-pull-request
;;
tag)
clone-tag
;;
*)
clone-commit
;;
esac

17
posix/clone-commit Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin ${DRONE_REMOTE_URL}
fi
set -e
set -x
git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:
git checkout ${DRONE_COMMIT_SHA} -b ${DRONE_COMMIT_BRANCH}

20
posix/clone-pull-request Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin ${DRONE_REMOTE_URL}
fi
set -e
set -x
git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:
git checkout ${DRONE_COMMIT_BRANCH}
git fetch origin ${DRONE_COMMIT_REF}:
git rebase ${DRONE_COMMIT_SHA}

17
posix/clone-tag Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi
if [ ! -d .git ]; then
git init
git remote add origin ${DRONE_REMOTE_URL}
fi
set -e
set -x
git fetch ${FLAGS} origin +refs/tags/${DRONE_TAG}:
git checkout -qf FETCH_HEAD

40
posix/fixtures.sh Executable file
View File

@ -0,0 +1,40 @@
#!/bin/sh
# This script creates a git repository and seeds with
# commit history. Used by unit tests.
set -e
set -x
rm -rf /tmp/remote/greeting
mkdir -p /tmp/remote/greeting
pushd /tmp/remote/greeting
git init
echo "hi world" > hello.txt
git add hello.txt
git commit -m "say hi"
git tag v1.0.0
echo "hello world" > hello.txt
git add hello.txt
git commit -m "say hello"
git tag v1.1.0
git checkout -b fr
echo "salut monde" > hello.txt
git add hello.txt
git commit -m "say hello in french"
git tag v2.0.0
echo "bonjour monde" > hello.txt
git add hello.txt
git commit -m "say hello en francais"
git tag v2.1.0
git checkout master
popd
tar -cvf fixtures.tar /tmp/remote/greeting

BIN
posix/fixtures.tar Normal file

Binary file not shown.

1
posix/posix.go Normal file
View File

@ -0,0 +1 @@
package posix

259
posix/posix_test.go Normal file
View File

@ -0,0 +1,259 @@
package posix
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestCommits(t *testing.T) {
remote := "/tmp/remote/greeting"
base, err := ioutil.TempDir("", "test")
if err != nil {
t.Error(err)
return
}
defer os.Remove(base)
for i, test := range tests {
local := filepath.Join(base, fmt.Sprint(i))
err = os.MkdirAll(local, 0777)
if err != nil {
t.Error(err)
return
}
bin, err := filepath.Abs("clone-commit")
if err != nil {
t.Error(err)
return
}
cmd := exec.Command(bin)
cmd.Dir = local
cmd.Env = []string{
fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", test.branch),
fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
fmt.Sprintf("DRONE_WORKSPACE=%s", local),
fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
t.Log(string(out))
return
}
commit, err := getCommit(local)
if err != nil {
t.Error(err)
return
}
branch, err := getBranch(local)
if err != nil {
t.Error(err)
return
}
if want, got := test.commit, commit; got != want {
t.Errorf("Want commit %s, got %s", want, got)
}
if want, got := test.branch, branch; got != want {
t.Errorf("Want branch %s, got %s", want, got)
}
file := filepath.Join(local, test.file)
out, err = ioutil.ReadFile(file)
if err != nil {
t.Error(err)
return
}
if want, got := test.text, string(out); want != got {
t.Errorf("Want file content %q, got %q", want, got)
}
}
}
func TestTags(t *testing.T) {
remote := "/tmp/remote/greeting"
base, err := ioutil.TempDir("", "test")
if err != nil {
t.Error(err)
return
}
defer os.Remove(base)
for i, test := range tests {
local := filepath.Join(base, fmt.Sprint(i))
err = os.MkdirAll(local, 0777)
if err != nil {
t.Error(err)
return
}
bin, err := filepath.Abs("clone-tag")
if err != nil {
t.Error(err)
return
}
cmd := exec.Command(bin)
cmd.Dir = local
cmd.Env = []string{
fmt.Sprintf("DRONE_TAG=%s", test.tag),
fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
fmt.Sprintf("DRONE_WORKSPACE=%s", local),
fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
t.Log(string(out))
return
}
commit, err := getCommit(local)
if err != nil {
t.Error(err)
return
}
if want, got := test.commit, commit; got != want {
t.Errorf("Want commit %s, got %s", want, got)
}
file := filepath.Join(local, test.file)
out, err = ioutil.ReadFile(file)
if err != nil {
t.Error(err)
return
}
if want, got := test.text, string(out); want != got {
t.Errorf("Want file content %q, got %q", want, got)
}
}
}
func TestPullRequest(t *testing.T) {
remote := "https://github.com/octocat/Spoon-Knife.git"
local, err := ioutil.TempDir("", "test")
if err != nil {
t.Error(err)
return
}
defer os.Remove(local)
bin, err := filepath.Abs("clone-pull-request")
if err != nil {
t.Error(err)
return
}
cmd := exec.Command(bin)
cmd.Dir = local
cmd.Env = []string{
fmt.Sprintf("DRONE_COMMIT_REF=%s", "refs/pull/14596/head"),
fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", "master"),
fmt.Sprintf("DRONE_COMMIT_SHA=%s", "26923a8f37933ccc23943de0d4ebd53908268582"),
fmt.Sprintf("DRONE_WORKSPACE=%s", local),
fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
t.Log(string(out))
return
}
commit, err := getCommit(local)
if err != nil {
t.Error(err)
return
}
branch, err := getBranch(local)
if err != nil {
t.Error(err)
return
}
if want, got := "26923a8f37933ccc23943de0d4ebd53908268582", commit; got != want {
t.Errorf("Want commit %s, got %s", want, got)
}
if want, got := "master", branch; got != want {
t.Errorf("Want branch %s, got %s", want, got)
}
file := filepath.Join(local, "directory/file.txt")
out, err = ioutil.ReadFile(file)
if err != nil {
t.Error(err)
return
}
}
func getBranch(path string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = path
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
func getCommit(path string) (string, error) {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = path
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
var tests = []struct {
branch string
commit string
tag string
file string
text string
}{
{
commit: "9cd29dca0a98f76df94d66493ee54788a18190a0",
branch: "master",
tag: "v1.0.0",
file: "hello.txt",
text: "hi world\n",
},
{
commit: "bbdf5d4028a6066431f59fcd8d83afff610a55ae",
branch: "master",
tag: "v1.1.0",
file: "hello.txt",
text: "hello world\n",
},
{
commit: "553af1ca53c9ad54b096d7ff1416f6c4d1e5049f",
branch: "fr",
tag: "v2.0.0",
file: "hello.txt",
text: "salut monde\n",
},
{
commit: "94b4a1710d1581b8b00c5f7b077026eae3c07646",
branch: "fr",
tag: "v2.1.0",
file: "hello.txt",
text: "bonjour monde\n",
},
}