Repository: oppia/oppia

Issue:
Fix Github Actions URL Construction when Reporting CLI Status
When users run CI tests on PRs from forks of Oppia, the URLs constructed by the service are slightly malformed. For example, the url created by the logger might be `https://github.com/oppia/oppia/actions/runs/439041970` instead of `https://github.com/ericzlou/oppia/actions/runs/439041970`.

We might be able to use the `GITHUB_REPOSITORY` environment variable to address this. The variable holds:

> The owner and repository name. For example, octocat/Hello-World.

This is copied from oppia/oppia-e2e-test-results-logger#8
Report Branch Name to E2E Logging Server
We can use the `GITHUB_REF` variable for github actions ([docs](https://docs.github.com/en/actions/reference/environment-variables)) and the `CIRCLE_BRANCH` variable for circleci ([docs](https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables))


Discussion:
Can you assign me this issue @U8NWXD? Willing to work on this one.
@OBITORASU I don't think this issue is a good fit for you. It requires working with a logging server whose source code you don't have access to. I'd recommend looking for issues labeled with `good first issue` or `good second issue` instead
Thanks for the heads up! I will look into something else then thank you :)


Resolving patch:
diff --git a/scripts/flake_checker.py b/scripts/flake_checker.py
--- a/scripts/flake_checker.py
+++ b/scripts/flake_checker.py
@@ -37,18 +37,19 @@
         'env': {
             'identifier': 'CIRCLECI',
             'user_info': 'CIRCLE_USERNAME',
-            'build_url': 'CIRCLE_BUILD_URL',
-            'build_id': None
-        }
+            'branch': 'CIRCLE_BRANCH',
+            'build_url_template_vars': ['CIRCLE_BUILD_URL']
+        },
+        'build_url_template': '%s',
     },
     'githubActions': {
         'env': {
             'identifier': 'GITHUB_ACTIONS',
             'user_info': 'GITHUB_ACTOR',
-            'build_url': None,
-            'build_id': 'GITHUB_RUN_ID',
+            'branch': 'GITHUB_REF',
+            'build_url_template_vars': ['GITHUB_REPOSITORY', 'GITHUB_RUN_ID'],
         },
-        'build_url_template': 'https://github.com/oppia/oppia/actions/runs/%s'
+        'build_url_template': 'https://github.com/%s/actions/runs/%s',
     }
 }
 
@@ -89,16 +90,21 @@ def _get_build_info():
         if not os.getenv(ci_env['identifier']):
             continue
 
-        if os.getenv(ci_env['build_url']) is not None:
-            build_url = os.getenv(ci_env['build_url'])
-        else:
-            build_url = info['build_url_template'] % os.getenv(
-                ci_env['build_id'])
+        template_values = []
+        for template_var in ci_env['build_url_template_vars']:
+            value = os.getenv(template_var)
+            if value is None:
+                raise RuntimeError(
+                    'Expected environment variable %s missing' %
+                    template_var)
+            template_values.append(value)
+        build_url = info['build_url_template'] % tuple(template_values)
         timestamp = datetime.datetime.utcnow().isoformat() + '+00:00'
 
         build_info['username'] = os.getenv(ci_env['user_info'])
         build_info['build_url'] = build_url
         build_info['timestamp'] = timestamp
+        build_info['branch'] = os.getenv(ci_env['branch'])
 
         return build_info
 


Test patch:
diff --git a/scripts/flake_checker_test.py b/scripts/flake_checker_test.py
--- a/scripts/flake_checker_test.py
+++ b/scripts/flake_checker_test.py
@@ -84,6 +84,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -96,6 +97,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             }
         }
 
@@ -123,6 +125,8 @@ def mock_getenv(variable):
                 'GITHUB_ACTIONS': 1,
                 'GITHUB_ACTOR': 'user',
                 'GITHUB_RUN_ID': 1234,
+                'GITHUB_REF': 'develop',
+                'GITHUB_REPOSITORY': 'foo/oppia',
             }
             return environment_vars.get(variable)
 
@@ -133,8 +137,9 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
             'suite': 'suiteName',
             'metadata': {
                 'username': 'user',
-                'build_url': 'https://github.com/oppia/oppia/actions/runs/1234',
+                'build_url': 'https://github.com/foo/oppia/actions/runs/1234',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             }
         }
 
@@ -162,6 +167,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -174,6 +180,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             },
         }
 
@@ -211,6 +218,28 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 Exception, 'Unknown build environment.'):
                 flake_checker.report_pass('suiteName')
 
+    def test_missing_environment_variable(self):
+
+        def mock_getenv(variable):
+            environment_vars = {
+                'CIRCLECI': 1,
+                'CIRCLE_USERNAME': 'user',
+                'CIRCLE_BRANCH': 'develop',
+            }
+            return environment_vars.get(variable)
+
+        def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-argument
+            raise AssertionError('requests.post called.')
+
+        getenv_swap = self.swap(os, 'getenv', mock_getenv)
+        post_swap = self.swap(requests, 'post', mock_post)
+
+        with getenv_swap, post_swap:
+            with self.assertRaisesRegexp(
+                RuntimeError,
+                'Expected environment variable CIRCLE_BUILD_URL missing'):
+                flake_checker.report_pass('suiteName')
+
 
 class MockResponse(python_utils.OBJECT):
 
@@ -243,6 +272,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -265,6 +295,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             }
         }
 
@@ -294,6 +325,8 @@ def mock_getenv(variable):
                 'GITHUB_ACTIONS': 1,
                 'GITHUB_ACTOR': 'user',
                 'GITHUB_RUN_ID': 1234,
+                'GITHUB_REF': 'develop',
+                'GITHUB_REPOSITORY': 'foo/oppia'
             }
             return environment_vars.get(variable)
 
@@ -314,8 +347,9 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
             'output_lines': ['line1', 'line2'],
             'metadata': {
                 'username': 'user',
-                'build_url': 'https://github.com/oppia/oppia/actions/runs/1234',
+                'build_url': 'https://github.com/foo/oppia/actions/runs/1234',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             }
         }
 
@@ -345,6 +379,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -358,6 +393,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             },
         }
 
@@ -387,6 +423,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -400,6 +437,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             },
         }
 
@@ -429,6 +467,7 @@ def mock_getenv(variable):
                 'CIRCLECI': 1,
                 'CIRCLE_USERNAME': 'user',
                 'CIRCLE_BUILD_URL': 'https://example.com',
+                'CIRCLE_BRANCH': 'develop',
             }
             return environment_vars.get(variable)
 
@@ -442,6 +481,7 @@ def mock_post(url, json, allow_redirects, headers):  # pylint: disable=unused-ar
                 'username': 'user',
                 'build_url': 'https://example.com',
                 'timestamp': '2020-01-01T00:00:00.000001+00:00',
+                'branch': 'develop',
             },
         }