Skip to content

Commit

Permalink
Merge pull request #668 from mfocko/get-sha-from-branch
Browse files Browse the repository at this point in the history
Implement getter of SHA for branches

Required for passing commit hashes from service to testing farm for
merging of tests that are run on their side or usage in tmt preparation.
Signed-off-by: Matej Focko [email protected]
TODO:

 tests
 consider shared method get_sha_from(branch=None, tag=None) -> Optional[str]



We have introduced a new function into ogr that allows you to get commit SHA of the HEAD of the branch.

Reviewed-by: Jiri Popelka <None>
Reviewed-by: Tomas Tomecek <[email protected]>
  • Loading branch information
2 parents 14a14f2 + 685904e commit a8455da
Show file tree
Hide file tree
Showing 13 changed files with 1,584 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,13 @@ def get_web_url(self) -> str:
"""
raise NotImplementedError()

def get_sha_from_branch(self, branch: str) -> Optional[str]:
"""
Returns:
Commit SHA of head of the branch. `None` if no branch was found.
"""
raise NotImplementedError()


class GitUser(OgrAbstractClass):
"""
Expand Down
8 changes: 8 additions & 0 deletions ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,11 @@ def get_web_url(self) -> str:

def get_tags(self) -> List["GitTag"]:
return [GitTag(tag.name, tag.commit.sha) for tag in self.github_repo.get_tags()]

def get_sha_from_branch(self, branch: str) -> Optional[str]:
try:
return self.github_repo.get_branch(branch).commit.sha
except GithubException as ex:
if ex.status == 404:
return None
raise GithubAPIException from ex
9 changes: 9 additions & 0 deletions ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Optional, Dict, Set, Union

import gitlab
from gitlab.exceptions import GitlabGetError
from gitlab.v4.objects import Project as GitlabObjectsProject

