github api to know work breakdown for repository - github

For our organization, we want to plot metrics of new work (new changes in code), and rework (changing existing code) happening across the repository. Is there any GitHub API to get that info?

Is there any GitHub API to get that info?
No direct native API.
You would need the GitHub commits API for that, which you can query using the GitHub CLI gh (to be installed first):
gh api \
-H "Accept: application/vnd.github+json" \
/repos/OWNER/REPO/commits
For each commit, you can get a commit, which will include:
gh api \
-H "Accept: application/vnd.github+json" \
/repos/OWNER/REPO/commits/REF
"stats": {
"additions": 104,
"deletions": 4,
"total": 108
},
"files": [
{
"filename": "file1.txt",
"additions": 10,
"deletions": 2,
"changes": 12,
...
That should be enough to determine if this is new work or rework.
Depending on the depth of the repository history, that might mean such a script could not scale if there is too many commit.
But it could work if you limit the analysis to a range of commits.

Related

How to get stats for Github PRs code reviews

I want to get a list of people who did some PR code reviews in my organization in the past month. Also how many code reviews were done by each one of them. It would also be helpful if I can know how many comments each person made. Is there a way to get these stats in UI without doing any code changes?
You have a few options including: the Github API or a proprietary service like PR Stats.
With the Github API you can use the following:
curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER/reviews
With PR Stats you'll need to create a Github Action to include a table of PR stats in each PR you create within Github.

Use Github action secrets on local machine - is it possible?

I know I can use curl to list the secrets of a repo via curl like so:
curl -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token [personal access token]" \
https://api.github.com/repos/[user]/[repo]/actions/secrets
This returns something like this:
{
"total_count": 1,
"secrets": [
{
"name": "A_TEST_SECRET",
"created_at": "2022-04-14T13:12:22Z",
"updated_at": "2022-04-14T13:12:22Z"
}
]
}
But is it somehow possible to also retrieve the secret's value?
My use case is this:
I have a step in a Github Actions workflow that uses a secret and needs to be executed before I can run my build step, which just contains a script to run.
Now sometimes I want to also run my buildscript on my local machine, but to do that I also need to run the step before it, which needs the secret. Is there any way I can retrieve the secret's value to a local machine from Github to do that?
As stated by Github Partner brightran on this Github Community Thread:
We have no safe way to fetch the values of secrets, and actually it is not recommended to try fetching and sharing the secrets.
He also shared in that post two ways to use the repository secrets in the source code files of your project when you build the project in the workflow runs, maybe you can try them with your buildscript in a Github Actions workflow that could be triggered manually (using a workflow_dispatch event, for example).

Do GitHub CLI commnds use REST APIs of GitHub? Is there any rate limit for using the commands?

I am using the GitHub CLI tool to get some pull-request information from my local repository. Mainly using the following commands from - https://cli.github.com/
gh pr list
gh pr view
But sometimes I get - graphql error: 'API rate limit exceeded'. Is there anyone who knows about the CLI rate limit? I could not find anything from their CLI documentation.
The CLI uses the REST as well as the GraphQL API. The rate limit depends on the kind of account you're using and whether you're signed in.
The graphql rate limit is 5000 when authenticated.
You can check all the rate limits for your account/current gh login, by querying https://api.github.com/rate_limit
First check your gh token in ~/.config/gh/hosts.yml
Query the api by e.g. curl -H "Authorization: Bearer <your token>" -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/rate_limit
And you will see all the limits and current usage.

How to get all commits in a Git tag through GitHub API

I have to fetch all new commits that were a part when a new tag was created on a Git repo. This needs to be done through GitHub API.
For example the Git UI says Tagging Tag1 and has a sha associated with it... let's say the sha is : SHA1
Now how do I get all commits which happened or were a part of Tag1 through GitHub API? I want to store all these commits and perform some analysis on them.
Based on the clarification on your comment:
I want to get all commits between this newly created tag and previous tag
1. Get all the tags in a given repo, so you can get the current and the previous tag names
curl -X "GET" "https://api.github.com/repos/:owner/:repo/tags" \
-H "Authorization: token YOUR_GITHUB_ACCESS_TOKEN"
2. Get all the commits between the latest 2 tags
curl -X "GET" "https://api.github.com/repos/:owner/:repo/compare/:tag_1...:tag_2" \
-H "Authorization: token YOUR_GITHUB_ACCESS_TOKEN"
Doc links:
https://developer.github.com/v3/repos/#list-tags
https://developer.github.com/v3/repos/commits/#compare-two-commits

How can I send a pull request via command line in Bitbucket?

I have to send a lot of pull requests, so I would rather use the bash command line than bitbucket's web interface.
Usage example: $ git-req username
Here is such a script for Github: http://pastebin.com/F9n3nPuu
Is there one for Bitbucket?
Bitbucket with it's RESTful API 2.0 supports managing pull requests without interface. In CLI you can request it with CURL. This older version of the documentation has better interface details.
Get pull request data with CURL
To get full data about specific pull request:
$ curl --user s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests/4
In return I get JSON with full info about my pull request #4 (put your username twice, password and reponame in command).
Create new pull request with RESTClient
To create new pull request we need to provide a lot of data with POST command, below how it looks in my RESTClient:
After firing Bitbucket shows pull request immediately:
Create new pull request with CURL
You can still create the same pull request with one liner:
$ curl -X POST -H "Content-Type: application/json" -u s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests -d '{ "title": "Merge some branches", "description": "stackoverflow example", "source": { "branch": { "name": "choose branch to merge with" }, "repository": { "full_name": "s3m3n/reponame" } }, "destination": { "branch": { "name": "choose branch that is getting changes" } }, "reviewers": [ { "username": "some other user needed to review changes" } ], "close_source_branch": false }'
REST browser tool (discontinued)
If you want to test all possible methods of API hop to REST browser tool of Bitbucket. It will show you all possible requests while returning your real repo's data.
Tried and tested :
Generate personal access token by clicking here
Save the Unique token id, append it after "Bearer in header".
For example: "Authorization : Bearer MDg4MzA4NTcfhtrhthyt/Thyythyh "
Complete JSON sample here:
Step 1 to enter the details and necessary headers
Try running it
Step 2
Output on BitBucket, You will be able to see the pull request
Final output
Command Line Syntax:
curl -i -X POST -H "Authorization:Bearer MDg4MzA4NTk/TlMSS6Ea" -H "X-Atlassian-Token:no-check" -H "Content-Type:application/json" -d '{"description":"1. Changes made 2. Changes made 3. Hello hanges","closed":false,"fromRef":{"id":"refs\/heads\/branch","repository":{"name":"From Repository ","project":{"key":"ProjectName"},"slug":"From Repository "}},"state":"OPEN","title":"Merge changes from branch to master","locked":false,"reviewers":[],"open":true,"toRef":{"id":"refs\/heads\/master","repository":{"name":"RepoName","project":{"key":"ProjectName"},"slug":"RepoName"}}}' 'https://bitbucket.agile.com/rest/api/1.0/projects/projectName/repos/repoName/pull-requests'
There are 2 repos on bitbucket that could help:
the Attlassian team have stash (ruby): https://bitbucket.org/atlassian/bitbucket-server-cli
Zhemao has bitbucket-cli (python): https://bitbucket.org/zhemao/bitbucket-cli
both have pull request feature from command line.
I wasn't too satisfied with the answers in this thread, so I created an package for it:
https://www.npmjs.com/package/bitbucket-pr
Instructions:
npm i -g bitbucket-pr
... Go to folder where you want to create a pull request ...
bitbucket-pr
I have created a pull request cli utility to ease my tasks.
Currently,
it can create/delete pr's right from the terminal
show basic diff for a raised PR
I have tested it with bitbucket enterprise 6.10.10
Source code: https://github.com/psadi/bbcli