能否通过Gerrit REST API获取特定变更的dependsOn与neededBy属性?
dependsOn and neededBy properties? Absolutely! You don’t need to rely on the Gerrit SSH command to fetch dependency information—the REST API fully supports getting both dependsOn and neededBy relationships for a specific change. Here’s how to implement it:
1. Use the Change Detail Endpoint
The most direct approach is to call the /changes/{change-id}/detail endpoint, which returns comprehensive details about a change, including its dependencies. Since these fields aren’t included in the default response, you’ll need to explicitly request them via query parameters.
Example Request
Send a GET request with these parameters:
GET /changes/{change-id}/detail?o=CURRENT_REVISION&o=DEPENDENCIES
Replace {change-id} with your target change’s identifier—this can be the full hash (like I123456789abcdef123456789abcdef123456789a) or the project-branch-number format (e.g., my-project~main~123).
Response Breakdown
In the JSON output, navigate to the current_revision object (this maps to the --current-patch-set flag in your SSH command). The associated patch set will include two key arrays:
depends_on: Lists changes that the current change relies on (matches the SSH command’sdependsOnproperty)needed_by: Lists changes that depend on the current change (matches the SSH command’sneededByproperty)
Each entry in these arrays includes details like the dependency’s change ID, branch, subject, and basic status.
2. Alternative: Mirror the SSH Query Structure
If you want to replicate your original SSH query logic (change:$changeid AND branch:$branch), use the /changes/ query endpoint with matching filters plus the dependency flags:
GET /changes/?q=change:{change-id}+branch:{branch}&o=CURRENT_REVISION&o=DEPENDENCIES
This returns a list (with one entry for your target change) containing the same dependency fields as the detail endpoint.
Key Notes
- Permissions: Make sure you have read access to both the target change and any linked dependencies—Gerrit will omit data you don’t have permission to view.
- Parameter Alignment: The
o=CURRENT_REVISIONflag corresponds to--current-patch-setin SSH, ando=DEPENDENCIESmatches the--dependenciesflag.
内容的提问来源于stack exchange,提问作者Some programmer dude

