Browse code

Add Travis CI configuration to validate DCO and gofmt

After each push, Travis CI will trigger, and check two things:

- make sure that each commit in the push has the Docker certificate of origin
- make sure that all .go files changed by this sequence of commits are correctly formatted in the most recent commit

Note: there is one edge case; if you do a git force push, we cannot figure out the actual commits in the force push, and we will just run the checks as if upstream master were the base. Pull requests will always be tested correctly, though.

Docker-DCO-1.0-Signed-off-by: Andrew Page <admwiggin@gmail.com> (github: tianon)

Tianon Gravi authored on 2013/12/05 06:47:37
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,22 @@
0
+# Note: right now we don't use go-specific features of travis.
1
+# Later we might automate "go test" etc. (or do it inside a docker container...?)
2
+
3
+language: go
4
+
5
+go: 1.2
6
+
7
+# Disable the normal go build.
8
+install: true
9
+
10
+before_script:
11
+  - env | sort
12
+  - sudo apt-get update -qq
13
+  - sudo apt-get install -qq python-yaml
14
+  - git remote add upstream git://github.com/dotcloud/docker.git
15
+  - git fetch upstream +refs/heads/master:refs/remotes/upstream/master
16
+
17
+script:
18
+  - hack/travis/dco.py
19
+  - hack/travis/gofmt.py
20
+
21
+# vim:set sw=2 ts=2:
... ...
@@ -2,6 +2,7 @@ Solomon Hykes <solomon@dotcloud.com> (@shykes)
2 2
 Guillaume Charmes <guillaume@dotcloud.com> (@creack)
3 3
 Victor Vieux <victor@dotcloud.com> (@vieux)
4 4
 Michael Crosby <michael@crosbymichael.com> (@crosbymichael)
5
+.travis.yml: Tianon Gravi <admwiggin@gmail.com> (@tianon)
5 6
 api.go: Victor Vieux <victor@dotcloud.com> (@vieux)
6 7
 Dockerfile: Tianon Gravi <admwiggin@gmail.com> (@tianon)
7 8
 Makefile: Tianon Gravi <admwiggin@gmail.com> (@tianon)
8 9
new file mode 100755
... ...
@@ -0,0 +1,41 @@
0
+#!/usr/bin/env python
1
+import re
2
+import subprocess
3
+import yaml
4
+
5
+from env import commit_range
6
+
7
+commit_format = '-%n hash: %h%n author: %aN <%aE>%n message: |%n%w(0,2,2)%B'
8
+
9
+gitlog = subprocess.check_output([
10
+	'git', 'log', '--reverse',
11
+	'--format=format:'+commit_format,
12
+	'..'.join(commit_range), '--',
13
+])
14
+
15
+commits = yaml.load(gitlog)
16
+if not commits:
17
+	exit(0) # what?  how can we have no commits?
18
+
19
+DCO = 'Docker-DCO-1.0-Signed-off-by:'
20
+
21
+p = re.compile(r'^{0} ([^<]+) <([^<>@]+@[^<>]+)> \(github: (\S+)\)$'.format(re.escape(DCO)), re.MULTILINE|re.UNICODE)
22
+
23
+failed_commits = 0
24
+
25
+for commit in commits:
26
+	m = p.search(commit['message'])
27
+	if not m:
28
+		print 'Commit {1} does not have a properly formatted "{0}" marker.'.format(DCO, commit['hash'])
29
+		failed_commits += 1
30
+		continue # print ALL the commits that don't have a proper DCO
31
+	
32
+	(name, email, github) = m.groups()
33
+	
34
+	# TODO verify that "github" is the person who actually made this commit via the GitHub API
35
+
36
+if failed_commits > 0:
37
+	exit(failed_commits)
38
+
39
+print 'All commits have a valid "{0}" marker.'.format(DCO)
40
+exit(0)
0 41
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+import os
1
+import subprocess
2
+
3
+if 'TRAVIS' not in os.environ:
4
+	print 'TRAVIS is not defined; this should run in TRAVIS. Sorry.'
5
+	exit(127)
6
+
7
+if os.environ['TRAVIS_PULL_REQUEST'] != 'false':
8
+	commit_range = [os.environ['TRAVIS_BRANCH'], 'FETCH_HEAD']
9
+else:
10
+	try:
11
+		subprocess.check_call([
12
+			'git', 'log', '-1', '--format=format:',
13
+			os.environ['TRAVIS_COMMIT_RANGE'], '--',
14
+		])
15
+		commit_range = os.environ['TRAVIS_COMMIT_RANGE'].split('...')
16
+		if len(commit_range) == 1: # if it didn't split, it must have been separated by '..' instead
17
+			commit_range = commit_range[0].split('..')
18
+	except subprocess.CalledProcessError:
19
+		print 'TRAVIS_COMMIT_RANGE is invalid. This seems to be a force push. We will just assume it must be against upstream master and compare all commits in between.'
20
+		commit_range = ['upstream/master', 'HEAD']
0 21
new file mode 100755
... ...
@@ -0,0 +1,28 @@
0
+#!/usr/bin/env python
1
+import subprocess
2
+
3
+from env import commit_range
4
+
5
+files = subprocess.check_output([
6
+	'git', 'diff', '--diff-filter=ACMR',
7
+	'--name-only', '...'.join(commit_range), '--',
8
+])
9
+
10
+exit_status = 0
11
+
12
+for filename in files.split('\n'):
13
+	if filename.endswith('.go'):
14
+		try:
15
+			out = subprocess.check_output(['gofmt', '-s', '-l', filename])
16
+			if out != '':
17
+				print out,
18
+				exit_status = 1
19
+		except subprocess.CalledProcessError:
20
+			exit_status = 1
21
+
22
+if exit_status != 0:
23
+	print 'Reformat the files listed above with "gofmt -s -w" and try again.'
24
+	exit(exit_status)
25
+
26
+print 'All files pass gofmt.'
27
+exit(0)