How can i stop CircleCi from automatically deleting branches that are deleted from github? - github

I have a workflow on CircleCi that builds and deploys a branch to AWS when a pull request is open from that branch.
I have a second workflow that tears down the deployed environment when the pull request is merged.
NOW: When a branch is deleted from Github, it is also deleted from CircleCi. And most of the times, when the developers in my team merge their pull request, they delete the branch.
When this happens, CircleCi tries to teardown the previously deployed environment. The problem now is that since the branch was deleted from Github and thus automatically deleted from CircleCi, the request to start the teardown workflow fails with "branch not found" error.
The question now is, how can I make circleCi retain the branch that was deleted on Github so that this request will run? I can make the teardown job then delete the branch from CircleCi after the teardown is done.
The request to CircleCi to start the teardown workflow looks like this...
curl -X POST 'https://circleci.com/api/v2/project/github/<organization>/<project_name>/pipeline' \
-H 'Circle-Token: ${{env.CIRCLECI_TOKEN}}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d "{\"branch\":\"${{env.CIRCLECI_BRANCH}}\",\"parameters\":{\"pull_request_closed\":true}}"

I think the actual issue here is this:
\"branch\":\"${{env.CIRCLECI_BRANCH}}\"
This syntax to reference a CircleCI environment variable is incorrect.
Try this instead:
\"branch\": \"${CIRCLE_BRANCH}\"
 
Also, this statement:
When a branch is deleted from Github, it is also deleted from CircleCI
is not completely true.
Although the related branch might be deleted from the "Filter Branches" list, any build that ran on the now-deleted branch will still be present and visible in CircleCI.
You can access them either by:
Constructing a URL for the branch in question
https://app.circleci.com/pipelines/github/your_org_name/your_project_name?branch=your_deleted_branch_name
Scrolling through the "Pipelines" page to find a build that ran on that branch, then by clicking the branch name you'll see all builds that previously ran on the branch.

Related

How can I dynamically add a **must** reviewer to Pull request on github via github API

Background:
In Github, we have a shared project which can be updated by all the teams. In this project we have a lot of files belong to one or multiple teams. We can identify a file belong to which team by checking the files(.py) header, like:
# protected-by: teamA, teamB
Now, I am working on protecting the file by adding the teamA and teamB as must reviewers when anyone send a PR having updates on the file.
Problem
So far, I can use CircleCI to detect the changed files, extract the protectors list and set them as reviewers by using Github API like this:
curl --location --request POST -u $GH_USER:$GH_TOKEN \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/PROJECT/pulls/${pr_number}/requested_reviewers \
-d "{\"team_reviewers\":[$reviewers]}"
The problem is the reviewers I added are not must reviewers like CODEOWNER, users still could be able to merge without protectors' review. So I am thinking is there anyway to make PR must be reviewed by some reviewers, otherwise can not be merged by using Github API or other methods?

How we can disable merge of a pull request in github if any check fails in the CI job

We have a Jenkins CI job, where we will run a job when a pull request is raised. If that job fails in any case, we should not allow the user to merge the pull request. Is there any way we can do using github actions?
You could:
enable to branch protection policy "Require status checks to pass before merging"
Follow the "Creating CI tests with the Checks API" guide, which does not require a GitHub Action, and can create "Check runs and requested actions".
Using checks (as I mentioned here) would be a good way to prevent any merge while the PR has any check with an associated "failed" status.
The OP Ramanichandran confirms in the comments it is working:
For each failure stage in jenkins, we call this github api
sh('curl "https://api.github.com/repos/reponame/statuses/$GIT_COMMIT?access_token=xxx" \
-H "Content-Type: application/json" \
-X POST \
-d "{\\\"state\\\": \\\"failure\\\", \\\"target_url\\\": \\\"https://jenkinsurl/job/foldername/job/jobname/$BUILD_NUMBER/console\\\", \\\"description\\\": \\\"Jenkins-CI-pre-merge-job-sonarscan-failure\\\", \\\"context\\\": \\\"Jenkins-CI-pre-merge-job-sonarscan-failure\\\"}"') } –

GitHub unicorn when creating PR: "This page is taking too long to load."

When creating a Pull Request on GitHub, the page refuses to load and I am instead shown a unicorn error message:
This page is taking too long to load.
Sorry about that. Please try refreshing and contact us if the problem persists.
This repo's default branch is set to master, which is well behind the development branch we merge into. This error is likely a result of there being too many commits or the resulting PR being "too big".
Is there another URL or method to create a PR without calculating the full diff of the default branch?
If you push a branch, GitHub will helpfully show a Compare & pull request button as a shortcut to creating a PR for that branch:
That button is useful in most circumstances, but does automatically compare against the default branch using this URL:
https://github.com/user/repo/compare/branch?expand=1
If the resulting diff is too big or complicated, the unicorn error message will be shown.
Instead, press the New pull request button. The subsequent page will allow you to select the PR's base and compare branches prior to generating the diff.
https://github.com/user/repo/compare
The diff into your development branch will be simpler, and you should no longer get the error.
I managed to solve by creating the PR via the GitHub API e.g.
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/OWNER/REPO/pulls \
-f title='Amazing new feature' \
-f body='Please pull these awesome changes in!' \
-f head='octocat:new-feature' \
-f base='master'
https://docs.github.com/en/rest/pulls/pulls#create-a-pull-request

