Skip to content

Commit

Permalink
Merge pull request #4 from demisto/revert-to-0.1.8
Browse files Browse the repository at this point in the history
* Revert to a stable version
* Add support for archived cards
* Ignore closed issues
  • Loading branch information
ronykoz authored Nov 22, 2020
2 parents 644de1b + 86725a1 commit 94ef872
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 316 deletions.
11 changes: 7 additions & 4 deletions src/github_automation/core/issue/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def _extract_project_cards(project_cards):
card_id_project[node['id']] = {
"project_number": node['project']['number']
}
if node['project']['columns']['nodes']:
card_id_project[node['id']]['project_column'] = node['project']['columns']['nodes'][0]['name']
if 'column' in node and node['column'] and 'name' in node['column']:
card_id_project[node['id']]['project_column'] = node['column']['name']

return card_id_project

Expand All @@ -66,16 +66,19 @@ def parse_issue(github_issue):
"pull_request": _get_pull_request(github_issue),
"labels": get_labels(github_issue.get('labels', {}).get('edges', [])),
"milestone": _get_milestone(github_issue),
"card_id_to_project": _extract_project_cards(github_issue.get('projectCards', {}))
"card_id_to_project": _extract_project_cards(github_issue.get('projectCards', {})),
"state": github_issue.get('state', '')
}


class Issue(object):
def __init__(self, id: str, title: str, number: int, assignees: list = None, pull_request: PullRequest = None,
labels: list = None, milestone: str = '', card_id_to_project: dict = None, priority_list: list = None):
labels: list = None, milestone: str = '', card_id_to_project: dict = None, priority_list: list = None,
state: str = ''):
self.id = id
self.title = title
self.number = number
self.state = state

self.assignees = assignees if assignees else []
self.pull_request = pull_request
Expand Down
26 changes: 15 additions & 11 deletions src/github_automation/core/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def add_card(self, card_id, new_issue, client):
exception_msg = str(ex)
if 'The card must not be archived' in exception_msg:
try:
self.config.logger.info(f"Un-archiving the card of {new_issue.title}")
client.un_archive_card(card_id)
client.add_to_column(card_id=card_id,
column_id=self.id)
Expand All @@ -99,6 +100,7 @@ def add_card(self, card_id, new_issue, client):
exception_msg = str(ex)
if 'The card must not be archived' in exception_msg:
try:
self.config.logger.info(f"Un-archiving the card of {new_issue.title}")
client.un_archive_card(card_id)
client.move_to_specific_place_in_column(card_id=card_id,
column_id=self.id,
Expand Down Expand Up @@ -281,21 +283,23 @@ def get_current_location(self, issue_id):

return None, None

def move_issues(self, client, config: Configuration):
def move_issues(self, client, config: Configuration, all_issues):
# todo: add explanation that we are relying on the github automation to move closed issues to the Done queue
for column in self.columns.values():
issues = column.get_issues()
for issue in issues:
column_name_before, card_id = self.get_current_location(issue.id)
column_name_after = self.get_matching_column(issue, config)
column_id = self.columns[column_name_after].id if column_name_after else ''
if not column_id or column_name_before == column_name_after:
continue
for issue in all_issues.values():
column_name_before, card_id = self.get_current_location(issue.id)
column_name_after = self.get_matching_column(issue, config)
column_id = self.columns[column_name_after].id if column_name_after else ''
if not column_id or column_name_before == column_name_after or issue.state == 'closed':
continue

self.move_issue(client, issue, column_name_after, config)
self.columns[column_name_before].remove_card(card_id)
self.move_issue(client, issue, column_name_after, config)
self.columns[column_name_before].remove_card(card_id)

def move_issue(self, client, issue, column_name, config: Configuration):
if issue.state == 'closed':
config.logger.debug(f'skipping {issue.title} because the issue is closed')
return

card_id = [_id for _id, value in issue.card_id_project.items()
if value['project_number'] == config.project_number][0]

Expand Down
19 changes: 16 additions & 3 deletions src/github_automation/management/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def __init__(self, conf: str, event: str, quiet: bool = False, log_path: str = '

@staticmethod
def get_issue_number(event):
return event['issue']['number']
if 'issue' in event:
return event['issue']['number']
else:
print("This is not an issue, and we still do not support other github entities in the project.")
return

def get_prev_column_cursor(self, column_name):
layout = self.client.get_project_layout(owner=self.config.project_owner,
Expand Down Expand Up @@ -61,8 +65,7 @@ def load_project_column(self, column_name):
def manage_issue_in_project(self, issue):
if (self.config.remove and self.config.project_number in issue.get_associated_project()
and not is_matching_issue(issue.labels,
self.config.must_have_labels,
self.config.cant_have_labels,
self.config.must_have_labels, self.config.cant_have_labels,
self.config.filter_labels)):

card_id = [_id for _id, value in issue.card_id_project.items()
Expand All @@ -81,6 +84,7 @@ def manage_issue_in_project(self, issue):
if value['project_number'] == self.config.project_number][0]
if (self.config.add and not column_name_before) or \
(self.config.move and matching_column_name != column_name_before):
print(f'Moving {issue.title} from {column_name_before}')
project = self.load_project_column(matching_column_name)
project.move_issue(self.client, issue, matching_column_name, self.config)
return
Expand All @@ -93,13 +97,22 @@ def manage_issue_in_project(self, issue):

def get_issue_object(self):
issue_number = self.get_issue_number(self.event)
if issue_number is None:
return # In case the event is not for an issue

issue_response = self.client.get_issue(
self.project_owner, self.repository_name, issue_number) # need to address the remove here
issue = Issue(**parse_issue(issue_response['repository']['issue']))
return issue

def run(self):
issue = self.get_issue_object()
if issue is None:
return # In case the event is not for an issue

if issue.state == 'closed':
print("The issue is closed, not taking an action.")
return

for conf_path in self.conf_paths:
self.config = Configuration(conf_path, self.verbose, self.quiet, self.log_path)
Expand Down
134 changes: 33 additions & 101 deletions src/github_automation/management/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ def get_github_issues(self, owner, name, after, labels, milestone):
projectCards(first:5){
nodes{
id
column {
name
}
project{
number
columns(first:1){
nodes {
name
}
}
}
}
}
timelineItems(first:10, itemTypes:[CROSS_REFERENCED_EVENT]){
__typename
Expand Down Expand Up @@ -152,6 +150,7 @@ def get_github_issues(self, owner, name, after, labels, milestone):
title
id
number
state
milestone {
title
}
Expand Down Expand Up @@ -190,13 +189,11 @@ def get_github_issues(self, owner, name, after, labels, milestone):
projectCards(first:5){
nodes{
id
column {
name
}
project{
number
columns(first:1){
nodes {
name
}
}
}
}
}
Expand Down Expand Up @@ -338,13 +335,11 @@ def get_issue(self, owner, name, issue_number):
projectCards(first:5){
nodes{
id
column {
name
}
project{
number
columns(first:1){
nodes {
name
}
}
}
}
}
Expand Down Expand Up @@ -386,6 +381,7 @@ def get_issue(self, owner, name, issue_number):
title
id
number
state
milestone {
title
}
Expand Down Expand Up @@ -445,6 +441,13 @@ def get_column_issues(self, owner, name, project_number, prev_column_id, start_c
}
}
}
assignees(first: 10) {
edges {
node {
login
}
}
}
}
}
}
Expand All @@ -456,7 +459,7 @@ def get_column_issues(self, owner, name, project_number, prev_column_id, start_c
}
}
''', {"owner": owner, "name": name, "projectNumber": project_number, "prevColumnID": prev_column_id,
"$start_cards_cursor": start_cards_cursor})
"start_cards_cursor": start_cards_cursor})

def get_first_column_issues(self, owner, name, project_number, start_cards_cursor=''):
return self.execute_query('''
Expand Down Expand Up @@ -493,6 +496,13 @@ def get_first_column_issues(self, owner, name, project_number, start_cards_curso
}
}
}
assignees(first: 10) {
edges {
node {
login
}
}
}
}
... on PullRequest {
id
Expand All @@ -508,91 +518,13 @@ def get_first_column_issues(self, owner, name, project_number, start_cards_curso
}
}
}
''', {"owner": owner, "name": name, "projectNumber": project_number, "$start_cards_cursor": start_cards_cursor})

def search_issues_by_query(self, query, start_cursor=None):
return self.execute_query('''
query ($query: String!, $start_cursor: String){
search(type: ISSUE, query: $query, first: 100, after: $start_cursor) {
... on SearchResultItemConnection {
pageInfo {
hasNextPage
endCursor
}
issueCount
edges {
cursor
node {
... on Issue {
title
number
id
labels(first:10) {
edges {
node {
name
}
}
}
milestone {
title
}
assignees(last: 10) {
edges {
node {
id
login
}
}
}
timelineItems(first: 10, itemTypes: [CROSS_REFERENCED_EVENT]) {
__typename
... on IssueTimelineItemsConnection {
nodes {
... on CrossReferencedEvent {
willCloseTarget
source {
__typename
... on PullRequest {
state
isDraft
assignees(first: 10) {
nodes {
login
}
}
labels(first: 5) {
nodes {
name
}
}
reviewRequests(first: 1) {
totalCount
}
reviews(first: 1) {
totalCount
}
number
reviewDecision
}
}
}
}
}
}
}
}
}
}
}
}
''', {'query': query, 'start_cursor': start_cursor})
''', {"owner": owner, "name": name, "projectNumber": project_number, "start_cards_cursor": start_cards_cursor})

def un_archive_card(self, card_id):
return self.execute_query(''' mutation ($card_id: ID!, $isArchived: Boolean){
updateProjectCard(input: {projectCardId: $card_id, isArchived: $isArchived}) {
projectCard {
isArchived
}
}
}''', {'card_id': card_id, "isArchived": False})
updateProjectCard(input: {projectCardId: $card_id, isArchived: $isArchived}) {
projectCard {
isArchived
}
}
}''', {'card_id': card_id, "isArchived": False})
Loading

0 comments on commit 94ef872

Please sign in to comment.