Use this guide to learn how to find the total lines of code (LoC) for a PR. Use this data to find your average or median LoC for PRs.
Because LoC is associated with commits, which are then associated with PRs, you must use both the Commits and Pull requests endpoints to find this information.
Grab the PRs
This first API call is our main source of information for all the other API calls in this guide. The below call returns all merged PRs within the START_DATE
and END_DATE
you designate.
https://HOSTNAME/v3/customer/core/pull_requests/?closed_at__gte=START_DATE&closed_at__lte=END_DATE&fields=id,project_id,commit_shas,created_at,created_by_id,title,closed_at,state&state=merged&group_by[created_by_id]
-
HOSTNAME:
is the IP address or URL for the Flow application. If you’re using the cloud instance of Flow, this isflow.pluralsight.com
. If you’re using Flow Enterprise Server, this is something other thanflow.pluralsight.com
. -
START_DATE/END_DATE
: Select your start and end date.
This API request gives you, by PR, the following fields grouped by PR author:
- PR ID: id
- Project id: Project id.
- Commit SHAs: associated commits shas for each PR
- Created at: date at which the PR was created
- Created by id: Alias who created the PR
- PR Title: Title of PR
- Closed at: date at which the PR was closed
- State: state of PR, this should be merged since that is what we are filtering for
If you want to include merged and closed PRs, remove the filter &state=merged
and the API call will automatically include any PR that were merged or closed during your date range.
You can export this data to a CSV file by adding &format=csv
at the end of the query.
Note: If you want to associate these PRs with their apex users in Flow, go to the next step on finding user information. Otherwise, skip to the last step of calculating the LoC for each PR.
Grab the user information
Now you can map the created_by_id
to the Flow apex user. Collect a comma separated list of the created_by_id’s
from the previous step and make an API call using the user_alias
endpoint to get user information.
https://HOSTNAME/v3/customer/core/user_alias/?id__in=YOUR_USER_ID_LIST&fields=id,name,apex_user
-
YOUR_USER_ID_LIST:
This is the list ofcreated_by_ids
from the previous step. They should be in a comma separated list.
The API call returns a consolidated list of all the users with their name and apex_user_id which you can now map to your original list of PR created_by_ids
.
LoC by PR
There are two ways you can grab the commit-level data associated with your PRs. You need to do this for each of your PRs, so writing a script may be in your best interest.
Use the commit_shas
list to find LoC
Take all the commit_shas
associated with a PR from the first step and place them in the following API call:
https://HOSTNAME/v3/customer/core/commits.agg/?hexsha__in=YOUR_COMMIT_SHA_LIST&aggregate[sum]=haloc
-
YOUR_COMMIT_SHA_LIST
: A comma-separated list of commit shas for a PR from the first step.
This call aggregates the total LoC (HALOC) across all commit_shas
used in the API call.
Your results will look something like:
<count>1</count>
<next/><previous/>
<results>
<list-item>
<haloc_sum>19</haloc_sum>
</list-item></results>
<records>1</records>
haloc_sum is the total LoC for the PR.
Repeat this step for each PR you want to calculate LoC for, then use these numbers to find the average or median for all PRs as desired.
Use the PR id to find LoC
Use the PR id for each PR from the first step to identify associated commits and aggregate their LoC (HALOC).
To aggregate the LoC for all commits associated with a PR, use the API call:
https://HOSTNAME/v3/customer/core/commits.agg/?pull_request_id=PULL_REQUEST_ID&aggregate[sum]=haloc
-
PULL_REQUEST_ID
: The id for the PR from the first step.
If you want to see the LoC for each commit in the PR, use the following API call:
https://HOSTNAME/v3/customer/core/commits/?pull_request_id=PULL_REQUEST_ID&fields=hexsha,haloc,id
Repeat this for each PR you want to calculate LoC for, then use these numbers to find the average or median for all PRs as desired.