class GithubCheckRun(models.Model):
run_id = models.BigIntegerField(blank=True, null=True)
unique_id = models.UUIDField(default=uuid.uuid4)
# Head of the request when it was run
run_sha = models.CharField(max_length=40)
ref_pull_request = models.ForeignKey(
MonitoredPullRequest, on_delete=models.PROTECT, related_name="checks"
)
# This flag will be set to true if:
# 1. The documentation_pr exists and pr has been approved(i.e. webhook for approval received)
# 2. The documentation_pr does not exist and the user sets it manually
force_approve = models.BooleanField(null=True, blank=True, default=None)
class Meta:
unique_together = (
"run_sha",
"ref_pull_request",
)
def __str__(self):
return f"{self.ref_pull_request.code_pull_request.repository.repo_full_name}/{self.ref_pull_request.code_pull_request.pr_number} @ {self.run_sha[:7]} [{self.force_approve}]"
def save(self, *args, **kwargs):
if not self.pk:
# PK doesn't exist, new instance being saved to the DB. Create a new check
token = (
self.ref_pull_request.code_pull_request.repository.owner.creator.access_token
)
github_app_instance = github.Github(token)
github_repo = github_app_instance.get_repo(
self.ref_pull_request.code_pull_request.repository.repo_id
)
check_run_instance = github_repo.create_check_run(
name="Continuous Documentation",
head_sha=self.run_sha,
external_id=self.unique_id.__str__(),
status="in_progress",
details_url=f"{settings.WEBSITE_HOST}/{github_repo.full_name}/pull/{self.ref_pull_request.code_pull_request.pr_number}",
)
logger.info(
"Created new check run for PR", extra={"response": check_run_instance}
)
self.run_id = check_run_instance.id
super().save(*args, **kwargs)
class GithubCheckRun(models.Model):
run_id = models.BigIntegerField(blank=True, null=True)
unique_id = models.UUIDField(default=uuid.uuid4)
# Head of the request when it was run
run_sha = models.CharField(max_length=40)
ref_pull_request = models.ForeignKey(
MonitoredPullRequest, on_delete=models.PROTECT, related_name="checks"
)
# This flag will be set to true if:
# 1. The documentation_pr exists and pr has been approved(i.e. webhook for approval received)
# 2. The documentation_pr does not exist and the user sets it manually
force_approve = models.BooleanField(null=True, blank=True, default=None)
class Meta:
unique_together = (
"run_sha",
"ref_pull_request",
)
def __str__(self):
return f"{self.ref_pull_request.code_pull_request.repository.repo_full_name}/{self.ref_pull_request.code_pull_request.pr_number} @ {self.run_sha[:7]} [{self.force_approve}]"
def save(self, *args, **kwargs):
if not self.pk:
# PK doesn't exist, new instance being saved to the DB. Create a new check
token = (
self.ref_pull_request.code_pull_request.repository.owner.creator.access_token
)
github_app_instance = github.Github(token)
github_repo = github_app_instance.get_repo(
self.ref_pull_request.code_pull_request.repository.repo_id
)
check_run_instance = github_repo.create_check_run(
name="Continuous Documentation",
head_sha=self.run_sha,
external_id=self.unique_id.__str__(),
status="in_progress",
details_url=f"{settings.WEBSITE_HOST}/{github_repo.full_name}/pull/{self.ref_pull_request.code_pull_request.pr_number}",
)
logger.info(
"Created new check run for PR", extra={"response": check_run_instance}
)
self.run_id = check_run_instance.id
super().save(*args, **kwargs)