nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mandarons/icloud-drive-docker
Pull request #133 (2023-03-17 03:28:32)

once a day heartbeat


Patch:
diff --git a/src/usage.py b/src/usage.py
index 1330819c..895c1da8 100644
--- a/src/usage.py
+++ b/src/usage.py
@@ -1,6 +1,7 @@
 """To record usage of the app."""
 import json
 import os
+from datetime import datetime, timedelta
 
 import requests
 
@@ -95,6 +96,28 @@ def send_heartbeat(app_id, data=None):
     return post_new_heartbeat(data)
 
 
+def current_time():
+    """Get current time."""
+    return datetime.now()
+
+
+def heartbeat(cached_data, data):
+    """Send heartbeat."""
+    previous_heartbeat = cached_data.get("heartbeat_timestamp", None)
+    current = current_time()
+    if previous_heartbeat:
+        previous = datetime.strptime(previous_heartbeat, "%Y-%m-%d %H:%M:%S.%f")
+        if previous < (current - timedelta(hours=24)):
+            if send_heartbeat(cached_data.get("id"), data=data):
+                cached_data["heartbeat_timestamp"] = str(current)
+                return cached_data
+        else:
+            return False
+    elif send_heartbeat(cached_data.get("id"), data=data):
+        cached_data["heartbeat_timestamp"] = str(current)
+        return cached_data
+
+
 def alive(config, data=None):
     """Record liveliness."""
     cache_file_path = init_cache(config=config)
@@ -103,4 +126,7 @@ def alive(config, data=None):
         installed_data = install(cached_data=cached_data)
         if installed_data:
             return save_cache(file_path=cache_file_path, data=installed_data)
-    return send_heartbeat(app_id=cached_data.get("id"), data=data)
+    heartbeat_data = heartbeat(cached_data=cached_data, data=data)
+    if heartbeat_data:
+        return save_cache(file_path=cache_file_path, data=heartbeat_data)
+    return False


Test patch:
diff --git a/tests/test_usage.py b/tests/test_usage.py
index 8dd6fc81..58cc41a1 100644
--- a/tests/test_usage.py
+++ b/tests/test_usage.py
@@ -1,4 +1,5 @@
 """Tests for usage.py file."""
+import datetime
 import os
 import unittest
 import uuid
@@ -147,3 +148,23 @@ class TestUsage(unittest.TestCase):
             data={"installationId": fresh["id"], "data": None}
         )
         self.assertFalse(actual)
+
+    @patch("requests.post", side_effect=tests.mocked_usage_post)
+    def test_alive_valid(self, mock_post):
+        """Test alive functionality."""
+        # Test first run - install
+        actual = usage.alive(config=self.config)
+        self.assertTrue(actual)
+        # Test second run - no install but heartbeat
+        actual = usage.alive(config=self.config)
+        self.assertTrue(actual)
+        # Test third run - no install, no heartbeat
+        actual = usage.alive(config=self.config)
+        self.assertFalse(actual)
+
+        # Test heartbeat 24 hours ago
+        mock_now = datetime.datetime.now() + datetime.timedelta(hours=25)
+        with patch("src.usage.current_time") as mock_datetime_now:
+            mock_datetime_now.return_value = mock_now
+            actual = usage.alive(config=self.config)
+            self.assertTrue(actual)
Repository: mandarons/icloud-drive-docker
Pull request #154 (2023-09-20 17:43:15)

Fix: Repair receiver address
This PR is fixing Bug #153 


Patch:
diff --git a/src/notify.py b/src/notify.py
index 53f9c32e..7a696741 100644
--- a/src/notify.py
+++ b/src/notify.py
@@ -36,7 +36,7 @@ def send(config, last_send=None, dry_run=False):
                     else:
                         smtp.login(email, password)
 
-                msg = build_message(email)
+                msg = build_message(email, to_email)
 
                 smtp.sendmail(from_addr=email, to_addrs=to_email, msg=msg.as_string())
                 smtp.quit()
@@ -49,9 +49,9 @@ def send(config, last_send=None, dry_run=False):
     return sent_on
 
 
-def build_message(email):
+def build_message(email, to_email):
     """Create email message."""
-    message = Message(to=email)
+    message = Message(to=to_email)
     message.sender = "icloud-docker <" + email + ">"
     message.date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
     message.subject = "icloud-docker: Two step authentication required"


Test patch:
diff --git a/tests/test_notify.py b/tests/test_notify.py
index d9bdab45..8ceee2da 100644
--- a/tests/test_notify.py
+++ b/tests/test_notify.py
@@ -16,6 +16,7 @@ class TestNotify(unittest.TestCase):
             "app": {
                 "smtp": {
                     "email": "user@test.com",
+                    "to": "to@email.com",
                     "host": "smtp.test.com",
                     "port": "587",
                     "password": "password",
@@ -44,8 +45,11 @@ class TestNotify(unittest.TestCase):
 
     def test_build_message(self):
         """Test for building a valid email."""
-        msg = notify.build_message(self.config["app"]["smtp"]["email"])
-        self.assertEqual(msg.to, self.config["app"]["smtp"]["email"])
+        msg = notify.build_message(
+            email=self.config["app"]["smtp"]["email"],
+            to_email=self.config["app"]["smtp"]["to"],
+        )
+        self.assertEqual(msg.to, self.config["app"]["smtp"]["to"])
         self.assertIn(self.config["app"]["smtp"]["email"], msg.sender)
         self.assertIn("icloud-docker: Two step authentication required", msg.subject)
         self.assertIn(