Post a comment on Github Pull Request via Command Line - github

Is there a way I can post a comment on a Github Pull Request via the command line? The ideas is that I want Jenkins to post comments on Pull Requests with a summary of the results of a script.

This is absolutely possible with nothing more than curl.
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/octocat/hello-world/issues/42/comments \
-d '{"body":"Hello Github!"}'
Read more about the API in use here https://docs.github.com/en/rest/reference/issues#create-an-issue-comment
NOTE: This assumes you have a Personal Access Token stored in an environment variable named GITHUB_TOKEN

It's not possible without third party extensions.
You may be intrested in this: https://github.com/stephencelis/ghi
Sorry I can't help more!

This is a little late, but this sounds like exactly what you are looking for:
gh pr comment 6 --body "Hi from GitHub CLI"
https://cli.github.com/manual/gh_pr_comment
Simply allows you to add comments to a pr from a given pr number.

Related

Create environment for repository using gh

Is it possible to create a new environment for a repository https://github.com/org/repo/settings/environments using the gh cli?
The only mention of environment I can find in the manual is here https://cli.github.com/manual/gh_secret_set where it says you can assign a secret to an existing environment, but it seems the environment would have to be created manually.
From what I'm seeing here in the gh cli repo that's going to be a no at this time. The issue [linked] is an enhancement request for just that.
I too wanted to know if this could be done, as it's part of my workflow to inject environment secrets.
You can still use the API to create it though.
curl -X PUT \
-H 'Authorization: Bearer ghp_...' \
-H 'Accept: application/vnd.github.v3+json' \
https://api.github.com/repos/<org>/<repo>/environments/<env>
Basically you need to create the environment first, then you can set branch policies:
jq -n '{"deployment_branch_policy": {"protected_branches": false, "custom_branch_policies": true}}'|gh api -H "Accept: application/vnd.github+json" -X PUT /repos/:owner/:repo/environments/dev --input -
gh api --method POST -H "Accept: application/vnd.github+json" "/repos/Oceaneering/it_infra_base_application_bootstrapper/environments/dev/deployment-branch-policies" -f name=dev
I wrote a python script for my use case that uses the gh cli to create environments and can include a branch pattern.
https://gist.github.com/walkerk1980/8a6f6879b32260360854a89bb880a48d

Github Workflow - Remove label at the end?

I have a workflow that is triggered when the pull request is labeled (via pull_request_target).
I would to automatically remove the label that triggered the analysis as the last step of that workflow.
How can I do that?
On Github: every pull request is an issue (but not every issue is a pull request).
You can find more informations about this on this stackoverflow answer.
Therefore, you could use the Github API to remove a (specific) label from an issue (or an action doing the same thing) as the last step of your workflow, using the PR number.
Here is the API on Github to remove label from issue (Official Documentation)
If you want to call it directly from the shell in your workflow with curl, it will looks like this:
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/<owner>/<repo>/issues/<pr_number>/labels/<label_name>
GuiFalourd does indeed have the correct answer, but I did have to improve on it a bit.
If you want to use the workflow on a private repo, you do need to add an authorization token. Here's an example that uses GitHub context to populate everything besides the label name you want to delete:
curl --silent --fail-with-body \
-X DELETE \
-H 'Accept: application/vnd.github.v3+json' \
-H 'Authorization: token ${{ github.token }}' \
'https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/labels/YOUR-LABEL'
Also if your label has spaces, use %20 for the whitespace.

How to post the comments on GitHub pull request page using curl.exe?

Are there any possibilities to post the comments on Github pull request page using curl.exe?
I have used the below as below mentioned commands, but it's not working, the comments have not posted on the pull request page. Could you suggest a solution for this?
curl -s -H "UserName:Token" -X POST -d '{"body": "My Review comments"}' "https://api.github.com/repos/UserName/my-docs/issues/11/comments"
In your question you mention "pull request" but your in your curl command you are calling the Issues API....I'll try and limit my answer to what you are attempting to do with the command by making a few changes in the POST headers:
curl -X POST \
-H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/UserName/my-docs/issues/11/comments \
-d '{"body":"My Review comments"}'
If you still run into trouble, run CURL with --verbose or -v option to get more information on what may be happening.

How do you create a project in a specific group via GitLab API?

I tried to use GitLab API to create a new project. This worked, but this is in my user space:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/projects/?name=$test-proj"
But I wanted to do it under a specific group with group_id <group_id> (I blanked it here). The most sensible approach that occured to me was:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/groups/<group_id>/projects/?name=test-proj
But this did not work. Are there any suggestions on how I could achieve this?
I consulted the following references
https://forum.gitlab.com/t/create-a-new-project-in-a-group-using-api/1552
https://docs.gitlab.com/ee/api/projects.html#create-project
The GitLab documentation mentions the path or namespace_id attribute (although I would actually be in search of a group_id attribute). I am not sure about path and how to specify that. I tried - without success - to retrieve the namespace_id via
curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" "https://gitlab.example.com/api/v4/namespaces"
It is well possible that I might not have the rights to do the required operation. Therefore a reference to an official (GitLab) documentation together with a test curl command that works would be very helpful to me - thank you!
For anyone looking for the direct command:
STEP 1:
Navigate as below and identify the group ID from GUI:
Admin Area -> Groups -> GroupName
STEP 2:
Construct the command with two parameters viz., name, namespace_id
curl --header "PRIVATE-TOKEN: <myprivatetoken>" -X POST "https://gitlab.com/api/v4/projects?name=myexpectedrepo&namespace_id=38"
Both users and groups are considered "namespaces" as far as the Gitlab API is concerned, so you can use either a group ID or a username in the namespace_id field. You can see this in use by getting a single namespace with either a Group ID (that you can see in the /groups API call, or from a Group's "profile" page) or a username:
# this will show the namespace details of the Group with ID 54
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespaces/54
# this will show the namespace details of the User with username my-username
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespace/my-username
If you have the appropriate access level, you can assign a git remote to your local repository that includes your group name. Then, the push event will trigger GitLab to create the remote repository.
$ mkdir project_name && cd $_
$ echo "# project_name" > README.md
$ git init; git add .; git commit -m initial
$ GITLAB_HOST="gitlab.com"
$ git remote add origin git#${GITLAB_HOST}:group_name/project_name.git
$ git push origin HEAD
Then, point your browser to ${GITLAB_HOST}/group_name/project_name
https://docs.gitlab.com/ee/user/group/#specify-who-can-add-projects-to-a-group

How to delete my follower at github?

Is it possible to do?
If yes, could you please provide a curl example?
Removing a follower on Github in fact is possible with this workaround:
Block the user who watches you, as described in the github help.
Then unblock him again.
After that he will not be blocked anymore and he will also no longer follow you.
Wrote to Github support:
Is there any way to delete followers? I know about "Block User" option but it's not exactly what I want. Github API methods are also suitable.
They answered:
No, this isn't currently possible.
So, there is no way for now. Only "Block User".
Manually: go to user profile - in the right corner there is Follow user and right next to it menu with Block user/Report user.
By clicking on Block user, he was removed from my followers.
Here is Github manual to do that.
Could not find an api alternative.
These would be the curl commands, you were looking for:
Block a user1
$ curl \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
https://api.github.com/user/blocks/USERNAME
Unblock a user2
$ curl \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
https://api.github.com/user/blocks/USERNAME
1 https://docs.github.com/en/rest/users/blocking#block-a-user
2 https://docs.github.com/en/rest/users/blocking#unblock-a-user