1

I want to create a Java application that monitors a GitHub repository, pulls changes to the machine it is running on and triggers a method when something in the repo has changed.

while(true)
{
    if(new commits on the remote github repo)
    {
        pullChanges();
        doSomething();
    }
}

I am currently using JGit and regularly deleting the local repository to clone a new one and compare the combined file size to check if anything has changed. This is incredibly hacky and not reliable (if a commit just changes a letter, the file sizes would be the same)

domisum
  • 531
  • 1
  • 7
  • 12
  • you should be able to get the commit log from the repository using jgit and if the hash changes pull the changes. – Dodge Mar 03 '16 at 15:40
  • [here](https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowLog.java) is a sample for retrieving the commit log – Dodge Mar 03 '16 at 15:41

3 Answers3

1

You can use GitHub Webhooks.

Webhooks allow you to build or set up integrations which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a HTTP POST payload to the webhook's configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You're only limited by your imagination.

ouzman
  • 71
  • 1
  • 4
0

Your method to get a diff between your local copy and the remote is too tricky to be reliable.

See this post: How to check for changes on remote (origin) Git repository?

Community
  • 1
  • 1
Xvolks
  • 2,065
  • 1
  • 21
  • 32
0

I changed it to clone the repository when the java application starts, after that I created a seperate thread that pulls new commits from the repository every 30 seconds and checks if something has changed with the 'FetchResult' returned from the Java method.

These helped me:

Cloning a repo

Pulling commits

domisum
  • 531
  • 1
  • 7
  • 12