diff --git a/docs/testing/web_platform_tests.md b/docs/testing/web_platform_tests.md index 44d8477e366c28..785c4c19c49953 100644 --- a/docs/testing/web_platform_tests.md +++ b/docs/testing/web_platform_tests.md @@ -120,9 +120,14 @@ unauthenticated requests, so it is recommended that you let `wpt-export` and `wpt-import` use your GitHub credentials when sending requests: 1. Generate a new [personal access token](https://github.com/settings/tokens) - 1. Create a JSON file with two keys: `GH_USER`, your GitHub user name, and - `GH_TOKEN`, the access token you have just generated. - 1. Pass `--credentials-json ` to `wpt-export` and `wpt-import`. + 1. Set up your credentials by either: + * Setting the `GH_USER` environment variable to your GitHub user name + and the `GH_TOKEN` environment variable to the access token you have + just created **or** + * Creating a JSON file with two keys: `GH_USER`, your GitHub user name, + and `GH_TOKEN`, the access token you have just generated. After that, + pass `--credentials-json ` to `wpt-export` and + `wpt-import`. ### Manual import diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common.py b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common.py index 89056d3d3a773e..16c34e410b6369 100644 --- a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common.py +++ b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common.py @@ -24,9 +24,22 @@ def read_credentials(host, credentials_json): - """Extracts credentials from a JSON file.""" + """Extracts GitHub and Gerrit credentials. + + The data is read from the environment and (optionally) a JSON file. When a + JSON file is specified, any variables from the environment are discarded + and the values are all expected to be present in the file. + + Args: + credentials_json: Path to a JSON file containing an object with the + keys we want to read, or None. + """ + env_credentials = {} + for key in ('GH_USER', 'GH_TOKEN', 'GERRIT_USER', 'GERRIT_TOKEN'): + if key in host.environ: + env_credentials[key] = host.environ[key] if not credentials_json: - return {} + return env_credentials if not host.filesystem.exists(credentials_json): _log.warning('Credentials JSON file not found at %s.', credentials_json) return {} diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common_unittest.py b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common_unittest.py index 2d1394addeadec..a2fa5e794ee039 100644 --- a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common_unittest.py +++ b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/common_unittest.py @@ -25,6 +25,24 @@ def test_get_credentials_empty(self): def test_get_credentials_none(self): self.assertEqual(read_credentials(MockHost(), None), {}) + def test_get_credentials_gets_values_from_environment(self): + host = MockHost() + host.environ.update({ + 'GH_USER': 'user-github', + 'GH_TOKEN': 'pass-github', + 'GERRIT_USER': 'user-gerrit', + 'GERRIT_TOKEN': 'pass-gerrit', + 'UNUSED_VALUE': 'foo', + }) + self.assertEqual( + read_credentials(host, None), + { + 'GH_USER': 'user-github', + 'GH_TOKEN': 'pass-github', + 'GERRIT_USER': 'user-gerrit', + 'GERRIT_TOKEN': 'pass-gerrit', + }) + def test_get_credentials_gets_values_from_file(self): host = MockHost() host.filesystem.write_text_file( @@ -44,6 +62,27 @@ def test_get_credentials_gets_values_from_file(self): 'GERRIT_TOKEN': 'pass-gerrit', }) + def test_get_credentials_choose_file_over_environment(self): + host = MockHost() + host.environ.update({ + 'GH_USER': 'user-github-from-env', + 'GH_TOKEN': 'pass-github-from-env', + 'GERRIT_USER': 'user-gerrit-from-env', + 'GERRIT_TOKEN': 'pass-gerrit-from-env', + }) + host.filesystem.write_text_file( + '/tmp/credentials.json', + json.dumps({ + 'GH_USER': 'user-github-from-json', + 'GH_TOKEN': 'pass-github-from-json', + })) + self.assertEqual( + read_credentials(host, '/tmp/credentials.json'), + { + 'GH_USER': 'user-github-from-json', + 'GH_TOKEN': 'pass-github-from-json', + }) + def test_is_testharness_baseline(self): self.assertTrue(is_testharness_baseline('fake-test-expected.txt')) self.assertTrue(is_testharness_baseline('external/wpt/fake-test-expected.txt')) diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_exporter.py b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_exporter.py index 7b9c1746069ab2..53aca97b425e03 100644 --- a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_exporter.py +++ b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_exporter.py @@ -48,8 +48,13 @@ def main(self, argv=None): self.host.executive.error_output_limit = None credentials = read_credentials(self.host, options.credentials_json) - if not (credentials['GH_USER'] and credentials['GH_TOKEN']): - _log.error('Must provide both user and token for GitHub.') + if not (credentials.get('GH_USER') and credentials.get('GH_TOKEN')): + _log.error('You must provide your GitHub credentials for this ' + 'script to work.') + _log.error('See https://chromium.googlesource.com/chromium/src' + '/+/master/docs/testing/web_platform_tests.md' + '#GitHub-credentials for instructions on how to set ' + 'your credentials up.') return False self.wpt_github = self.wpt_github or WPTGitHub(self.host, credentials['GH_USER'], credentials['GH_TOKEN']) diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_importer.py b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_importer.py index 6dac1d45f8fab3..a16e33695b4bb5 100644 --- a/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_importer.py +++ b/third_party/WebKit/Tools/Scripts/webkitpy/w3c/test_importer.py @@ -89,6 +89,14 @@ def main(self, argv=None): credentials = read_credentials(self.host, options.credentials_json) gh_user = credentials.get('GH_USER') gh_token = credentials.get('GH_TOKEN') + if not gh_user or not gh_token: + _log.warning('You have not set your GitHub credentials. This ' + 'script may fail with a network error when making ' + 'an API request to GitHub.') + _log.warning('See https://chromium.googlesource.com/chromium/src' + '/+/master/docs/testing/web_platform_tests.md' + '#GitHub-credentials for instructions on how to set ' + 'your credentials up.') self.wpt_github = self.wpt_github or WPTGitHub(self.host, gh_user, gh_token) self.git_cl = GitCL(self.host, auth_refresh_token_json=options.auth_refresh_token_json)