from ogr.abstract import (
Expand Down Expand Up @@ -499,3 +500,11 @@ def _release_from_gitlab_object(

def get_web_url(self) -> str:
return self.gitlab_repo.web_url

def get_sha_from_branch(self, branch: str) -> Optional[str]:
try:
return self.gitlab_repo.branches.get(branch).attributes["commit"]["id"]
except GitlabGetError as ex:
if ex.response_code == 404:
return None
raise GitlabAPIException from ex
7 changes: 7 additions & 0 deletions ogr/services/pagure/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,10 @@ def get_files(
paths = filter_paths(paths, filter_regex)

return paths

def get_sha_from_branch(self, branch: str) -> Optional[str]:
branches = self._call_project_api(
"git", "branches", params={"with_commits": True}
)["branches"]

return branches.get(branch)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
_requre:
DataTypes: 1
key_strategy: StorageKeysInspectSimple
version_storage_file: 3
requests.sessions:
send:
GET:
https://api.github.com:443/repos/packit/hello-world:
- metadata:
latency: 0.31723451614379883
module_call_list:
- unittest.case
- requre.record_and_replace
- tests.integration.github.test_generic_commands
- ogr.abstract
- ogr.services.github.project
- github.MainClass
- github.Requester
- requests.sessions
- requre.objects
- requre.cassette
- requests.sessions
- send
output:
__store_indicator: 2
_content:
allow_auto_merge: false
allow_forking: true
allow_merge_commit: true
allow_rebase_merge: true
allow_squash_merge: true
allow_update_branch: false
archive_url: https://api.github.com/repos/packit/hello-world/{archive_format}{/ref}
archived: false
assignees_url: https://api.github.com/repos/packit/hello-world/assignees{/user}
blobs_url: https://api.github.com/repos/packit/hello-world/git/blobs{/sha}
branches_url: https://api.github.com/repos/packit/hello-world/branches{/branch}
clone_url: https://github.com/packit/hello-world.git
collaborators_url: https://api.github.com/repos/packit/hello-world/collaborators{/collaborator}
comments_url: https://api.github.com/repos/packit/hello-world/comments{/number}
commits_url: https://api.github.com/repos/packit/hello-world/commits{/sha}
compare_url: https://api.github.com/repos/packit/hello-world/compare/{base}...{head}
contents_url: https://api.github.com/repos/packit/hello-world/contents/{+path}
contributors_url: https://api.github.com/repos/packit/hello-world/contributors
created_at: '2019-05-02T18:54:46Z'
default_branch: main
delete_branch_on_merge: false
deployments_url: https://api.github.com/repos/packit/hello-world/deployments
description: The most progresive command-line tool in the world.
disabled: false
downloads_url: https://api.github.com/repos/packit/hello-world/downloads
events_url: https://api.github.com/repos/packit/hello-world/events
fork: false
forks: 20
forks_count: 20
forks_url: https://api.github.com/repos/packit/hello-world/forks
full_name: packit/hello-world
git_commits_url: https://api.github.com/repos/packit/hello-world/git/commits{/sha}
git_refs_url: https://api.github.com/repos/packit/hello-world/git/refs{/sha}
git_tags_url: https://api.github.com/repos/packit/hello-world/git/tags{/sha}
git_url: git://github.com/packit/hello-world.git
has_downloads: true
has_issues: true
has_pages: false
has_projects: true
has_wiki: true
homepage: null
hooks_url: https://api.github.com/repos/packit/hello-world/hooks
html_url: https://github.com/packit/hello-world
id: 184635124
is_template: false
issue_comment_url: https://api.github.com/repos/packit/hello-world/issues/comments{/number}
issue_events_url: https://api.github.com/repos/packit/hello-world/issues/events{/number}
issues_url: https://api.github.com/repos/packit/hello-world/issues{/number}
keys_url: https://api.github.com/repos/packit/hello-world/keys{/key_id}
labels_url: https://api.github.com/repos/packit/hello-world/labels{/name}
language: Python
languages_url: https://api.github.com/repos/packit/hello-world/languages
license:
key: mit
name: MIT License
node_id: MDc6TGljZW5zZTEz
spdx_id: MIT
url: https://api.github.com/licenses/mit
merges_url: https://api.github.com/repos/packit/hello-world/merges
milestones_url: https://api.github.com/repos/packit/hello-world/milestones{/number}
mirror_url: null
name: hello-world
network_count: 20
node_id: MDEwOlJlcG9zaXRvcnkxODQ2MzUxMjQ=
notifications_url: https://api.github.com/repos/packit/hello-world/notifications{?since,all,participating}
open_issues: 57
open_issues_count: 57
organization:
avatar_url: https://avatars.githubusercontent.com/u/46870917?v=4
events_url: https://api.github.com/users/packit/events{/privacy}
followers_url: https://api.github.com/users/packit/followers
following_url: https://api.github.com/users/packit/following{/other_user}
gists_url: https://api.github.com/users/packit/gists{/gist_id}
gravatar_id: ''
html_url: https://github.com/packit
id: 46870917
login: packit
node_id: MDEyOk9yZ2FuaXphdGlvbjQ2ODcwOTE3
organizations_url: https://api.github.com/users/packit/orgs
received_events_url: https://api.github.com/users/packit/received_events
repos_url: https://api.github.com/users/packit/repos
site_admin: false
starred_url: https://api.github.com/users/packit/starred{/owner}{/repo}
subscriptions_url: https://api.github.com/users/packit/subscriptions
type: Organization
url: https://api.github.com/users/packit
owner:
avatar_url: https://avatars.githubusercontent.com/u/46870917?v=4
events_url: https://api.github.com/users/packit/events{/privacy}
followers_url: https://api.github.com/users/packit/followers
following_url: https://api.github.com/users/packit/following{/other_user}
gists_url: https://api.github.com/users/packit/gists{/gist_id}
gravatar_id: ''
html_url: https://github.com/packit
id: 46870917
login: packit
node_id: MDEyOk9yZ2FuaXphdGlvbjQ2ODcwOTE3
organizations_url: https://api.github.com/users/packit/orgs
received_events_url: https://api.github.com/users/packit/received_events
repos_url: https://api.github.com/users/packit/repos
site_admin: false
starred_url: https://api.github.com/users/packit/starred{/owner}{/repo}
subscriptions_url: https://api.github.com/users/packit/subscriptions
type: Organization
url: https://api.github.com/users/packit
permissions:
admin: true
maintain: true
pull: true
push: true
triage: true
private: false
pulls_url: https://api.github.com/repos/packit/hello-world/pulls{/number}
pushed_at: '2022-01-03T06:50:35Z'
releases_url: https://api.github.com/repos/packit/hello-world/releases{/id}
size: 58
ssh_url: [email protected]:packit/hello-world.git
stargazers_count: 4
stargazers_url: https://api.github.com/repos/packit/hello-world/stargazers
statuses_url: https://api.github.com/repos/packit/hello-world/statuses/{sha}
subscribers_count: 9
subscribers_url: https://api.github.com/repos/packit/hello-world/subscribers
subscription_url: https://api.github.com/repos/packit/hello-world/subscription
svn_url: https://github.com/packit/hello-world
tags_url: https://api.github.com/repos/packit/hello-world/tags
teams_url: https://api.github.com/repos/packit/hello-world/teams
temp_clone_token: ''
topics: []
trees_url: https://api.github.com/repos/packit/hello-world/git/trees{/sha}
updated_at: '2021-11-30T09:33:55Z'
url: https://api.github.com/repos/packit/hello-world
visibility: public
watchers: 4
watchers_count: 4
_next: null
elapsed: 0.315507
encoding: utf-8
headers:
Access-Control-Allow-Origin: '*'
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP,
X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource,
X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval,
X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Cache-Control: private, max-age=60, s-maxage=60
Content-Encoding: gzip
Content-Security-Policy: default-src 'none'
Content-Type: application/json; charset=utf-8
Date: Mon, 03 Jan 2022 11:57:10 GMT
ETag: W/"cad969bec821c8b7182c0f49a48de8b0fb5b0b1219bdd9e869c8e7d2900d72eb"
Last-Modified: Tue, 30 Nov 2021 09:33:55 GMT
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Server: GitHub.com
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Transfer-Encoding: chunked
Vary: Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept,
X-Requested-With
X-Accepted-OAuth-Scopes: repo
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-GitHub-Media-Type: github.v3; format=json
X-GitHub-Request-Id: C3CC:6210:A9ECCB6:ACB4237:61D2E496
X-OAuth-Scopes: admin:enterprise, admin:gpg_key, admin:org, admin:org_hook,
admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist,
notifications, repo, user, workflow, write:discussion, write:packages
X-RateLimit-Limit: '5000'
X-RateLimit-Remaining: '4995'
X-RateLimit-Reset: '1641214432'
X-RateLimit-Resource: core
X-RateLimit-Used: '5'
X-XSS-Protection: '0'
raw: !!binary ""
reason: OK
status_code: 200
https://api.github.com:443/repos/packit/hello-world/branches/non-existing:
- metadata:
latency: 0.19494342803955078
module_call_list:
- unittest.case
- requre.record_and_replace
- tests.integration.github.test_generic_commands
- ogr.abstract
- ogr.services.github.project
- github.Repository
- github.Requester
- requests.sessions
- requre.objects
- requre.cassette
- requests.sessions
- send
output:
__store_indicator: 2
_content:
documentation_url: https://docs.github.com/rest/reference/repos#get-a-branch
message: Branch not found
_next: null
elapsed: 0.192827
encoding: utf-8
headers:
Access-Control-Allow-Origin: '*'
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP,
X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource,
X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval,
X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Content-Encoding: gzip
Content-Security-Policy: default-src 'none'
Content-Type: application/json; charset=utf-8
Date: Mon, 03 Jan 2022 11:57:10 GMT
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Server: GitHub.com
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Transfer-Encoding: chunked
Vary: Accept-Encoding, Accept, X-Requested-With
X-Accepted-OAuth-Scopes: ''
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-GitHub-Media-Type: github.v3; format=json
X-GitHub-Request-Id: C3CC:6210:A9ECD1C:ACB4294:61D2E496
X-OAuth-Scopes: admin:enterprise, admin:gpg_key, admin:org, admin:org_hook,
admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist,
notifications, repo, user, workflow, write:discussion, write:packages
X-RateLimit-Limit: '5000'
X-RateLimit-Remaining: '4994'
X-RateLimit-Reset: '1641214432'
X-RateLimit-Resource: core
X-RateLimit-Used: '6'
X-XSS-Protection: '0'
raw: !!binary ""
reason: Not Found
status_code: 404
8 changes: 8 additions & 0 deletions tests/integration/github/test_generic_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def test_commit_flags(self):
assert isinstance(flags, list)
assert len(flags) == 0

def test_get_sha_from_branch(self):
commit_sha = self.hello_world_project.get_sha_from_branch("test-for-flock")
assert commit_sha and commit_sha.startswith("e2282f3")

def test_get_sha_from_branch_non_existing(self):
commit_sha = self.hello_world_project.get_sha_from_branch("non-existing")
assert commit_sha is None

def test_get_sha_from_tag(self):
assert (
self.ogr_project.get_sha_from_tag("0.0.1")
Expand Down
Loading

0 comments on commit a8455da

Please sign in to comment.