From 79596e43c2a12fba1ca3781b094259b3053db17a Mon Sep 17 00:00:00 2001
From: Sergio Zharinov <zharinov@users.noreply.github.com>
Date: Wed, 9 Sep 2020 15:54:57 +0400
Subject: [PATCH] fix(gitea): Return only PRs created by Renovate (#7229)

---
 .../gitea/__snapshots__/index.spec.ts.snap    | 42 +++++++++++++++++++
 lib/platform/gitea/gitea-helper.ts            |  1 +
 lib/platform/gitea/index.spec.ts              | 35 ++++++++++++++++
 lib/platform/gitea/index.ts                   |  7 ++++
 4 files changed, 85 insertions(+)

diff --git a/lib/platform/gitea/__snapshots__/index.spec.ts.snap b/lib/platform/gitea/__snapshots__/index.spec.ts.snap
index eb328c1496..094944432f 100644
--- a/lib/platform/gitea/__snapshots__/index.spec.ts.snap
+++ b/lib/platform/gitea/__snapshots__/index.spec.ts.snap
@@ -72,6 +72,48 @@ Object {
 }
 `;
 
+exports[`platform/gitea getPrList should filter list by creator 1`] = `
+Object {
+  "endpoint": "https://gitea.com/api/v1/",
+  "gitAuthor": "Renovate Bot <renovate@example.com>",
+}
+`;
+
+exports[`platform/gitea getPrList should filter list by creator 2`] = `
+Array [
+  Object {
+    "body": "some random pull request",
+    "branchName": "some-head-branch",
+    "canMerge": true,
+    "createdAt": "2015-03-22T20:36:16Z",
+    "displayNumber": "Pull Request #1",
+    "hasAssignees": false,
+    "isConflicted": false,
+    "number": 1,
+    "sha": "some-head-sha",
+    "sourceRepo": "some/repo",
+    "state": "open",
+    "targetBranch": "some-base-branch",
+    "title": "Some PR",
+  },
+  Object {
+    "body": "other random pull request",
+    "branchName": "other-head-branch",
+    "canMerge": true,
+    "createdAt": "2011-08-18T22:30:38Z",
+    "displayNumber": "Pull Request #2",
+    "hasAssignees": false,
+    "isConflicted": false,
+    "number": 2,
+    "sha": "other-head-sha",
+    "sourceRepo": "some/repo",
+    "state": "closed",
+    "targetBranch": "other-base-branch",
+    "title": "Other PR",
+  },
+]
+`;
+
 exports[`platform/gitea getPrList should return list of pull requests 1`] = `
 Array [
   Object {
diff --git a/lib/platform/gitea/gitea-helper.ts b/lib/platform/gitea/gitea-helper.ts
index a9436f2ae5..ed91a3f799 100644
--- a/lib/platform/gitea/gitea-helper.ts
+++ b/lib/platform/gitea/gitea-helper.ts
@@ -36,6 +36,7 @@ export interface PR {
     login?: string;
   };
   assignees?: any[];
+  user?: { username?: string };
 }
 
 export interface Issue {
diff --git a/lib/platform/gitea/index.spec.ts b/lib/platform/gitea/index.spec.ts
index bb80df56c2..20101dd206 100644
--- a/lib/platform/gitea/index.spec.ts
+++ b/lib/platform/gitea/index.spec.ts
@@ -514,6 +514,41 @@ describe('platform/gitea', () => {
       expect(res).toMatchSnapshot();
     });
 
+    it('should filter list by creator', async () => {
+      helper.getCurrentUser.mockResolvedValueOnce(mockUser);
+
+      expect(
+        await gitea.initPlatform({ token: 'some-token' })
+      ).toMatchSnapshot();
+
+      await initFakeRepo();
+
+      helper.searchPRs.mockResolvedValueOnce([
+        partial<ght.PR>({
+          number: 3,
+          title: 'Third-party PR',
+          body: 'other random pull request',
+          state: PrState.Open,
+          diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/3.diff',
+          created_at: '2011-08-18T22:30:38Z',
+          closed_at: '2016-01-09T10:03:21Z',
+          mergeable: true,
+          base: { ref: 'third-party-base-branch' },
+          head: {
+            label: 'other-head-branch',
+            sha: 'other-head-sha',
+            repo: partial<ght.Repo>({ full_name: mockRepo.full_name }),
+          },
+          user: { username: 'not-renovate' },
+        }),
+        ...mockPRs.map((pr) => ({ ...pr, user: { username: 'renovate' } })),
+      ]);
+
+      const res = await gitea.getPrList();
+      expect(res).toHaveLength(mockPRs.length);
+      expect(res).toMatchSnapshot();
+    });
+
     it('should cache results after first query', async () => {
       helper.searchPRs.mockResolvedValueOnce(mockPRs);
       await initFakeRepo();
diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts
index 003b87cd3e..80d4f6733b 100644
--- a/lib/platform/gitea/index.ts
+++ b/lib/platform/gitea/index.ts
@@ -58,6 +58,7 @@ const defaultConfigFile = configFileNames[0];
 
 let config: GiteaRepoConfig = {} as any;
 let botUserID: number;
+let botUserName: string;
 
 function toRenovateIssue(data: helper.Issue): Issue {
   return {
@@ -85,6 +86,11 @@ function toRenovatePR(data: helper.PR): Pr | null {
     return null;
   }
 
+  const createdBy = data.user?.username;
+  if (createdBy && botUserName && createdBy !== botUserName) {
+    return null;
+  }
+
   return {
     number: data.number,
     displayNumber: `Pull Request #${data.number}`,
@@ -187,6 +193,7 @@ const platform: Platform = {
       const user = await helper.getCurrentUser({ token });
       gitAuthor = `${user.full_name || user.username} <${user.email}>`;
       botUserID = user.id;
+      botUserName = user.username;
     } catch (err) {
       logger.debug(
         { err },
-- 
GitLab