Skip to content

Instantly share code, notes, and snippets.

@andrewchilds andrewchilds/clubhouse.sh
Last active May 4, 2019

Embed
What would you like to do?
Clubhouse/git/deploy utility functions
# Clubhouse / git / deploy utility functions
#
# API docs: https://clubhouse.io/api
#
# Assuming the following:
# 1. We have a range of git commits that represent the changes being deployed
# 2. Our git branches and pull requests use the username/ch123/story-name format,
# so that "ch123" ends up in a commit message
# 3. We have a Clubhouse API token set to the $CLUBHOUSE_API_TOKEN environment variable
#
# Then we can get a list of Clubhouse stories related to this deploy and update them
# in some way. In this case, we're moving the stories to a "Deployed" workflow state,
# but we could make any number of updates using the Clubhouse API.
#
# Example usage:
#
# START_SHA="87d4fed" # Previous deploy commit
# END_SHA="2d5b22d" # New deploy commit
#
# git log $START_SHA..$END_SHA --oneline
# > 2d5b22d Merge pull request #123 from clubhouse/ac/ch456/my-story-name
# > e730351 Fix bug
#
# # Move stories to deployed:
#
# clubhouse_move_stories_to_deployed $START_SHA $END_SHA
# > Moving Story 456 to Deployed.
#
# # Move stories to deployed and post a comment:
#
# clubhouse_move_stories_to_deployed $START_SHA $END_SHA "This story was deployed as part of $DEPLOY_ID."
# > Moving Story 456 to Deployed.
DEPLOYED_STATE_ID="123"
function clubhouse_find_stories_in_commit_range {
START_SHA="$1"
END_SHA="$2"
git log $START_SHA..$END_SHA --oneline | grep -io 'ch[0-9]*' | sed 's/[^0-9]*//g' | uniq
}
function clubhouse_move_story_to_workflow_state {
STORY_ID="$1"
WORKFLOW_STATE_ID="$2"
curl -X PUT \
-H "Content-Type: application/json" \
-d "{ \"workflow_state_id\": $WORKFLOW_STATE_ID }" \
-L "https://api.clubhouse.io/api/v1/stories/$STORY_ID?token=$CLUBHOUSE_API_TOKEN"
}
function clubhouse_add_comment_to_story {
STORY_ID="$1"
COMMENT_TEXT="$2"
curl -X POST \
-H "Content-Type: application/json" \
-d "{ \"text\": \"$COMMENT_TEXT\" }" \
-L "https://api.clubhouse.io/api/v1/stories/$STORY_ID/comments?token=$CLUBHOUSE_API_TOKEN"
}
function clubhouse_move_stories_to_deployed {
START_SHA="$1"
END_SHA="$2"
COMMENT_TEXT="$3"
for STORY_ID in $(clubhouse_find_stories_in_commit_range $START_SHA $END_SHA); do
if [ -n "$STORY_ID" ]; then
echo "Moving Story $STORY_ID to Deployed."
clubhouse_move_story_to_workflow_state $STORY_ID $DEPLOYED_STATE_ID
# And if you want to post a comment with more information:
if [ -n "$COMMENT_TEXT" ]; then
clubhouse_add_comment_to_story $STORY_ID "$COMMENT_TEXT"
fi
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.