Bookmarklet to generate a commit message with Pivotal Tracker story ID and title

For clarity and traceability, your commit messages should include the ID and title of the Pivotal Tracker story you're working on. For example:

[#12345] Add Google Maps to user profiles
Optional further commit messages in the body

Also see Howto: Write a proper git commit message

To quickly generate such commit messages, add a new link "Commit" to your bookmarks and use the following Javascript as the link URL:

javascript:(function() { 
  let storyTitles = [];
  let stories = Array.from(document.querySelectorAll('.story[data-selected="true"]')).map((element) => element.closest('.story'));
  let collapsed = true;
  
  if (stories.length == 0) { stories = Array.from(document.querySelectorAll('.story .details')).map((element) => element.closest('.story')); collapsed = false; } 
  stories.forEach((story) => {
    let title = (collapsed ? story.querySelector('[data-aid="StoryPreviewItem__title"]').innerText : story.querySelector('[name="story[name]"]').value);
    let id = story.dataset.id;
    if (id && title) { let gitptTitle = '[#' + id + '] ' + title; if (storyTitles.indexOf(gitptTitle) == -1) { storyTitles.push(gitptTitle); } }
   });
   
   if (storyTitles.length > 0) { prompt('Your commit message:', storyTitles.join('; ')); } else { alert("Please select the stories first."); } 
 })();

When you now open one or more stories in Pivotal Tracker and hit the bookmarklet, it will display a prompt with a commit message. You can also select the stories using the blue box next to them.

To add the bookmarklet to Firefox first add a new bookmark then right click the bookmark and open properties. Paste your code snippet in the location field.

We also have a shell script for this.

Henning Koch Over 13 years ago