-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add project metadata tracking with telemetry #3929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b564859
Implement project metadata metrics
Leo10Gama 89400c5
Implement metric metadata integration tests
Leo10Gama 554e02b
Tweak methods so tests don't fail on Linux machines
Leo10Gama 067fd39
Repair failing integration test
Leo10Gama 4312f99
Implement proper exception handling for subprocesses
Leo10Gama a865083
Format according to standard
Leo10Gama f7e3e51
Refactor function names and encryption logic
Leo10Gama 6ee44d8
Remove unnecessary file
Leo10Gama eea4f31
Add type hints to new functions
Leo10Gama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| Creates and encrypts metadata regarding SAM CLI projects. | ||
| """ | ||
|
|
||
| from os import getcwd | ||
| from os.path import basename | ||
| import re | ||
| import subprocess | ||
| from typing import List, Optional | ||
| from uuid import uuid5, NAMESPACE_URL | ||
|
|
||
| from samcli.cli.global_config import GlobalConfig | ||
|
|
||
|
|
||
| def get_git_remote_origin_url() -> Optional[str]: | ||
| """ | ||
| Retrieve an encrypted version of the project's git remote origin url, if it exists. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | None | ||
| A UUID5 encrypted string of the git remote origin url, formatted such that the | ||
| encrypted value follows the pattern <hostname>/<owner>/<project_name>.git. | ||
| If telemetry is opted out of by the user, or the `.git` folder is not found | ||
| (the directory is not a git repository), returns None | ||
| """ | ||
| if not bool(GlobalConfig().telemetry_enabled): | ||
| return None | ||
|
|
||
| git_url = None | ||
| try: | ||
| runcmd = subprocess.run( | ||
| ["git", "config", "--get", "remote.origin.url"], capture_output=True, shell=True, check=True, text=True | ||
| ) | ||
| metadata = _parse_remote_origin_url(str(runcmd.stdout)) | ||
| git_url = "/".join(metadata) + ".git" # Format to <hostname>/<owner>/<project_name>.git | ||
| except subprocess.CalledProcessError: | ||
| return None # Not a git repo | ||
|
|
||
| return _encrypt_value(git_url) | ||
|
|
||
|
|
||
| def get_project_name() -> Optional[str]: | ||
| """ | ||
| Retrieve an encrypted version of the project's name, as defined by the .git folder (or directory name if no .git). | ||
|
|
||
| Returns | ||
| ------- | ||
| str | None | ||
| A UUID5 encrypted string of either the name of the project, or the name of the | ||
| current directory that the command is running in. | ||
| If telemetry is opted out of by the user, returns None | ||
| """ | ||
| if not bool(GlobalConfig().telemetry_enabled): | ||
| return None | ||
|
|
||
| project_name = "" | ||
| try: | ||
| runcmd = subprocess.run( | ||
| ["git", "config", "--get", "remote.origin.url"], capture_output=True, shell=True, check=True, text=True | ||
| ) | ||
| project_name = _parse_remote_origin_url(str(runcmd.stdout))[2] # dir is git repo, get project name from URL | ||
| except subprocess.CalledProcessError: | ||
| project_name = basename(getcwd().replace("\\", "/")) # dir is not a git repo, get directory name | ||
|
|
||
| return _encrypt_value(project_name) | ||
|
|
||
|
|
||
| def get_initial_commit_hash() -> Optional[str]: | ||
| """ | ||
| Retrieve an encrypted version of the project's initial commit hash, if it exists. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | None | ||
| A UUID5 encrypted string of the git project's initial commit hash. | ||
| If telemetry is opted out of by the user, or the `.git` folder is not found | ||
| (the directory is not a git repository), returns None. | ||
| """ | ||
| if not bool(GlobalConfig().telemetry_enabled): | ||
| return None | ||
|
|
||
| metadata = None | ||
| try: | ||
| runcmd = subprocess.run( | ||
| ["git", "rev-list", "--max-parents=0", "HEAD"], capture_output=True, shell=True, check=True, text=True | ||
| ) | ||
| metadata = runcmd.stdout.strip() | ||
| except subprocess.CalledProcessError: | ||
| return None # Not a git repo | ||
|
|
||
| return _encrypt_value(metadata) | ||
|
|
||
|
|
||
| def _parse_remote_origin_url(url: str) -> List[str]: | ||
| """ | ||
| Parse a `git remote origin url` into its hostname, owner, and project name. | ||
|
|
||
| Returns | ||
| ------- | ||
| List[str] | ||
| A list of 3 strings, with indeces corresponding to 0:hostname, 1:owner, 2:project_name | ||
| """ | ||
| pattern = re.compile(r"(?:https?://|git@)(?P<hostname>\S*)(?:/|:)(?P<owner>\S*)/(?P<project_name>\S*)\.git") | ||
| return [str(item) for item in pattern.findall(url)[0]] | ||
|
|
||
|
|
||
| def _encrypt_value(value: str) -> str: | ||
| """Encrypt a string, and then return the encrypted value as a string.""" | ||
| return str(uuid5(NAMESPACE_URL, value)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """ | ||
| Module for testing the project_metadata.py methods. | ||
| """ | ||
|
|
||
| from subprocess import CompletedProcess, CalledProcessError | ||
| from uuid import uuid5, NAMESPACE_URL | ||
| from unittest.mock import patch, Mock | ||
| from unittest import TestCase | ||
|
|
||
| from parameterized import parameterized | ||
|
|
||
| from samcli.lib.telemetry.project_metadata import get_git_remote_origin_url, get_project_name, get_initial_commit_hash | ||
|
|
||
|
|
||
| class TestProjectMetadata(TestCase): | ||
| def setUp(self): | ||
| self.gc_mock = Mock() | ||
| self.global_config_patcher = patch("samcli.lib.telemetry.project_metadata.GlobalConfig", self.gc_mock) | ||
| self.global_config_patcher.start() | ||
| self.gc_mock.return_value.telemetry_enabled = True | ||
|
|
||
| def tearDown(self): | ||
| self.global_config_patcher.stop() | ||
|
|
||
| def test_return_none_when_telemetry_disabled(self): | ||
| self.gc_mock.return_value.telemetry_enabled = False | ||
|
|
||
| git_origin = get_git_remote_origin_url() | ||
| self.assertIsNone(git_origin) | ||
|
|
||
| project_name = get_project_name() | ||
| self.assertIsNone(project_name) | ||
|
|
||
| initial_commit = get_initial_commit_hash() | ||
| self.assertIsNone(initial_commit) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("https://github.com/aws/aws-sam-cli.git\n", "github.com/aws/aws-sam-cli.git"), | ||
| ("http://github.com/aws/aws-sam-cli.git\n", "github.com/aws/aws-sam-cli.git"), | ||
| ("git@github.com:aws/aws-sam-cli.git\n", "github.com/aws/aws-sam-cli.git"), | ||
| ("https://github.com/aws/aws-cli.git\n", "github.com/aws/aws-cli.git"), | ||
| ("http://not.a.real.site.com/somebody/my-project.git", "not.a.real.site.com/somebody/my-project.git"), | ||
| ("git@not.github:person/my-project.git", "not.github/person/my-project.git"), | ||
| ] | ||
| ) | ||
| @patch("samcli.lib.telemetry.project_metadata.subprocess.run") | ||
| def test_retrieve_git_origin(self, origin, expected, sp_mock): | ||
| sp_mock.return_value = CompletedProcess(["git", "config", "--get", "remote.origin.url"], 0, stdout=origin) | ||
|
|
||
| git_origin = get_git_remote_origin_url() | ||
| self.assertEqual(git_origin, str(uuid5(NAMESPACE_URL, expected))) | ||
|
|
||
| @patch("samcli.lib.telemetry.project_metadata.subprocess.run") | ||
| def test_retrieve_git_origin_when_not_a_repo(self, sp_mock): | ||
| sp_mock.side_effect = CalledProcessError(128, ["git", "config", "--get", "remote.origin.url"]) | ||
|
|
||
| git_origin = get_git_remote_origin_url() | ||
| self.assertIsNone(git_origin) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("https://github.com/aws/aws-sam-cli.git\n", "aws-sam-cli"), | ||
| ("https://github.com/aws/aws-sam-cli.git\n", "aws-sam-cli"), | ||
| ("git@github.com:aws/aws-sam-cli.git\n", "aws-sam-cli"), | ||
| ("https://github.com/aws/aws-cli.git\n", "aws-cli"), | ||
| ("http://not.a.real.site.com/somebody/my-project.git", "my-project"), | ||
| ("git@not.github:person/my-project.git", "my-project"), | ||
| ] | ||
| ) | ||
| @patch("samcli.lib.telemetry.project_metadata.subprocess.run") | ||
| def test_retrieve_project_name_from_git(self, origin, expected, sp_mock): | ||
| sp_mock.return_value = CompletedProcess(["git", "config", "--get", "remote.origin.url"], 0, stdout=origin) | ||
|
|
||
| project_name = get_project_name() | ||
| self.assertEqual(project_name, str(uuid5(NAMESPACE_URL, expected))) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("C:/Users/aws/path/to/library/aws-sam-cli", "aws-sam-cli"), | ||
| ("C:\\Users\\aws\\Windows\\path\\aws-sam-cli", "aws-sam-cli"), | ||
| ("C:/", ""), | ||
| ("C:\\", ""), | ||
| ("E:/path/to/another/dir", "dir"), | ||
| ("This/one/doesn't/start/with/a/letter", "letter"), | ||
| ("/banana", "banana"), | ||
| ("D:/one/more/just/to/be/safe", "safe"), | ||
| ] | ||
| ) | ||
| @patch("samcli.lib.telemetry.project_metadata.getcwd") | ||
| @patch("samcli.lib.telemetry.project_metadata.subprocess.run") | ||
| def test_retrieve_project_name_from_dir(self, cwd, expected, sp_mock, cwd_mock): | ||
| sp_mock.side_effect = CalledProcessError(128, ["git", "config", "--get", "remote.origin.url"]) | ||
| cwd_mock.return_value = cwd | ||
|
|
||
| project_name = get_project_name() | ||
| self.assertEqual(project_name, str(uuid5(NAMESPACE_URL, expected))) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("0000000000000000000000000000000000000000"), | ||
| ("0123456789abcdef0123456789abcdef01234567"), | ||
| ("abababababababababababababababababababab"), | ||
| ] | ||
| ) | ||
| @patch("samcli.lib.telemetry.project_metadata.subprocess.run") | ||
| def test_retrieve_initial_commit(self, git_hash, sp_mock): | ||
| sp_mock.return_value = CompletedProcess(["git", "rev-list", "--max-parents=0", "HEAD"], 0, stdout=git_hash) | ||
|
|
||
| initial_commit = get_initial_commit_hash() | ||
| self.assertEqual(initial_commit, str(uuid5(NAMESPACE_URL, git_hash))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.