Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Last active March 22, 2023 18:29
Show Gist options
  • Save ralphbean/164f31b6eee43bae16215716a0b14735 to your computer and use it in GitHub Desktop.
Save ralphbean/164f31b6eee43bae16215716a0b14735 to your computer and use it in GitHub Desktop.
Move all child stories of epics of a feature up to higher priority (rank) than the highest story in a project

Installation

$ python3 -m venv virtualenv
$ source virtualenv/bin/activate
$ pip install jira
$ deactivate

Usage

$ source virtualenv/bin/activate
$ export JIRA_TOKEN=...
$ python prioritize.py ASC STONE-248
$ deactivate
#!/usr/bin/env python
""" Automatically prioritize JIRA stories attached to a JIRA feature
Problem: If features are prioritized up or down - that ranking doesn't cascade to stories. In order
to plan sprints, you manually have to open tons of tabs, compare the priority of Features, find all
the epics on those features and then find all the stories for your team on those epics - and move
them up in your sprint planning backlog individually. What a pain!
This script attempts to automate that for you.
This script accepts two arguments: a feature id and a project id. All of the stories of all of the
epics of the Feature will be prioritized up higher than the highest ranked story in the given
project.
"""
import argparse
import os
import sys
import jira
def get_args():
"""
Parse args from the command-line.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"project",
help="Name of the project we are prioritizing in",
)
parser.add_argument(
"feature",
help="Key of the feature we are prioritizing to the top",
)
return parser.parse_args()
args = get_args()
url = os.environ.get("JIRA_URL", "https://issues.redhat.com")
token = os.environ.get("JIRA_TOKEN")
if not token:
print("Set JIRA_TOKEN environment variable to your JIRA personal access token")
sys.exit(1)
JIRA = jira.client.JIRA(server=url, token_auth=token)
query = f"key={args.feature} and type=Feature"
print("Confirming the Feature exists:")
print(" > " + query)
results = JIRA.search_issues(query)
if not results:
print(f"Feature not found via query: {query}")
sys.exit(1)
feature = results[0]
query = f'issueFunction in portfolioChildrenOf("key={feature}")'
print("Looking up epics on the feature")
print(" > " + query)
epics = JIRA.search_issues(query)
if not epics:
print("No epics found.")
sys.exit(1)
epic_keys = ",".join([epic.key for epic in epics])
query = f'issueFunction in issuesInEpics("key in ({epic_keys})") and project="{args.project}" and statusCategory != Done'
print(f"Looking up stories in {args.project} on those epics")
print(" > " + query)
stories = JIRA.search_issues(query)
query = f'project="{args.project}" ORDER BY Rank Asc'
print("Finding current highest ranked story in the project")
print(" > " + query)
current_order = JIRA.search_issues(query)
highest = current_order[0]
print()
print(f"Moving {len(stories)} stories higher than {highest.key}")
for story in stories:
if story == highest:
print(f" Ignoring {story.key}. It is already ranked highest.")
continue
print(f" Moving rank of {url}/browse/{story.key} above {highest.key}")
JIRA.rank(issue=story.key, prev_issue=highest.key)
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment