How to get the commit range of a push/PR event?

How can I get the commit range of a push/PR event? I’d like to use it to do partial testing.

Travis CI has this environment.
TRAVIS_COMMIT_RANGE: The range of commits that were included in the push or pull request. (Note that this is empty for builds triggered by the initial commit of a new branch.)

I have checked the environment reference of Drone CI, but didn’t find a similar env. (http://readme.drone.io/usage/environment-reference/)

There’s DRONE_PREV_COMMIT_SHA in the environment… not sure if you’d be able to use that with a range operator like ${DRONE_PREV_COMMIT_SHA}..${DRONE_COMMIT_SHA} to solve your use case.

Thanks. It works in push request.

However, when you restart a build, ${DRONE_PREV_COMMIT_SHA} will be the same as ${DRONE_COMMIT_SHA}. So it seems that I cannot rebuild this push commit range anymore.

This has been blocking me as well. It is most frustrating in pull requests where I need to know what is different between the branch and master.

My current workaround is to pull the master branch so I can compare what files are different between the current revision and master:

  inspect-changes:
    image: myrepo/git
    commands:
      - git pull origin master
      - FILES_CHANGED=$(git diff --name-only origin/master)

However, once the pull request is merged to master, I have yet to figure out how to get a list of all files changed in the merged commit range.

@jimsheldon in 0.9 there will be two features that should help with your use case

once the pull request is merged to master, I have yet to figure out how to get a list of all files changed in the merged commit range.

We will begin tracking the before and after commit sha that is provided by the github API for push hooks (e.g. merge to master). This would allow you to calculate an accurate diff.

My current workaround is to pull the master branch so I can compare what files are different between the current revision and master:

We have plans to fetch the diff and make this information available for yaml conditions. This would allow you to limit steps and executions based on file changes. See https://github.com/drone/drone/issues/1021

That is great to hear @bradrydzewski!

We ended up writing a small script to query the github api:

if [ $DRONE_BUILD_EVENT == 'pull_request' ]; then
  BASE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/pulls/$DRONE_PULL_REQUEST | jq -r .base.sha)
elif [ $DRONE_BUILD_EVENT == 'push' ]; then
  BASE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/commits/$DRONE_COMMIT_SHA | jq -r .parents[0].sha)
else
  echo "build event $DRONE_BUILD_EVENT not supported" >&2
  exit 1
fi
echo "$BASE..$DRONE_COMMIT_SHA"

We can then invoke it like so:

git --no-pager diff --name-only $(commit_range.sh)

I would love to know of any potential pitfalls for this approach, but for now it works.

1 Like

@jimsheldon What does parents[0].sha represent?
The first commit in the push?