Skip to content
Snippets Groups Projects
Commit 5fbab8ac authored by Jayesh's avatar Jayesh :cat2:
Browse files

Code formatting and minor improvements

parent b0883eed
No related branches found
No related tags found
No related merge requests found
......@@ -16,29 +16,30 @@ from gql.transport.aiohttp import AIOHTTPTransport
# GraphQL API endpoint
API_URL = "https://archive.softwareheritage.org/graphql/"
def get_dir_latest(url, bearer_token):
# GraphQL query with parameters for origin URL and number of commits
query = gql("""
query = gql(
"""
query getOriginEntries($url: String!) {
origin(url: $url) {
url
latestSnapshot {
swhid
branches(first: 1, nameInclude: "HEAD") {
nodes {
name {
latestSnapshot {
swhid
branches(first: 1, nameInclude: "HEAD") {
nodes {
name {
text
}
target {
resolveChain {
text
}
target {
resolveChain {
text
}
node {
...on Revision {
node {
...on Revision {
swhid
directory {
swhid
directory {
swhid
}
}
}
}
......@@ -46,39 +47,38 @@ def get_dir_latest(url, bearer_token):
}
}
}
}
}
""")
"""
)
headers = {"Content-Type": "application/json"}
if (bearer_token):
if bearer_token:
headers["Authorization"] = "Bearer " + bearer_token
transport = AIOHTTPTransport(
url=API_URL,
headers=headers
)
transport = AIOHTTPTransport(url=API_URL, headers=headers)
client = Client(
transport=transport,
fetch_schema_from_transport=False,
)
response = client.execute(query, {"url": url})
# Extract the SWHIDs of the commits
branches = response["origin"]["latestSnapshot"]["branches"]["nodes"]
if len(branches) < 1:
print("Unable to identify the main branch for this origin")
return None
else:
directory = response["origin"]["latestSnapshot"]["branches"]["nodes"][0]["target"]["node"]["directory"]
dir_swhid = directory["swhid"]
snapshot = response["origin"]["latestSnapshot"]
snp_swhid = snapshot["swhid"]
revision = response["origin"]["latestSnapshot"]["branches"]["nodes"][0]["target"]["node"]
rev_swhid=revision["swhid"]
return (dir_swhid,rev_swhid,snp_swhid)
if len(branches) < 1 or branches[0]["target"]["node"] is None:
raise Exception("Unable to identify the main branch for this origin")
# Extract the SWHIDs of the commits
directory = branches[0]["target"]["node"]["directory"]
dir_swhid = directory["swhid"]
snapshot = response["origin"]["latestSnapshot"]
snp_swhid = snapshot["swhid"]
revision = branches[0]["target"]["node"]
rev_swhid = revision["swhid"]
# now return
return (dir_swhid, rev_swhid, snp_swhid)
# now return
@click.command()
@click.option('--url', prompt='Software origin URL', help='The URL of the software origin.')
@click.option(
"--url", prompt="Software origin URL", help="The URL of the software origin."
)
@click.option(
"-a",
"--swh-bearer-token",
......@@ -87,13 +87,18 @@ def get_dir_latest(url, bearer_token):
show_default=True,
help="bearer token to bypass SWH API rate limit",
)
@click.option('--nofqid',is_flag=True, help='Print core directory SWHID instead of fully qualified directory SWHID.')
def main(url,swh_bearer_token,nofqid):
d,r,s=get_dir_latest(url,swh_bearer_token)
@click.option(
"--nofqid",
is_flag=True,
help="Print core directory SWHID instead of fully qualified directory SWHID.",
)
def main(url, swh_bearer_token, nofqid):
d, r, s = get_dir_latest(url, swh_bearer_token)
if nofqid:
print(d)
else:
print(f"{d};origin={url};visit={s};anchor={r}")
if __name__ == '__main__':
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment