Development · Downloadables · Project · Tutorial

Humanlights.Twitter

A console application of hooking git commits and sending them to Twitter.

Usage

The application is best used with git hooks, it is only created for printing commits, not posting random tweets on Twitter. It uses one of my personal projects which I use as extension apps for Jenkins (for instance) which is Humanlights.GitBatch, which gets the commit informations from a repository folder that usually is called “.git” and stores a json file of it.

Humanlights.GitBatch

In order to make Humanlights.Twitter work, you’ll have to input a json file that is generated by GitBatch. Since both apps are console applications and use command-lines (ran through .bat or .sh files), here is a sample of how to use GitBatch.

Humanlights.GitBatch.exe -git ".git" -json "git_commit.json"

  • -git: The git folder.
  • -json: Where the json file will be exported.
  • -keepTemp: (Optional) Does not remove the files generated thoughout the process.

Humanlights.Twitter

This application uses lots of dll assembly dependencies/references because of the main plugin it uses, Tweetinvi (NuGet) to post stuff to Twitter.

Humanlights.Twitter.exe -credentials "credentials.json" -commit "git_commit.json"

Humanlights.Twitter.exe -credentials "credentials.json" -commit
"git_commit.json" +tweetFormat "[message][RETURN][RETURN][author_name]
on [branch]#[changeset]" +replyFormat "Changes: [changes_long]" +reply

  • -credentials: Credentials json file. You have to make a Twitter application and get consumer key, consumer secret, user access token and user access secret.
  • -commit: Path to commit file generated by Humanlights.GitBatch.
  • +tweetFormat: (Optional) The twitter message format. “[message][RETURN][RETURN][author_name] on [branch]#[changeset]” by default. Which every single [] case is being replaced.
  • +replyFormat: (Optional) The twitter message format. “[changes_long]” by default.
  • +reply: (Optional) Enable posting commit reply. (Only posts attached to the first commit, if multiple lined) Be aware that if using +replyFormat, this needs to be added as well to work.
  • +hiddenCommitPlaceholder: (Optional) Pretty much self explanatory. “This commit has been marked as private, so it is hidden.” by default.

Replacements

  • [RETURN]: It replaces all ‘[RETURN]’ in the tweet or reply format with ‘\n’, basically returns.
  • [TAB]: Adds ‘\t’, basically tabbing.
  • [shortSHA]: Replaces with commit’s short SHA code.
  • [longSHA]: Commit’s long SHA code.
  • [changeset]: Repository’s commit count, basically.
  • [author_name]: Commit’s author’s name.
  • [author_email]: Commit’s author’s email.
  • [date]: Commit date and time.
  • [repository_name]: The repository name took from the origin.
  • [branch]: Commit’s branch.
  • [tag]: Commit’s tag (if any).
  • [message]: Commit’s message.
  • [changes_count]: Commit’s changes count. E.g “50 files”, or “1 file”.
  • [changes_short]: Commit’s changes in short format. E.g “32 add., 5 mod., 1 del.”, or “2 mod., 1 del.”.
  • [changes_long]: Commit’s changes in long format. E.g “4 files added, 1 file modified and 2 deleted”.

Includes

  • [h]: If any commit has “[h]” or “[H]” anywhere in the text message, the commit message will be replaced with hidden commit placeholder.

Instructions

Step 1 – Git Hooks

In order to make this actually work, to combine it all with the git repository, you’ll just have to look at the Example folder and treat the files by just copying them to .git/hooks/ folder in your repository. It is best to use post-commit (or post-push) because the post-commit script will use GitBatch to download the header commit of the repository (which is going to take the new commit — after it has been commited and/or pushed).

You can obviously customize the post-commit script with your default preferences for twitter post and/or reply tweet (if want to add some more info related to the commit in case it doesn’t fit).

The ‘post-commit’ in the Example folder is pretty much quick-start code, does not need any other path adjustments or something. Add it there and commit something. Don’t forget to update the Credentials.json file!

Step 2 – Twitter Credentials

I’ll just make a little tutorial of taking the informations needed for the Credentials.json file (which you’ll have to manually fulfill).

1. Create an application

Go to Twitter Application Management and create a new app. Fill up basic stuff, follow the instructions on that page, etc.

2. Get credentials

Open that Credentials.json file. Go to Keys and Access Tokens and copy-paste Consumer Key (API Key) and put it at — you got it – do as well with Consumer Secret (API Secret). Lower the page, click on Create my access token and get Access Token and place it at userAccessToken as well with the Access Token Secret to userAccessSecret.

Note: Just a friendly note, be sure to remove unnecesarry spaces (if any).

3. Done!

This is pretty much done. You’ve set up Git Hooks, created a Twitter application and got credentials needed and you’re good to commit. Try it out and check out your Twitter profile.

Example

Our Humanlights Bot uses it.

License

See license (hint: MIT).

GitHub