Skip to content

Instantly share code, notes, and snippets.

@coilysiren
Last active April 28, 2018 04:39
Show Gist options
  • Save coilysiren/7c16ff304ae896ac5da7bd2635359221 to your computer and use it in GitHub Desktop.
Save coilysiren/7c16ff304ae896ac5da7bd2635359221 to your computer and use it in GitHub Desktop.
travis ci pull request cleanup bot script
sudo: required # possibly not needed, I haven't tested it
before_script:
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] || bin/pr-cleanup-script.sh' # dont run on both PR and branch builds
script:
- make lint # fail the build if cleanup scripts detect changes
- make test # or whatever your test script is

travic ci pull request cleanup bot script

travis ci pull request cleanup bot script, for when you have cleanup scripts (ex make clean) that you run on pull requests.

don't make developers run cleanup scripts manually! use a bot for it!

currently powering running on callisto's callisto-bot

requirements

what it does, in words

  • on every pull request build
  • run a cleanup script
  • and if there's any changes
  • push them back to the branch for that pull request
# nodejs and python examples, you'll surely have your own linter preferences though
clean:
# npx tslint -p . --fix --force
# black .
lint:
# npx tslint -p .
# black --check .
#!/bin/sh
# do main work
make clean
# git setup
git remote rm origin # travis ci starts with an origin, but its readonly
git remote add origin https://${GITHUB_API_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git # new origin, with push access!
git config --global user.email "example@example.com"
git config --global user.name "example"
# push to repo if there's changes
git status -s # for debugging, show changes if they exist
# consider also `git diff-index` or `git diff-files` in the next line
# but beware, (at time of writing) the repo travis is works off of is in a detached head state
# so those alternate git commands may not work exactly as you'd expect
if [ "$(git status -s)" ]; then
echo "tree is dirty, pushing cleaned files"
git commit . --message "$ make clean {build: $TRAVIS_BUILD_NUMBER}" # the build number in the commit message is just a personal touch
git push origin HEAD:${TRAVIS_PULL_REQUEST_BRANCH}
else
echo "tree is clean"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment