def post_task_handler(**kwargs): job_id = kwargs['kwargs']['job_id'] job = database['session'].query(Job).get(job_id) if job.integration_name != 'github': return task_state = kwargs['state'] if job.conf: if task_state == 'SUCCESS' and job.status != 'running': github_status = job.status else: github_status = 'error' set_commit_status( github_status, owner=job.conf['owner'], repo=job.conf['repo'], sha=job.conf['sha'], job_number=job.number, is_pr=job.conf['is_pr'], tokens=job.source.tokens)
def test_set_commit_status_error(mock_post): mock_response = mock.MagicMock() mock_response.status_code = 201 mock_post.return_value = mock_response tokens = ['my-token'] r = set_commit_status('error', 'my-org', 'my-repo', 'abcde', 'my-job-id', tokens) assert r expected_url = '{base}/repos/my-org/my-repo/statuses/abcde'.format( base=settings.GITHUB_API_BASE, ) expected_headers = {'Authorization': 'token my-token'} expected_data = { 'state': 'error', 'target_url': '{base}/jobs/my-job-id'.format(base=settings.BASE_URL), 'description': 'Errors during data validation', 'context': 'goodtables.io/push' } mock_post.assert_called_with(expected_url, json=expected_data, headers=expected_headers)
def test_set_commit_status_success(mock_post): mock_response = mock.MagicMock() mock_response.status_code = 201 mock_post.return_value = mock_response tokens = ['my-token'] r = set_commit_status('success', 'my-org', 'my-repo', 'abcde', 1, False, tokens) assert r expected_url = '{base}/repos/my-org/my-repo/statuses/abcde'.format( base=settings.GITHUB_API_BASE, ) expected_headers = {'Authorization': 'token my-token'} expected_data = { 'state': 'success', 'target_url': '{base}/github/my-org/my-repo/jobs/1'.format(base=settings.BASE_URL), 'description': 'Data is valid', 'context': 'continuous-integration/goodtables.io/push' } mock_post.assert_called_with(expected_url, json=expected_data, headers=expected_headers)
def create_and_start_job(self, conf=None): # Get tokens if not self.tokens: log.error('No GitHub tokens available to perform the job') return None # Get default repo details if not conf: conf = get_default_repo_details(self.owner, self.repo, token=self.tokens[0]) if not conf: log.error('No default repo details are available') return None # Save job to database job_id = make_uuid() params = { 'id': job_id, 'integration_name': 'github', 'source_id': self.id, 'conf': conf } job = Job(**params) job.source = self database['session'].add(job) database['session'].commit() # Set GitHub status set_commit_status('pending', owner=conf['owner'], repo=conf['repo'], sha=conf['sha'], job_number=job.number, is_pr=conf['is_pr'], tokens=self.tokens) # Run validation run_validation(conf['owner'], conf['repo'], conf['sha'], job_id=job_id, tokens=self.tokens) return job_id
def test_set_commit_status_problem(mock_post): mock_response = mock.MagicMock() mock_response.status_code = 400 mock_post.return_value = mock_response tokens = ['my-token'] r = set_commit_status('success', 'my-org', 'my-repo', 'abcde', 'my-job-id', tokens) assert not r