Repository: packit/packit-service
Issue: AttributeError: 'NoneType.set_commit_status' - project_with_commit is not set for TFT

Sentry Issue: [RED-HAT-0P-31A](https://sentry.io/organizations/red-hat-0p/issues/2083112609/?referrer=github_integration)

```
AttributeError: 'NoneType' object has no attribute 'set_commit_status'
(1 additional frame(s) were not displayed)
...
  File "packit_service/worker/handlers/abstract.py", line 256, in run_job
    job_results[result_key] = self.run_n_clean()
  File "packit_service/worker/handlers/abstract.py", line 152, in run_n_clean
    return self.run()
  File "packit_service/worker/handlers/testing_farm_handlers.py", line 133, in run
    status_reporter.report(
  File "packit_service/worker/reporting.py", line 87, in report
    self.set_status(
  File "packit_service/worker/reporting.py", line 162, in set_status
    self.project_with_commit.set_commit_status(
```

Changed files:
packit_service/worker/build/build_helper.py, tests/unit/test_reporting.py, packit_service/worker/reporting.py

diff --git a/packit_service/worker/build/build_helper.py b/packit_service/worker/build/build_helper.py
index 81eaa078..08967a7d 100644
--- a/packit_service/worker/build/build_helper.py
+++ b/packit_service/worker/build/build_helper.py
@@ -245,7 +245,6 @@ class BaseBuildJobHelper:
                 project=self.project,
                 commit_sha=self.metadata.commit_sha,
                 pr_id=self.metadata.pr_id,
-                base_project=self.base_project,
             )
         return self._status_reporter
 
diff --git a/packit_service/worker/reporting.py b/packit_service/worker/reporting.py
index a62ed280..f264a7e6 100644
--- a/packit_service/worker/reporting.py
+++ b/packit_service/worker/reporting.py
@@ -40,13 +40,12 @@ class StatusReporter:
         project: GitProject,
         commit_sha: str,
         pr_id: Optional[int] = None,
-        base_project: Optional[GitProject] = None,
     ):
         logger.debug(
             f"Status reporter will report for {project}, commit={commit_sha}, pr={pr_id}"
         )
         self.project = project
-        self.base_project = base_project
+        self._project_with_commit = None
         self.commit_sha = commit_sha
         self.pr_id = pr_id
 
@@ -55,10 +54,14 @@ class StatusReporter:
         """
         Returns GitProject from which we can set commit status.
         """
-        if isinstance(self.project, GitlabProject):
-            return self.base_project
+        if self._project_with_commit is None:
+            self._project_with_commit = (
+                self.project.get_pr(self.pr_id).source_project
+                if isinstance(self.project, GitlabProject) and self.pr_id is not None
+                else self.project
+            )
 
-        return self.project
+        return self._project_with_commit
 
     def report(
         self,
@@ -165,8 +168,9 @@ class StatusReporter:
         except gitlab.exceptions.GitlabCreateError as e:
             # Ignoring Gitlab 'enqueue' error
             # https://github.com/packit-service/packit-service/issues/741
-            if e.response_code == 403:
+            if e.response_code != 400:
                 # 403: No permissions to set status, falling back to comment
+                # 404: Commit has not been found, e.g. used target project on GitLab
                 logger.error(
                     f"Failed to set status for {self.commit_sha}, commenting on"
                     f" commit as a fallback: {str(e)}"
@@ -174,6 +178,8 @@ class StatusReporter:
                 self.__add_commit_comment_with_status(
                     state, description, check_name, url
                 )
+            if e.response_code not in {400, 403, 404}:
+                raise
         except github.GithubException:
             self.__add_commit_comment_with_status(state, description, check_name, url)
 
diff --git a/tests/unit/test_reporting.py b/tests/unit/test_reporting.py
index 36b3cb9d..797fa0c6 100644
--- a/tests/unit/test_reporting.py
+++ b/tests/unit/test_reporting.py
@@ -26,13 +26,14 @@ import pytest
 from flexmock import flexmock
 
 from ogr.abstract import CommitStatus
+from ogr.services.gitlab import GitlabProject
 from packit_service.worker.reporting import StatusReporter
 
 
 @pytest.mark.parametrize(
     (
         "project,commit_sha,"
-        "pr_id,has_pr_id,pr_object,"
+        "pr_id,pr_object,"
         "state,description,check_name,url,"
         "needs_pr_flags,uid"
     ),
@@ -41,7 +42,6 @@ from packit_service.worker.reporting import StatusReporter
             flexmock(),
             "7654321",
             "11",
-            True,
             flexmock(),
             CommitStatus.success,
             "We made it!",
@@ -55,7 +55,6 @@ from packit_service.worker.reporting import StatusReporter
             flexmock(),
             "7654321",
             None,
-            False,
             flexmock(),
             CommitStatus.failure,
             "We made it!",
@@ -69,7 +68,6 @@ from packit_service.worker.reporting import StatusReporter
             flexmock(),
             "7654321",
             None,
-            False,
             flexmock(head_commit="1234567"),
             CommitStatus.pending,
             "We made it!",
@@ -83,7 +81,6 @@ from packit_service.worker.reporting import StatusReporter
             flexmock(),
             "7654321",
             None,
-            False,
             flexmock(head_commit="7654321"),
             CommitStatus.error,
             "We made it!",
@@ -99,7 +96,6 @@ def test_set_status(
     project,
     commit_sha,
     pr_id,
-    has_pr_id,
     pr_object,
     state,
     description,
@@ -114,7 +110,7 @@ def test_set_status(
         commit_sha, state, url, description, check_name, trim=True
     ).once()
 
-    if has_pr_id:
+    if pr_id is not None:
         project.should_receive("get_pr").with_args(pr_id).once().and_return(pr_object)
 
     if needs_pr_flags:
@@ -125,6 +121,56 @@ def test_set_status(
     reporter.set_status(state, description, check_name, url)
 
 
+@pytest.mark.parametrize(
+    ("commit_sha,pr_id,pr_object," "state,description,check_name,url,"),
+    [
+        pytest.param(
+            "7654321",
+            None,
+            None,
+            CommitStatus.success,
+            "We made it!",
+            "packit/pr-rpm-build",
+            "https://api.packit.dev/build/111/logs",
+            id="Gitlab branch",
+        ),
+        pytest.param(
+            "7654321",
+            1,
+            flexmock(source_project=flexmock()),
+            CommitStatus.success,
+            "We made it!",
+            "packit/pr-rpm-build",
+            "https://api.packit.dev/build/111/logs",
+            id="Gitlab PR",
+        ),
+    ],
+)
+def test_set_status_gitlab(
+    commit_sha,
+    pr_id,
+    pr_object,
+    state,
+    description,
+    check_name,
+    url,
+):
+    project = GitlabProject(None, None, None)
+    reporter = StatusReporter(project, commit_sha, pr_id)
+    act_upon = flexmock(pr_object.source_project) if pr_id else flexmock(GitlabProject)
+
+    act_upon.should_receive("set_commit_status").with_args(
+        commit_sha, state, url, description, check_name, trim=True
+    ).once()
+
+    if pr_id is not None:
+        flexmock(GitlabProject).should_receive("get_pr").with_args(pr_id).and_return(
+            pr_object
+        )
+
+    reporter.set_status(state, description, check_name, url)
+
+
 @pytest.mark.parametrize(
     (
         "project,commit_sha,"