internal/kokoro: add apidiff

Change-Id: Ibb8ec6321921c2decff833a0abb0a535e0f186a1
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/37671
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chris Broadfoot <cbro@google.com>
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b7e5e68..cc1c1d4 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -26,6 +26,10 @@
 1. Keep amending to the change with `git codereview change` and mail as your
 receive feedback. Each new mailed amendment will create a new patch set for
 your change in Gerrit.
+    - Note: if your change includes a breaking change, our breaking change
+    detector will cause CI/CD to fail. If your breaking change is acceptable
+    in some way, add BREAKING_CHANGE_ACCEPTABLE=<reason> to cause the
+    detector not to be run and to make it clear why that is acceptable.
 
 ## Integration Tests
 
diff --git a/go.mod b/go.mod
index 69fccf5..d2d2a3f 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@
 	github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
 	go.opencensus.io v0.19.1
 	golang.org/x/build v0.0.0-20190314133821-5284462c4bec
+	golang.org/x/exp v0.0.0-20190121172915-509febef88a4
 	golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f
 	golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
 	golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6
diff --git a/go.sum b/go.sum
index a173c62..1acb05d 100644
--- a/go.sum
+++ b/go.sum
@@ -85,6 +85,7 @@
 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/exp v0.0.0-20190121172915-509febef88a4 h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
 golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 h1:x/bBzNauLQAlE3fLku/xy92Y8QwKX5HZymrMz2IiKFc=
diff --git a/internal/kokoro/check_incompat_changes.sh b/internal/kokoro/check_incompat_changes.sh
new file mode 100755
index 0000000..8ea3ebd
--- /dev/null
+++ b/internal/kokoro/check_incompat_changes.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+
+# Display commands being run
+set -x
+
+# Only run apidiff checks on go1.12 (we only need it once).
+if [[ `go version` != *"go1.12"* ]]; then
+    exit 0
+fi
+
+if git log -1 | grep BREAKING_CHANGE_ACCEPTABLE; then
+  exit 0
+fi
+
+go install golang.org/x/exp/cmd/apidiff
+
+# We compare against master@HEAD. This is unfortunate in some cases: if you're
+# working on an out-of-date branch, and master gets some new feature (that has
+# nothing to do with your work on your branch), you'll get an error message.
+# Thankfully the fix is quite simple: rebase your branch.
+git clone https://code.googlesource.com/gocloud /tmp/gocloud
+
+V1_DIRS=`find . -type d -regex '.*v1$'`
+V1_SUBDIRS=`find . -type d -regex '.*v1\/.*'`
+for dir in $V1_DIRS $V1_SUBDIRS; do
+  # turns things like ./foo/bar into foo/bar
+  dir_without_junk=`echo $dir | sed -n "s#\(\.\/\)\(.*\)#\2#p"`
+  pkg="cloud.google.com/go/$dir_without_junk"
+  echo "Testing $pkg"
+
+  cd /tmp/gocloud
+  apidiff -w /tmp/pkg.master $pkg
+  cd - > /dev/null
+
+  # TODO(deklerk) there's probably a nicer way to do this that doesn't require
+  # two invocations
+  if ! apidiff -incompatible /tmp/pkg.master $pkg | (! read); then
+    apidiff -incompatible /tmp/pkg.master $pkg
+    exit 1
+  fi
+done
\ No newline at end of file
diff --git a/internal/kokoro/continuous.sh b/internal/kokoro/continuous.sh
index 9cef447..f08881f 100755
--- a/internal/kokoro/continuous.sh
+++ b/internal/kokoro/continuous.sh
@@ -52,6 +52,7 @@
 download_deps
 go install github.com/jstemmer/go-junit-report
 ./internal/kokoro/vet.sh
+./internal/kokoro/check_incompat_changes.sh
 
 mkdir $KOKORO_ARTIFACTS_DIR/tests
 
diff --git a/internal/kokoro/presubmit.sh b/internal/kokoro/presubmit.sh
index 9c3521b..c9d9c3b 100755
--- a/internal/kokoro/presubmit.sh
+++ b/internal/kokoro/presubmit.sh
@@ -42,6 +42,7 @@
 download_deps
 go install github.com/jstemmer/go-junit-report
 ./internal/kokoro/vet.sh
+./internal/kokoro/check_incompat_changes.sh
 
 mkdir $KOKORO_ARTIFACTS_DIR/tests
 
diff --git a/tools.go b/tools.go
index 5c91f8a..fa01cc4 100644
--- a/tools.go
+++ b/tools.go
@@ -26,6 +26,7 @@
 import (
 	_ "github.com/golang/protobuf/protoc-gen-go"
 	_ "github.com/jstemmer/go-junit-report"
+	_ "golang.org/x/exp/cmd/apidiff"
 	_ "golang.org/x/lint/golint"
 	_ "golang.org/x/tools/cmd/goimports"
 	_ "honnef.co/go/tools/cmd/staticcheck"