Repository: mozilla/pontoon

Issue:
Daily sync errors for android-l10n project
*This issue was created automatically by a [script](https://github.com/mathjazz/bugzilla2github/).*
## [Bug 1726957](https://bugzilla.mozilla.org/show_bug.cgi?id=1726957)
Bug Reporter: @flodolo
CC:   @mathjazz

Starting August 18, we constantly get Sync errors from android-l10n

```
! [rejected]            HEAD -> master (fetch first)
error: failed to push some refs to 'git@github.com:mozilla-l10n/android-l10n.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
```

My guess is that Pontoon doesn't know how to deal gracefully with two projects working on the same repository, given only Pontoon writes to it these days.
Daily sync errors for android-l10n project
*This issue was created automatically by a [script](https://github.com/mathjazz/bugzilla2github/).*
## [Bug 1726957](https://bugzilla.mozilla.org/show_bug.cgi?id=1726957)
Bug Reporter: @flodolo
CC:   @mathjazz

Starting August 18, we constantly get Sync errors from android-l10n

```
! [rejected]            HEAD -> master (fetch first)
error: failed to push some refs to 'git@github.com:mozilla-l10n/android-l10n.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
```

My guess is that Pontoon doesn't know how to deal gracefully with two projects working on the same repository, given only Pontoon writes to it these days.


Discussion:
Comment Author: @mathjazz

Correct, we have the same problem in l10n-central repos, which is why we exclude those errors from email alerts. We should do the same for the Android errors, but I'm not sure [Papertrail search syntax](https://www.papertrail.com/help/search-syntax/) supports that.

The HG error message is verbose enough in the first line to filter out, which is not the case for the GitHub message, and I'm not sure we can filter out messages based on surrounding lines.

--

HG:
Aug 21 09:28:26 mozilla-pontoon app/worker.1 Failed to sync locale el for project seamonkey due to commit error: abort: push creates new remote head ce5b5f67d119!

GitHub:
Aug 21 15:18:12 mozilla-pontoon app/worker.1 Failed to sync locale hu for project focus-for-android due to commit error: To github.com:mozilla-l10n/android-l10n.git

--

We'll likely need to intercept the error message before we log it and modify it accordingly to be able to catch it.


Not really a P2 bug.
Currently we mitigate this issue by excluding all messages containing "android" from the "Failed to Sync locale" alert in Papertrail. The query looks like this:
`"Failed to sync locale" AND -"new remote head" AND -"android"`

That's obviously suboptimal, because we might miss valid errors that happen in our Android projects. And we (or other users) might bump into the same issue in non-android projects.

Let's change the [log message for Git repositories](https://github.com/mozilla/pontoon/issues/2306#issuecomment-918459383) to:

Aug 21 15:18:12 mozilla-pontoon app/worker.1 Failed to sync locale hu for project focus-for-android due to commit error: **Remote contains work that you do not have locally.**
Comment Author: @mathjazz

Correct, we have the same problem in l10n-central repos, which is why we exclude those errors from email alerts. We should do the same for the Android errors, but I'm not sure [Papertrail search syntax](https://www.papertrail.com/help/search-syntax/) supports that.

The HG error message is verbose enough in the first line to filter out, which is not the case for the GitHub message, and I'm not sure we can filter out messages based on surrounding lines.

--

HG:
Aug 21 09:28:26 mozilla-pontoon app/worker.1 Failed to sync locale el for project seamonkey due to commit error: abort: push creates new remote head ce5b5f67d119!

GitHub:
Aug 21 15:18:12 mozilla-pontoon app/worker.1 Failed to sync locale hu for project focus-for-android due to commit error: To github.com:mozilla-l10n/android-l10n.git

--

We'll likely need to intercept the error message before we log it and modify it accordingly to be able to catch it.


Not really a P2 bug.
Currently we mitigate this issue by excluding all messages containing "android" from the "Failed to Sync locale" alert in Papertrail. The query looks like this:
`"Failed to sync locale" AND -"new remote head" AND -"android"`

That's obviously suboptimal, because we might miss valid errors that happen in our Android projects. And we (or other users) might bump into the same issue in non-android projects.

Let's change the [log message for Git repositories](https://github.com/mozilla/pontoon/issues/2306#issuecomment-918459383) to:

Aug 21 15:18:12 mozilla-pontoon app/worker.1 Failed to sync locale hu for project focus-for-android due to commit error: **Remote contains work that you do not have locally.**

Resolving patch:
diff --git a/pontoon/sync/vcs/repositories.py b/pontoon/sync/vcs/repositories.py
--- a/pontoon/sync/vcs/repositories.py
+++ b/pontoon/sync/vcs/repositories.py
@@ -171,8 +171,18 @@ def commit(self, path=None, message=None, user=None, branch=None):
 
         push = ["git", "push", self.url, push_target]
         code, output, error = execute(push, path)
+        exception_message = str(error)
+
         if code != 0:
-            raise CommitToRepositoryException(str(error))
+            if (
+                "Updates were rejected because the remote contains work that you do"
+                in error
+            ):
+                exception_message = (
+                    "Remote contains work that you do not have locally. "
+                    + exception_message
+                )
+            raise CommitToRepositoryException(exception_message)
 
         if "Everything up-to-date" in error:
             return self.nothing_to_commit()


Test patch: