From 4f81f77848c9768bd831ef38b2d98590abe263f3 Mon Sep 17 00:00:00 2001 From: Brian Stinson Date: Aug 29 2019 12:39:53 +0000 Subject: [PATCH 1/4] accept a with_commits parameter on the branches api to resolve the HEAD commits Signed-off-by: Brian Stinson --- diff --git a/pagure/api/project.py b/pagure/api/project.py index e86652b..5cfa1c1 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -271,6 +271,17 @@ def api_git_branches(repo, username=None, namespace=None): GET /api/0/fork///git/branches GET /api/0/fork////git/branches + Parameters + ^^^^^^^^^^ + + +-----------------+----------+---------------+--------------------------+ + | Key | Type | Optionality | Description | + +=================+==========+===============+==========================+ + | ``with_commits``| string | Optional | | Include the commit hash| + | | | | corresponding to the | + | | | | HEAD of each branch | + +-----------------+----------+---------------+--------------------------+ + Sample response ^^^^^^^^^^^^^^^ @@ -281,10 +292,23 @@ def api_git_branches(repo, username=None, namespace=None): "branches": ["master", "dev"] } + { + "total_branches": 2, + "branches": { + "master": "16ae2a4df107658b52750063ae203f978cf02ff7", + "dev": "8351c460167a41defc393f5b6c1d51fe1b3b82b8" + } + } + """ + + with_commits = pagure.utils.is_true( + flask.request.values.get("with_commits", False) + ) + repo = _get_repo(repo, username, namespace) - branches = pagure.lib.git.get_git_branches(repo) + branches = pagure.lib.git.get_git_branches(repo, with_commits=with_commits) return flask.jsonify( {"total_branches": len(branches), "branches": branches} diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 30c927c..0339ca9 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -2450,13 +2450,25 @@ def reinit_git(project, repofolder): ) -def get_git_branches(project): +def get_git_branches(project, with_commits=False): """ Return a list of branches for the project :arg project: The Project instance to get the branches for """ repo_path = pagure.utils.get_repo_path(project) - repo_obj = pygit2.Repository(repo_path) - return repo_obj.listall_branches() + repo_obj = PagureRepo(repo_path) + + if with_commits: + branches = {} + + for branch in repo_obj.listall_branches(): + resolved_branch = repo_obj.lookup_branch(branch).resolve() + com = resolved_branch.peel() + if com: + branches[branch] = com.oid.hex + else: + branches = repo_obj.listall_branches() + + return branches def new_git_branch( From 444a8294d84cc6d550208060ea70283517cbe52d Mon Sep 17 00:00:00 2001 From: Julen Landa Alustiza Date: Aug 29 2019 12:39:53 +0000 Subject: [PATCH 2/4] Fix with_commits argument type --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 5cfa1c1..516bcf1 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -277,7 +277,7 @@ def api_git_branches(repo, username=None, namespace=None): +-----------------+----------+---------------+--------------------------+ | Key | Type | Optionality | Description | +=================+==========+===============+==========================+ - | ``with_commits``| string | Optional | | Include the commit hash| + | ``with_commits``| boolean | Optional | | Include the commit hash| | | | | corresponding to the | | | | | HEAD of each branch | +-----------------+----------+---------------+--------------------------+ From f499a7c4e602bf99fecaf2532d70bd386c270c2b Mon Sep 17 00:00:00 2001 From: Julen Landa Alustiza Date: Aug 29 2019 12:39:53 +0000 Subject: [PATCH 3/4] lib/git: add with_commits to get_git_branches doc --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 0339ca9..8b7e080 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -2453,6 +2453,7 @@ def reinit_git(project, repofolder): def get_git_branches(project, with_commits=False): """ Return a list of branches for the project :arg project: The Project instance to get the branches for + :arg with_commits: Whether we should return branch head commits or not """ repo_path = pagure.utils.get_repo_path(project) repo_obj = PagureRepo(repo_path) From 240f793ee58320d04f3fb85b12270246d0456bb7 Mon Sep 17 00:00:00 2001 From: Julen Landa Alustiza Date: Aug 29 2019 12:39:53 +0000 Subject: [PATCH 4/4] Unit test for api_git_branches with_commits --- diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index e330f5a..e522b32 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -148,6 +148,47 @@ class PagureFlaskApiProjecttests(tests.Modeltests): }, ) + def test_api_git_branches_with_commits(self): + """ Test the api_git_branches method of the flask api with with_commits=True. """ + # Create a git repo to add branches to + tests.create_projects(self.session) + repo_path = os.path.join(self.path, "repos", "test.git") + tests.add_content_git_repo(repo_path) + new_repo_path = tempfile.mkdtemp(prefix="pagure-api-git-branches-test") + clone_repo = pygit2.clone_repository(repo_path, new_repo_path) + + # Create two other branches based on master + for branch in ["pats-win-49", "pats-win-51"]: + clone_repo.create_branch(branch, clone_repo.head.peel()) + refname = "refs/heads/{0}:refs/heads/{0}".format(branch) + PagureRepo.push(clone_repo.remotes[0], refname) + + # Check that the branches show up on the API + output = self.app.get("/api/0/test/git/branches?with_commits=true") + # Delete the cloned git repo after the API call + shutil.rmtree(new_repo_path) + + # Get the commit hex + repo_obj = pygit2.Repository( + os.path.join(self.path, "repos", "test.git") + ) + commit = repo_obj[repo_obj.head.target] + + # Verify the API data + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + self.assertDictEqual( + data, + { + "branches": { + "master": commit.hex, + "pats-win-49": commit.hex, + "pats-win-51": commit.hex, + }, + "total_branches": 3, + }, + ) + def test_api_git_branches_empty_repo(self): """ Test the api_git_branches method of the flask api when the repo is empty.