Branch_commits data via api #78161
Replies: 4 comments 5 replies
-
import githubV3py
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--owner")
parser.add_argument("-r", "--repo")
parser.add_argument("-t", "--token")
parser.add_argument("-s", "--sha")
options = parser.parse_args()
ghc = githubV3py.GitHubClient(token=options.token)
commits = ghc.ReposListCommits(options.owner, options.repo, sha=options.sha)
return
if __name__ == '__main__':
main()
|
Beta Was this translation helpful? Give feedback.
-
There's a method SearchCommits that does what you want. Q&D Example:import githubV3py
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--owner")
parser.add_argument("-r", "--repo")
parser.add_argument("-t", "--token")
parser.add_argument("-s", "--sha")
options = parser.parse_args()
ghc = githubV3py.GitHubClient(token=options.token)
commits = ghc.SearchCommits(f"hash:{options.sha}")
print(f"Commit {options.sha}: {commits.items[0].commit.message}")
return
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
-
Tried it with the Exemplary script#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
# https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# ======================= description ========================
# This script retrieves data about a specific commit from a GitHub repository, including branches
# and tags containing the commit, and associated pull requests from the default branch. It uses the
# GitHub GraphQL API, and the gh and jq command line tools. The script fetches a list of
# refs, and compares the commit to each ref.
# Ref: https://github.com/orgs/community/discussions/78161
# ======================= variables ========================
owner_repo="${1:-"elastic"}"
name_repo="${2:-"kibana"}"
commit_compare="${3:-"315fdfc8fa97641ab4fcd78ed57e7e5ed8198699"}"
refs_prefix="${4:-"refs/tags/"}"
# Create temporary files
tmp_json=$(mktemp)
result_PR_json=$(mktemp)
result_tags_heads_json=$(mktemp)
# =================== helper functions ====================
# Use trap command to ensure the temporary file is removed when script exits
trap 'rm -f "$tmp_json" "$result_tags_heads_json" "$result_PR_json"' EXIT SIGHUP SIGINT
run_query() {
gh api graphql \
--cache=15m --paginate \
--header 'X-Github-Next-Global-ID:1' \
--raw-field owner_repo="$owner_repo" \
--raw-field name_repo="$name_repo" \
--raw-field commit="$commit_compare" \
--raw-field refs_prefix="$refs_prefix" \
"$@"
}
die() {
echo ERROR: "$*" >&2
exit 1
}
# =================== check requirements ====================
if [ ${#} -gt 4 ]; then
die "Too many arguments: max is 4"
fi
for tool in gh jq; do
if ! type -p $tool >/dev/null; then
die "'$tool' was not found."
fi
done
if ! gh api graphql -f query='query { viewer { login }}' >/dev/null 2>&1; then
die "Unable to make GraphQL queries with 'gh' tool. Make sure you are authenticated."
fi
# ===================== let's begin ========================
while IFS='' read -r line; do
array_ref_id+=("$line")
done < <(run_query --raw-field query=$'
query ($owner_repo: String!, $name_repo: String!, $refs_prefix: String!, $endCursor: String) {
repository(owner: $owner_repo, name: $name_repo) {
refs(first: 100, refPrefix: $refs_prefix, after: $endCursor, direction:DESC) {
totalCount
pageInfo {
hasNextPage
endCursor
}
nodes {
id
}
}
}
}
' --jq '.data.repository.refs.nodes[] | "--raw-field=ref_ids[]=\(.id)"')
run_query --raw-field query=$'
query ($owner_repo: String!, $name_repo: String!, $commit: GitObjectID!) {
repository(owner: $owner_repo, name: $name_repo) {
object(oid: $commit) {
... on Commit {
associatedPullRequests(first: 100) {
nodes {
number
}
}
}
}
defaultBranchRef {
name
prefix
}
}
}' --jq '.data.repository' >"$result_PR_json"
# We can only pass 100 items at a time to the query.
for ((i = 0; i < ${#array_ref_id[@]}; i += 100)); do
chunk=("${array_ref_id[@]:i:100}")
run_query "${chunk[@]}" \
--raw-field query=$'
query ($ref_ids: [ID!]!, $commit: String!) {
nodes(ids: $ref_ids) {
... on Ref {
name
prefix
compare(headRef: $commit) {
status
}
}
}
}'
done >"$tmp_json"
# The results of multiple calls aren't properly merged; we have to do it ourselves.
# Ref: https://github.com/cli/cli/pull/5652
jq --slurp '[.[].data.nodes] | add' "$tmp_json" >"$result_tags_heads_json"
jq --raw-output '
"DefaultBranch:", [.defaultBranchRef.name],
"Associated PullRequests:", [.object.associatedPullRequests.nodes[].number]' "$result_PR_json"
jq --raw-output '
[.[] | select(.compare.status | test("BEHIND|IDENTICAL"))] | group_by(.prefix)[] |
if .[0].prefix == "refs/heads/" then
"Branches:", (map(.name))
elif .[0].prefix == "refs/tags/" then
"Tags:", (map(.name))
else
"Others:", (map(.name))
end' "$result_tags_heads_json" Exemplary usage# save the contents in a file, and make it executable
chmod +x branch_commits.sh
# run it
# default values are 'elastic' 'kibana' '315fdfc8fa97641ab4fcd78ed57e7e5ed8198699' 'refs/tags/'
./branch_commits.sh
# result takes ~10s result 1DefaultBranch:
[
"main"
]
Associated PullRequests:
[
150359
]
Tags:
[
"v8.11.3",
"v8.11.2",
"v8.11.1",
"v8.11.0",
"v8.10.4",
"v8.10.3",
"v8.10.2",
"v8.10.1",
"v8.10.0",
"v8.9.2",
"v8.9.1",
"v8.9.0",
"v8.8.2",
"v8.8.1",
"v8.8.0",
"v8.7.1",
"v8.7.0",
"deploy-fix@1701687168",
"deploy-fix@1699260155",
"deploy-fix@1698657637",
"deploy@1704089101",
"deploy@1703484304",
"deploy@1702903357",
"deploy@1702879551",
"deploy@1702367069",
"deploy@1702284899",
"deploy@1701687168",
"deploy@1701160888",
"deploy@1700491293",
"deploy@1699865290",
"deploy@1699260155",
"deploy@1698657637",
"deploy@1698046713",
"deploy@1697564183",
"deploy@1697232175",
"deploy@1697028216",
"deploy@1696873111",
"deploy@1696618725",
"deploy@1696508231",
"deploy@1696415195",
"deploy@1696328885",
"deploy@1695286747",
"deploy@1694683198",
"deploy@1694506029",
"deploy@1694162455",
"deploy@1694087994",
"deploy@1693866333",
"deploy@1693860790",
"deploy@1693853982",
"deploy@1693609987",
"deploy@1693594780"
] To see not just the tags but also all the branches with the commit, run it with "refs/". ./branch_commits.sh elastic kibana 315fdfc8fa97641ab4fcd78ed57e7e5ed8198699 "refs/" result 2DefaultBranch:
[
"main"
]
Associated PullRequests:
[
150359
]
Branches:
[
"8.12",
"8.11.0-rn",
"8.11.0-1",
"8.11",
"8.10.0-1",
"8.10",
"8.9.1-rn",
"8.9",
"8.8",
"8.7",
"zdt-dynamic-batch-size",
"yomduf-patch-1",
"worker-1644139",
"versioned-router-and-oas",
"validate-es-privilege-overhead",
"usage_collection/apply-concurrency-limits",
"update-performance-tests",
"update-k8s-templates-20231228022840",
"update-k8s-templates-20231225023052",
"update-k8s-templates-20231224011133",
"update-k8s-templates-20231223011300",
"update-k8s-templates-20231222213432",
"update-k8s-templates-20231207114421",
"update-k8s-templates-20231120165246",
"update-k8s-templates-20231115214142",
"update-k8s-templates-20231114150308",
"update-k8s-templates-20231114120207",
"update-k8s-templates-20231113233020",
"update-k8s-templates-20231113181210",
"update-k8s-templates-20231108191535",
"update-k8s-templates-20231106095814",
"update-k8s-templates-20231103162509",
"update-k8s-templates-20231016165511",
"update-k8s-templates-20231012152118",
"update-k8s-templates-20230918084257",
"update-k8s-templates-20230914210013",
"update-k8s-templates-20230914200215",
"update-k8s-templates-20230904143009",
"update-k8s-templates-20230901132310",
"update-k8s-templates-20230803175021",
"update-k8s-templates-20230725210419",
"update-k8s-templates-20230725201638",
"update-k8s-templates-20230720084550",
"update-k8s-templates-20230719215947",
"update-k8s-templates-20230717135608",
"update-k8s-templates-20230706191850",
"update-k8s-templates-20230706155557",
"update-k8s-templates-20230706154855",
"update-k8s-templates-20230613215952",
"update-k8s-templates-20230613163838",
"update-k8s-templates-20230508201146",
"update-k8s-templates-20230424210028",
"update-k8s-templates-20230420075920",
"update-k8s-templates-20230419140609",
"update-k8s-templates-20230413125845",
"update-k8s-templates-20230328135933",
"update-k8s-templates-20230328132953",
"update-k8s-templates-20230323034922",
"update-k8s-templates-20230323024622",
"update-k8s-templates-20230321171328",
"update-k8s-templates-20230307223227",
"update-cloud-defend-policy-schema-20230817081504",
"update-cloud-defend-policy-schema-20230816203616",
"update-cloud-defend-policy-schema-20230816080857",
"update-cloud-defend-policy-schema-20230814152246",
"update-cloud-defend-policy-schema-20230710080940",
"update-cloud-defend-policy-schema-20230709080636",
"update-cloud-defend-policy-schema-20230708081129",
"update-cloud-defend-policy-schema-20230506021232",
"update-cloud-defend-policy-schema-20230417220724",
"update-cloud-defend-policy-schema-20230330224153",
"update-cloud-defend-policy-schema-20230324195608",
"update-cloud-defend-policy-schema-20230324181243",
"update-cloud-defend-policy-schema-20230324180140",
"update-cloud-defend-policy-schema-20230312080817",
"update-cloud-defend-policy-schema-20230311081026",
"update-cloud-defend-policy-schema-20230311004853",
"update-cloud-defend-policy-schema-20230310081012",
"update-cloud-defend-policy-schema-20230309080629",
"update-cloud-defend-policy-schema-20230307011014",
"update-cloud-defend-policy-schema-20230228031115",
"update-cloud-defend-policy-schema-20230228030937",
"update-cloud-defend-policy-schema-20230228030359",
"update-cloud-defend-policy-schema-20230228001241",
"update-cloud-defend-policy-schema-20230227232821",
"update-cloud-defend-policy-schema-20230227232626",
"update-cloud-defend-policy-schema-20230227225312",
"update-cloud-defend-policy-schema-20230227224143",
"update-cloud-defend-policy-schema-20230227223622",
"update-cloud-defend-policy-schema-20230227221352",
"update-cloud-defend-policy-schema-20230227220725",
"update-cloud-defend-policy-schema-20230227215415",
"update-cloud-defend-policy-schema-20230227215049",
"update-cloud-defend-policy-schema-20230227204745",
"update-cloud-defend-policy-schema-20230227204701",
"update-cloud-defend-policy-schema-20230227204609",
"update-cloud-defend-policy-schema-20230227204113",
"update-cloud-defend-policy-schema-20230227202850",
"update-cloud-defend-policy-schema-20230227192303",
"update-cloud-defend-policy-schema-20230227192115",
"update-bundled-packages-20231024230602",
"update-bundled-packages-20231011122251",
"update-bundled-packages-20231004180931",
"update-bundled-packages-20230920203345",
"update-bundled-packages-20230904181651",
"update-bundled-packages-20230727131356",
"update-bundled-packages-20230719151804",
"update-bundled-packages-20230627094113",
"update-bundled-packages-20230601221447",
"update-bundled-packages-20230512032132",
"update-bundled-packages-20230512031251",
"update-bundled-packages-20230428165625",
"update-bundled-packages-20230213162650",
"unskipping-ransomware",
"unskip-home-page-test",
"ui-actions-refactor",
"testing-quality-gate",
"test-remove-migrationVersion-mapping",
"test-on-merge",
"test-ironbank-context",
"test-fix-docker-context",
"test-add-tags-listing-page-journey",
"tedious-wiki-edit",
"tailorzed-patch-2",
"system_actions_mvp",
"slo_group_feature",
"slo/feature-branch",
"shareable-dashboards",
"serverless-poc",
"serverless-es-impact",
"serverless/poc/main",
"security_solution_lazy_routes",
"security/entity-analytics",
"security/dkirchan-rp-integration",
"security/dkirchan-refactor-security-solution-buildkite",
"security/dkirchan-periodic-qg-integration",
"security/dkirchan-buildkite-analytics-integration",
"security/dkicrhan-report-bk-analytics-second",
"security/dkicrhan-report-bk-analytics",
"security/EDR-integration",
"seanstory/4333-have-create-pipeline-pass-along-field-mappings",
"seanstory/fix-double-ml.inference-prefix",
"scroll-count-failure",
"saml-cypress-integration",
"rylnd/ea_perf_10k_inputs",
"rylnd/ea_perf_top_5_inputs",
"rylnd/ea_perf_no_top_inputs",
"rm-hide-so-apis",
"risk_score_api_8.7",
"revert-172182-fix-get-bundled-file",
"revert-170808-bug/task-manager-validation-failures",
"revert-162149-TinaHeiligers-patch-3",
"revert-161669-refreshInterval",
"revert-151020-task/dw-action-list-api-with-optional-outputs-5946",
"resolve-import-api",
"renovate/main-xstate",
"renovate/main-typescript-eslint",
"renovate/main-tty-output",
"renovate/main-security-solution-modules",
"renovate/main-scss",
"renovate/main-redux",
"renovate/main-react-query",
"renovate/main-react-hook-form",
"renovate/main-profiling",
"renovate/main-opentelemetry-modules",
"renovate/main-jest",
"renovate/main-ftr",
"renovate/main-cypress",
"renovate/main-cloud-defend",
"renovate/main-babel",
"renovate/main-apm",
"renovate/main-@testing-library",
"refresh-false",
"refactor-n-svcs",
"rayafratkina-patch-1",
"profiling/fleet-permissions-indices",
"prebuilt-detection-rules-8.10.1-beta-1-cypress-test",
"prebuilt-detection-rules-8.9.4-beta-1-cypress-test",
"prebuilt-detection-rules-8.9.3-beta-1-cypress-test",
"prebuilt-detection-rules-8.8.9-beta-1-cypress-test",
"prebuilt-detection-rules-8.8.8-beta-1-cypress-test",
"prebuilt-detection-rules-8.8.4-beta-cypress-test",
"prebuilt-detection-rules-8.8.3-beta-cypress-test",
"prebuilt-detection-rules-8.8.2-beta-cypress-test",
"prebuilt-detection-rules-8.8.1-beta-cypress-test",
"prebuilt-detection-rules-8.7.11-beta-1-cypress-test",
"prebuilt-detection-rules-8.7.10-beta-cypress-test",
"prebuilt-detection-rules-8.7.9-beta-cypress-test",
"prebuilt-detection-rules-8.7.9-beta-1-cypress-test",
"prebuilt-detection-rules-8.7.7-beta-cypress-test",
"prebuilt-detection-rules-8.7.6-beta-cypress-test",
"prebuilt-detection-rules-8.7.5-beta-cypress-test",
"prebuilt-detection-rules-8.7.4-beta-cypress-test",
"prebuilt-detection-rules-8.7.2-beta-cypress-test",
"prebuilt-detection-rules-8.7.1-beta-cypress-test",
"popover-and-beacon-over-flyout",
"poc-ec-circuit-breaker",
"poc-conditional-refresh",
"philippkahr-patch-2",
"philippkahr-patch-1",
"perf-journeys/keep-ES-data-after-warmup",
"parallel-serverless-adjustment",
"packages/kbn-test/src/kbn/users.ts",
"osquery_drivers",
"onweek-synthtrace-ui",
"on_week/cloud_collaboration",
"obs-ai-assistant",
"mobile-ui-crash-widget",
"merge-queue-trial",
"markwalkom-data-view-runtimefield-clarification",
"main",
"logstash-management-tooltip",
"limit-pit-finder-collection",
"kibana-serverless-pipeline",
"kibana_data_scenario",
"kibana_context",
"kibana_context_test",
"jsevidal13-patch-2",
"journeys/switch-to-es-trial-license",
"investigate-node-test-runner",
"ingest-ftr-serverless-tests",
"green-execution",
"glo-playing",
"gh-readonly-queue/main/pr-170696-550b3cf08d5e618a9a03262cb4f216ec83126704",
"gh-readonly-queue/main/pr-170594-873a31c7e5e799a926cc12d05f3ed3661de4956c",
"ftr-config-try-flaky-test-runner",
"from-pmuellers-pr",
"flo-up-k8-integration-manifest",
"flo-up-debug-collector",
"flo-cloud-image-build",
"flo-ci-testing",
"flaky-after-split",
"fix-upgrade-snapshot",
"fix-release-helper-issues",
"fix-java-snippets",
"fix-fleet-8.11.1-release-notes",
"fix/synthetics-quote-monitor-name",
"features/poc-code-own-rules",
"feature-esql-backup",
"feature-esql-1",
"feat/obs-asset-manager-demo",
"feat/asset-manager_implicit-collection",
"extract_data_quality_helpers",
"expr/new_timeline_modal_discover_table",
"export-type-package",
"expandable-flyout-static-scope-id",
"enable-jest-coverage-on-prs",
"emrcbrn-patch-1",
"e2e/c77175-alert-table-controls",
"e2e/C77149-threat-indicator-alerts.cy.ts",
"doc_fix",
"dmitrii/4686-support-new-scheduling-format",
"dkirchan/unified-reporting",
"dev-tool-failover-doc",
"deploy-fix@1701687168",
"deploy-fix@1699260155",
"deploy-fix@1698657637",
"deno-alerting-rule",
"data-views-allowNoIndex-default-true",
"data_quality_stack_rule",
"dashboard-ftr",
"dark-mode/detect-issues",
"cypress-mki",
"custom-fields-default-values",
"csp-quick-nav",
"close-file-preview-with-escape",
"chore/cypress-data-session-followup",
"chore/add-cypress-data-session",
"cc-flaky-jest-integration-test",
"cc-cheaper-agent",
"case_action",
"build/rename-serverless",
"build/fix-chromium-install",
"bugfix-6992",
"bugfix-6992-revisited",
"bturquet-remove-functionbeat-link",
"brad/restrict-draft-bot",
"before-dot-kibana-split",
"backport/8.11/pr-171789",
"backport/8.11/pr-168720",
"backport/8.9/pr-161533",
"backport/8.8/pr-159432",
"backport/8.8/pr-155693",
"asset",
"asse",
"apm-test-plan-cluster-8.7",
"apm-plugin-description",
"apm-observability-logs-onboarding",
"apm-index-settings-dcs",
"api-test-overflow-count",
"aiops/related-events-feature",
"add-snapshots-to-service-map-e2e",
"add-on-merge-gh-labels",
"Terilia-patch-1",
"ReportingDocAdjustment",
"LucaWintergerst-patch-1",
"JeffHuss-patch-1",
"JasonStoltz-patch-1"
]
Tags:
[
"v8.11.3",
"v8.11.2",
"v8.11.1",
"v8.11.0",
"v8.10.4",
"v8.10.3",
"v8.10.2",
"v8.10.1",
"v8.10.0",
"v8.9.2",
"v8.9.1",
"v8.9.0",
"v8.8.2",
"v8.8.1",
"v8.8.0",
"v8.7.1",
"v8.7.0",
"deploy-fix@1701687168",
"deploy-fix@1699260155",
"deploy-fix@1698657637",
"deploy@1704089101",
"deploy@1703484304",
"deploy@1702903357",
"deploy@1702879551",
"deploy@1702367069",
"deploy@1702284899",
"deploy@1701687168",
"deploy@1701160888",
"deploy@1700491293",
"deploy@1699865290",
"deploy@1699260155",
"deploy@1698657637",
"deploy@1698046713",
"deploy@1697564183",
"deploy@1697232175",
"deploy@1697028216",
"deploy@1696873111",
"deploy@1696618725",
"deploy@1696508231",
"deploy@1696415195",
"deploy@1696328885",
"deploy@1695286747",
"deploy@1694683198",
"deploy@1694506029",
"deploy@1694162455",
"deploy@1694087994",
"deploy@1693866333",
"deploy@1693860790",
"deploy@1693853982",
"deploy@1693609987",
"deploy@1693594780"
]
---
|
Beta Was this translation helpful? Give feedback.
-
🕒 Discussion Activity Reminder 🕒 This Discussion has been labeled as dormant by an automated system for having no activity in the last 60 days. Please consider one the following actions: 1️⃣ Close as Out of Date: If the topic is no longer relevant, close the Discussion as 2️⃣ Provide More Information: Share additional details or context — or let the community know if you've found a solution on your own. 3️⃣ Mark a Reply as Answer: If your question has been answered by a reply, mark the most helpful reply as the solution. Note: This dormant notification will only apply to Discussions with the Thank you for helping bring this Discussion to a resolution! 💬 |
Beta Was this translation helpful? Give feedback.
-
Select Topic Area
Question
Body
Hello, is there any way to get via API (can be graphql/search/anything) the same data that is returned by calling
github.com/:owner/:repo/branch_commits/:sha/
?I'd like to call API(even multiple times) instead of scapring above endopoint that returns HTML, or cloning repo localy and calling
git branch --contains (...)
.Example: https://github.com/elastic/kibana/branch_commits/315fdfc8fa97641ab4fcd78ed57e7e5ed8198699
Beta Was this translation helpful? Give feedback.
All reactions