def test_project_empty_card(): project = Project( name="test project", config=Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')), columns={ "Queue": ProjectColumn(id="id", name="Queue", cards=[ IssueCard(id="sdf", issue=Issue(id="sdf", title="title", number=1)) ]), "Review in progress": ProjectColumn(id="id", name="Review in progress", cards=[]) }) assert project.name == "test project" assert len(project.get_all_issue_ids()) == 1 assert len(project.columns.keys()) == 2 assert project.columns['Queue'].name == 'Queue' assert project.columns['Review in progress'].name == 'Review in progress'
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.filter_labels)): card_id = [_id for _id, value in issue.card_id_project.items() if value['project_number'] == self.config.project_number][0] Project.remove_issue(self.client, issue.title, card_id, self.config) return matching_column_name = Project.get_matching_column(issue, self.config) if self.config.add and self.config.project_number not in issue.get_associated_project(): project = self.load_project_column(matching_column_name) project.add_issue(self.client, issue, matching_column_name, self.config) return column_name_before = [value['project_column'] for _id, value in issue.card_id_project.items() 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): project = self.load_project_column(matching_column_name) project.move_issue(self.client, issue, matching_column_name, self.config) return if self.config.sort and column_name_before == matching_column_name: project = self.load_project_column(matching_column_name) project.columns[matching_column_name].sort_cards(self.client, self.config) return
def test_missing_items(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() project = Project( name="test project", config=config, columns={ "Queue": ProjectColumn(id="id", name="Queue", cards=[ItemCard(id="sdf", item=Issue(id="sdf", title="title", number=1))]), "Review in progress": ProjectColumn(id="id", name="Review in progress", cards=[]) } ) assert len(project.get_all_item_ids()) == 1 assert len(project.columns.keys()) == 2 issue = Issue( id="2", number=2, title="issue title" ) issues = { "2": issue } assert project.find_missing_item_ids(issues) == {"2"}
def test_adding_issue(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() project = Project(name="test project", config=config, columns={ "Queue": ProjectColumn(id="id", name="Queue", cards=[ IssueCard(id="sdf", issue=Issue( id="sdf", title="title", number=2)) ]), "Review in progress": ProjectColumn(id="id", name="Review in progress", cards=[]) }) assert len(project.get_all_issue_ids()) == 1 assert len(project.columns.keys()) == 2 issue = Issue(id="1", number=1, title="Rony") assert issue.priority_rank == 0 issues = {"1": issue} class ClientMock(object): def add_issues_to_project(*args, **kwargs): return {'addProjectCard': {'cardEdge': {'node': {'id': "1"}}}} def add_to_column(*args, **kwargs): return def move_to_specific_place_in_column(*args, **kwargs): return project.add_issues(ClientMock, issues, {"1"}, config) assert project.columns['Queue'].cards[0].issue.title == "Rony" # testing non existent column with pytest.raises(Exception) as err: project.add_issue(ClientMock, issue, "non existent", config) assert "Did not found a matching column" in err issue2 = Issue(id="1", number=1, title="Rony", card_id_to_project={"1": { "project_number": 1 }}) issues2 = {"2": issue2} project.add_issues(ClientMock, issues2, {"2"}, config) assert project.columns['Queue'].cards[0].issue.title == "Rony"
def load_project_column(self, column_name): prev_cursor = self.get_prev_column_cursor(column_name) if prev_cursor: response = get_column_issues_with_prev_column(self.client, self.config, prev_cursor) else: response = get_first_column_issues(self.client, self.config) return Project(**parse_project(response.get("repository", {}).get('project', {}), config=self.config))
def load_project_column(self, column_name): prev_cursor = self.get_prev_column_cursor(column_name) if prev_cursor: response = get_column_items_with_prev_column( self.client, self.config, prev_cursor) else: response = get_first_column_items(self.client, self.config) project = get_project_from_response(response, self.config.is_org_project) return Project(**parse_project(project, config=self.config))
def test_removing_items(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() project = Project( name="test project", config=config, columns={ "Queue": ProjectColumn(id="id", name="Queue", cards=[ItemCard(id="sdf", item=Issue(id="sdf", title="title", number=1)), ItemCard(id="sdff", item=PullRequest(id="sdff", title="title", number=2))]), "Review in progress": ProjectColumn(id="id", name="Review in progress", cards=[]) } ) assert len(project.get_all_item_ids()) == 2 assert len(project.columns.keys()) == 2 class ClientMock(object): def delete_project_card(self, **kwargs): return project.remove_items(ClientMock, config) assert project.get_all_item_ids() == set()
def test_get_matching_column(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() issue_queue = Issue(id="1", title="issue 1", number=1) assert Project.get_matching_column(issue_queue, config) == 'Queue' issue_in_progress = Issue(id="1", title="issue 1", number=1) issue_in_progress.add_assignee("Rony") assert Project.get_matching_column(issue_in_progress, config) == '' issue_in_progress.add_label("Testing") assert Project.get_matching_column(issue_in_progress, config) == 'In progress' issue_review_in_progress = Issue(id="1", title="issue 1", number=1) issue_review_in_progress.add_assignee("Rony") class MockPullRequest(object): review_requested = True review_completed = False issue_review_in_progress.pull_request = MockPullRequest() assert Project.get_matching_column(issue_review_in_progress, config) == 'Review in progress' issue_docs = Issue(id="1", title="issue 1", number=1) issue_docs.add_assignee("Rony") class MockPullRequest2(object): review_requested = True review_completed = True assignees = "ronykoz" issue_docs.pull_request = MockPullRequest2() assert Project.get_matching_column(issue_docs, config) == 'Waiting for Docs' class MockPullRequest3(object): review_requested = True review_completed = True assignees = "someone" issue_docs.pull_request = MockPullRequest3() assert Project.get_matching_column(issue_docs, config) == 'Review in progress' # faulty field from issues config.column_to_rules["Waiting for Docs"] = { "issue.not_existent": "field" } assert Project.get_matching_column(issue_docs, config) == 'Review in progress'
def test_event_manager_flow(mocker): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() project_object = Project(name="project_name", columns={ "In progress": ProjectColumn(id="some id", name='In progress', cards=[]) }, config=config) mocker.patch.object(EventManager, "get_issue_object", return_value=Issue(id="1", title="this is a test title", number=1, assignees=["ronykoz"], labels=['test', 'Testing', 'bug'])) mocker.patch.object(EventManager, "load_project_column", return_value=project_object) class MockClient(object): def add_issues_to_project(*args, **kwargs): return {"addProjectCard": {"cardEdge": {"node": {"id": "1"}}}} def add_to_column(self, **kwargs): return def move_to_specific_place_in_column(self, **kwargs): return client = MockClient() manager = EventManager(os.path.join(MOCK_FOLDER_PATH, 'conf.ini'), client=client, event=json.dumps({"text": "text"})) manager.run() assert len(project_object.get_all_issue_ids()) == 1
def test_move_issues(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() issue = Issue(id="1", title="title", number=1, assignees=["Rony"], labels=["Testing"], card_id_to_project={"sdf": { "project_number": 1 }}) project = Project(name="test project", config=config, columns={ "Queue": ProjectColumn( id="id", name="Queue", cards=[IssueCard(id="sdf", issue=issue)]), "In progress": ProjectColumn(id="id", name="In progress", cards=[]) }) class MockClient(object): def add_to_column(*args, **kwargs): return def move_to_specific_place_in_column(*args, **kwargs): return project.move_issues(MockClient(), config) assert project.is_in_column("Queue", "1") is False assert project.is_in_column("In progress", "1") is True # Move within the same column project.move_issues(MockClient(), config) assert project.is_in_column("Queue", "1") is False assert project.is_in_column("In progress", "1") is True
def get_github_project(self): layout = self.client.get_project_layout(owner=self.config.project_owner, repository_name=self.config.repository_name, project_number=self.config.project_number) column_edges = layout['repository']['project']['columns']['edges'] project_builder = get_first_column_issues(self.client, self.config) for index, column in enumerate(column_edges): if column['node']['name'] in self.config.column_names: if index == 0: continue else: prev_cursor = column_edges[index - 1]['cursor'] column_response = get_column_issues_with_prev_column(self.client, self.config, prev_cursor) project_builder['repository']['project']['columns']['nodes'].extend( column_response['repository']['project']['columns']['nodes']) return Project(**parse_project(project_builder['repository']['project'], self.config))
def get_github_project(self): layout = self.client.get_project_layout(owner=self.config.project_owner, repository_name=self.config.repository_name, project_number=self.config.project_number, is_org_project=self.config.is_org_project) layout_project = get_project_from_response(layout, self.config.is_org_project) column_edges = layout_project['columns']['edges'] builder = get_first_column_items(self.client, self.config) builder_project = get_project_from_response(builder, self.config.is_org_project) for index, column in enumerate(column_edges): if column['node']['name'] in self.config.column_names: if index == 0: continue else: prev_cursor = column_edges[index - 1]['cursor'] column_response = get_column_items_with_prev_column(self.client, self.config, prev_cursor) column_project = get_project_from_response(column_response, self.config.is_org_project) builder_project['columns']['nodes'].extend( column_project['columns']['nodes']) return Project(**parse_project(builder_project, self.config))
def test_move_items(): config = Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')) config.load_properties() issue = Issue(id="1", title="title", number=1, assignees=["Rony"], labels=["Testing"], card_id_to_project={ "sdf": { "project_number": 1 } } ) pull_request = PullRequest(id="10", title="title", number=10, assignees=["Daud"], labels=["Testing"], card_id_to_project={ "dsf": { "project_number": 1 } }) project = Project( name="test project", config=config, columns={ "Queue": ProjectColumn(id="id", name="Queue", cards=[ItemCard(id="sdf", item=issue), ItemCard(id="dsf", item=pull_request) ]), "In progress": ProjectColumn(id="id", name="In progress", cards=[]) } ) class MockClient(object): def add_to_column(*args, **kwargs): return def move_to_specific_place_in_column(*args, **kwargs): return project.move_items(MockClient(), config, {'1': issue}) assert project.is_in_column("Queue", "1") is False assert project.is_in_column("In progress", "1") is True project.move_items(MockClient(), config, {'10': pull_request}) assert project.is_in_column("Queue", "10") is False assert project.is_in_column("In progress", "10") is True # Move within the same column project.move_items(MockClient(), config, {'1': issue}) assert project.is_in_column("Queue", "1") is False assert project.is_in_column("In progress", "1") is True issue.state = 'closed' project.move_item(MockClient(), issue, 'In progress', config) assert project.is_in_column("In progress", "1") is True
def test_project(): project = Project(**parse_project({ "name": "test", "columns": { "nodes": [ { "name": "Queue", "id": "1234", "cards": { "pageInfo": { "hasNextPage": True, "endCursor": "MQ" }, "edges": [ { "cursor": "MQ", "node": { "note": None, "state": "CONTENT_ONLY", "id": "3434=", "content": { "__typename": "Issue", "id": "1234=", "number": 1, "title": "issue 1", "labels": { "edges": [ { "node": { "name": "High" } }, { "node": { "name": "bug" } } ] }, "assignees": { "edges": [] } } } } ] } }, { "name": "Review in progress", "id": "5656", "cards": { "pageInfo": { "hasNextPage": True, "endCursor": "MQ" }, "edges": [ { "cursor": "MQ", "node": { "note": None, "state": "CONTENT_ONLY", "id": "56565=", "content": { "__typename": "Issue", "id": "5656=", "number": 15, "title": "issue 2", "labels": { "edges": [ { "node": { "name": "Medium" } }, { "node": { "name": "bug" } } ] }, "assignees": { "edges": [ { "node": { "id": "234", "login": "******" } } ] } } } } ] } } ] } }, Configuration(os.path.join(MOCK_FOLDER_PATH, 'conf.ini')))) assert len(project.get_all_item_ids()) == 2 assert len(project.columns.keys()) == 2 assert project.columns['Queue'].name == 'Queue' assert project.columns['Queue'].cards[0].item_title == 'issue 1' assert project.columns['Review in progress'].name == 'Review in progress' assert project.columns['Review in progress'].cards[0].item_title == 'issue 2'