github new branch creation and pull request - using REST API

I need to create a branch and then do a pull request using REST API. First i tired to create a new branch with the following curl command.
[root#localhost tmp]# curl -d '{ "ref": "refs/heads/new_branch", "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd" }' -u user-name -X POST https://api.github.com/repos/user-name/myrepo/git/refs
Enter host password for user 'user-name':
{
"message": "Object does not exist",
"documentation_url": "https://developer.github.com/v3/git/refs/#create-a-reference"
}
A better alternative (since February 2020) is to use the new GitHub cli, which replaces/offers an alternative to github/hub.
See "GitHub CLI is now in beta " and "Supercharge your command line experience":
It is cli.github.com.
If will create a fork for you:
Create a pull request
Create a branch, make several commits to fix the bug described in the issue, and use gh to create a pull request to share your contribution.
By using GitHub CLI to create pull requests, it also automatically creates a fork when you don’t already have one, and it pushes your branch and creates your pull request to get your change merged.
a good alternative is to use hub instead. https://github.com/github/hub
For example:
if you haven't cloned the repo yet:
hub checkout [PULLREQ-URL]
if you have a repo cloned already, and just want a new branch
hub checkout [BRANCH]
Using the following link to review the commands for creating a new branch (AKA: refs) and this link to review how to validate the credentials, I could create a new empty branch. If you use the command without the validation, you will get a confusing answer
"message": "Not Found"
I followed this step to create a new branch named:
Get the SHA using this command -> GET https://api.github.com/repos/{username}/{repository}/git/refs/heads and copy one SHA (In my case, all are the same)
Create a token using this link and copy it. This token will be used in the parameter to create the branch.
Create the new branch using this command -> POST https://api.github.com/repos/{username}/{repository}/git/refs, setting a basic authorization {username}:{token} in the header and in the body the follow content:
{
"ref": "refs/heads/{BranchName}",
"sha": "{SHA copied in the step 1}"
}
Finally, you will obtain the information of the new branch

How to change the base branch of a pull request?

I created a pull request on project on GitHub to a specific remote branch. After some time, the remote branch was deleted.
How can I change the pull request to point to another branch (specifically master)?
Updated: as Michael notes below, this is now possible:
You can now change the base branch of an open pull request. After you’ve created a pull request, you can modify the base branch so that the changes in the pull request are compared against a different branch. By changing the base branch of your original pull request rather than opening a new one with the correct base branch, you’ll be able to keep valuable work and discussion.
Click the Edit button by the title of the pull request to reveal the base branch selector.
Old answer
You can't. Just make a new pull request.
Although undocumented, you can do this using the GitHub REST API.
The usage of the API is explained in this answer, but basically you can issue a REST request like this one:
$ curl --user "tom" \
--request PATCH \
--data '{"issue": "15", "head": "tom:new-branch", "base": "master"}' \
https://api.github.com/repos/fred/fabproj/pulls
This will change the pull request embodied by issue 15 on the fred/fabproj repo to use the new-branch branch on the tom/fabproj fork.
Edit: Note: according to comments, the above is only for attaching a new pull request to an existing issue.
As of 08/15/2016 this is now possible natively via Github:
You can now change the base branch of an open pull request. After you’ve created a pull request, you can modify the base branch so that the changes in the pull request are compared against a different branch. By changing the base branch of your original pull request rather than opening a new one with the correct base branch, you’ll be able to keep valuable work and discussion.
I could change the target branch.
It is true that we cannot edit the name of target branch in the PR. But the trick is to rename the branch to something else, and rename your target branch to that of present already in PR.
Example: My PR is having name like "dev-4.9". There is another branch which is named "qa-4.9". All I want is that "qa-4.9" should be the PR target branch.
Steps:1
1) Re-name branch "dev-4.9" to something else "original-dev-4.9"
git checkout dev-4.9
git branch -w original-dev-4.9
git push origin original-dev-4.9
2) Re-name branch "qa-4.9" to "dev-4.9".
git checkout qa-4.9
git branch -w dev-4.9
git push origin dev-4.9 -f (force push to write entire branch to reflect dev-4.9)
3) Refresh PR url and see the commits in qa-4.9 reflected over there.
Instead of losing all the comments connected with a PR to a deleted branch:
create the branch again locally with the same name and the same contents the branch you want to merge to has;
push that branch to recreate the remote branch; and then
reopen the PR to the branch.
For example, you have a PR to branch1, which is deleted. You now want to merge to master and retain comments on your existing PR:
git checkout master
git pull
git checkout -b branch1
git push
reopen your PR to branch1
when merged to branch1, merge to master.
This is a bit hacky, but far better than destroying lots of comments.
Github supports this now. Edit button on the right end of the PR.
In theory...
you're supposed to use github api.
example : edit pull request with curl
curl --user "your_github_username" \
--request PATCH \
--data '{"title":"newtitle","body":"newbody",...}' \
https://api.github.com/repos/:owner/:repo/pulls/:number
you can find the detailled list of data in github developer doc
example : change name of my pull request
curl --user "jeremyclement" \
--request PATCH \
--data '{"title":"allows the control of files and folders permissions."}' \
https://api.github.com/repos/Gregwar/Cache/pulls/9
but in practice...
it seems that the fields head/label and head/ref are not editable. For now, the only solution seems to be that of Amber