Skip to content

Commit

Permalink
Merge pull request #203 from baijum/story-points
Browse files Browse the repository at this point in the history
Basic implementation of storypoints sync
  • Loading branch information
Zyzyx authored Oct 1, 2024
2 parents db3d7aa + 376d340 commit 40327d3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fedmsg.d/sync2jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'token_auth': 'YOUR_JIRA_ACCESS_TOKEN',
},
},

'default_github_project_fields': {'storypoints': ('Estimate', 'customfield_12310243')},
'map': {
'github': {
'GITHUB_USERNAME/Demo_project': {'project': 'FACTORY', 'component': 'gitbz',
Expand Down
24 changes: 24 additions & 0 deletions sync2jira/downstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,13 @@ def _update_jira_issue(existing, issue, client):
if not updates:
return

# Get fields representing project item fields in GitHub and Jira
github_project_fields = issue.downstream.get('github_project_fields', {})
# Only synchronize comments for listings that op-in
if 'github_project_fields' in updates and len(github_project_fields) > 0:
log.info("Looking for GitHub project fields")
_update_github_project_fields(client, existing, issue, github_project_fields)

# Only synchronize comments for listings that op-in
if 'comments' in updates:
log.info("Looking for new comments")
Expand Down Expand Up @@ -943,6 +950,23 @@ def _update_jira_labels(issue, labels):
issue.update(data)
log.info('Updated %s tag(s)' % len(_labels))

def _update_github_project_fields(client, existing, issue, github_project_fields):
"""Update a Jira issue with GitHub project item field values
:param jira.client.JIRA client: JIRA client
:param jira.resource.Issue existing: Existing JIRA issue
:param sync2jira.intermediary.Issue issue: Upstream issue
:param list: Fields representing GitHub project item fields in GitHub and Jira
"""

for name, values in github_project_fields.items():
_, jirafieldname = values
try:
existing.update({jirafieldname: str(getattr(issue, name))})
except JIRAError:
# Add a comment to indicate there was an issue
client.add_comment(existing, f"Error updating GitHub project field")


def _update_tags(updates, existing, issue):
"""
Expand Down
5 changes: 3 additions & 2 deletions sync2jira/intermediary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Issue(object):

def __init__(self, source, title, url, upstream, comments,
config, tags, fixVersion, priority, content,
reporter, assignee, status, id, upstream_id, downstream=None):
reporter, assignee, status, id, storypoints, upstream_id, downstream=None):
self.source = source
self._title = title[:254]
self.url = url
Expand All @@ -33,6 +33,7 @@ def __init__(self, source, title, url, upstream, comments,
self.tags = tags
self.fixVersion = fixVersion
self.priority = priority
self.storypoints = storypoints

# First trim the size of the content
self.content = trimString(content)
Expand Down Expand Up @@ -63,7 +64,6 @@ def title(self):
def upstream_title(self):
return self._title

@classmethod
def from_github(cls, upstream, issue, config):
"""Helper function to create intermediary object."""
upstream_source = 'github'
Expand Down Expand Up @@ -108,6 +108,7 @@ def from_github(cls, upstream, issue, config):
assignee=issue['assignees'],
status=issue['state'],
id=issue['id'],
storypoints=issue['storypoints'],
upstream_id=issue['number']
)

Expand Down
44 changes: 43 additions & 1 deletion sync2jira/upstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@


log = logging.getLogger('sync2jira')
graphqlurl = 'https://api.github.com/graphql'
project_items_query = '''
query MyQuery(
$orgname: String!, $reponame: String!, $ghfieldname: String!, $issuenumber: Int!
) {
repository(owner: $orgname, name: $reponame) {
issue(number: $issuenumber) {
title
body
projectItems(first: 1) {
nodes {
fieldValueByName(name: $ghfieldname) {
... on ProjectV2ItemFieldNumberValue {
number
}
}
}
}
}
}
}
'''


def handle_github_message(msg, config, pr_filter=True):
Expand Down Expand Up @@ -151,7 +173,6 @@ def handle_github_message(msg, config, pr_filter=True):

return i.Issue.from_github(upstream, msg['msg']['issue'], config)


def github_issues(upstream, config):
"""
Creates a Generator for all GitHub issues in upstream repo.
Expand Down Expand Up @@ -238,6 +259,27 @@ def github_issues(upstream, config):
if issue.get('milestone', None):
issue['milestone'] = issue['milestone']['title']

if not issue.get('storypoints', None):
issue['storypoints'] = ''
orgname, reponame = upstream.rsplit('/', 1)
issuenumber = issue['number']
default_github_project_fields = config['sync2jira']['default_github_project_fields']
project_github_project_fields = config['sync2jira']['map']['github'][upstream]['github_project_fields']
github_project_fields = default_github_project_fields | project_github_project_fields
variables = {"orgname": orgname, "reponame": reponame, "issuenumber": issuenumber}
for fieldname, values in github_project_fields.items():
ghfieldname, _ = values
variables['ghfieldname'] = ghfieldname
response = requests.post(graphqlurl, headers=headers, json={"query": project_items_query, "variables": variables})
data = response.json()
if fieldname == 'storypoints':
try:
issue[fieldname] = data['data']['repository']['issue']['projectItems']['nodes'][0]['fieldValueByName']['number']
except (TypeError, KeyError) as err:
log.debug("Error fetching %s!r from GitHub %s/%s#%s: %s",
ghfieldname, orgname, reponame, issuenumber, err)
continue

final_issues.append(issue)

final_issues = list((
Expand Down

0 comments on commit 40327d3

Please sign in to comment.