Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

batches: automatically delete branches when changeset is merged/closed with site config setting#52055

Merged
courier-new merged 26 commits into
mainfrom
aa+kr/auto-delete-branch
May 24, 2023
Merged

batches: automatically delete branches when changeset is merged/closed with site config setting#52055
courier-new merged 26 commits into
mainfrom
aa+kr/auto-delete-branch

Conversation

@courier-new

@courier-new courier-new commented May 17, 2023

Copy link
Copy Markdown
Contributor

Closes https://github.com/sourcegraph/sourcegraph/issues/50354.

This adds a new site setting, batchChanges.autoDeleteBranch, which, when enabled, will have Sourcegraph attempt to delete the branches for any changesets it has created when the changesets are merged or closed.

Ideally, this functionality would be opt-in per batch change or even per changeset operation, so there's room for future iteration here. This PR merely provides the groundwork to enable this functionality and exposes it via a global site config setting so that customers can start taking advantage of it immediately.

Code hosts offer varying levels of support for this. Several provide a 1st-party solution to this via a property on the changeset itself that can be toggled. But a few don't. For those, Sourcegraph must instead issue a secondary API call to delete the branch after it closes or merges the changeset. This does mean that on those code hosts, if the changeset is merged or closed on the code host itself, we won't have any control over whether or not the branch is deleted. I've tried to make this explicitly clear in the docs.

We introduce this logic at the Batch Changes Source layer, where we have consolidated other logic about the ways Batch Changes should interact with code host API methods. Generally speaking, the approach takes one of the following forms:

  • If a code host supports this natively with a flag on the changeset, great! At any point when we would create or update the properties of a changeset on the code host, we check the setting in site config and make sure to send its updated value along in the payload, ensuring the flag always stays closely synced with the setting on Sourcegraph.
  • If a code host does not support this natively, at any point when we close or merge a changeset on the code host, we check the setting in site config and, on successful close/merge, we send a follow-up request to delete the branch associated with the changeset.

Test plan

Wherever we already had VCR integration tests set up for Batch Changes Sources, I wrote additional tests to cover toggling the flag or the additional API calls. Two code hosts don't have any VCR test set ups, so for those I only manually tested with the code hosts. I'd love to add VCR testing for them but just don't have the time right now.

Code host Test strategy
Azure DevOps Manual testing
Bitbucket Server Additional VCR tests added, manual testing
Bitbucket Cloud Manual testing
GitHub Additional VCR tests added, manual testing
GitLab Additional VCR tests added, manual testing

@courier-new courier-new self-assigned this May 17, 2023
@cla-bot cla-bot Bot added the cla-signed label May 17, 2023
Comment on lines -173 to -174
CommitID string `json:"commitId"`
MergeStrategy *PullRequestMergeStrategy `json:"mergeStrategy"`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of the json tags here is intentional; this type isn't directly used as any request payload, it's only the input type of the CompletePullRequest method, so the tags are unnecessary as an object of this type is never encoded as JSON.

Comment on lines +1582 to +1584
if resp.StatusCode != http.StatusNoContent {
err = json.NewDecoder(resp.Body).Decode(result)
}

@courier-new courier-new May 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would see errors reported whenever the new DeleteBranch method was invoked for GitHub because the response is a 204 No Content, so the body would be totally empty and the JSON decoding would freak out when it immediately read in EOF. They were harmless errors, but this just cleans that up.

This is also covered by the GitHub integration test I added.

@courier-new courier-new requested a review from a team May 17, 2023 05:36
@courier-new courier-new added the batch-changes Issues related to Batch Changes label May 17, 2023
@courier-new courier-new marked this pull request as ready for review May 17, 2023 05:36
@sourcegraph-bot

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 929cc1e...836141f.

Notify File(s)
@eseliger doc/batch_changes/how-tos/site_admin_configuration.md
enterprise/internal/batches/sources/BUILD.bazel
enterprise/internal/batches/sources/azuredevops.go
enterprise/internal/batches/sources/bitbucketcloud.go
enterprise/internal/batches/sources/bitbucketserver.go
enterprise/internal/batches/sources/bitbucketserver_test.go
enterprise/internal/batches/sources/github.go
enterprise/internal/batches/sources/github_test.go
enterprise/internal/batches/sources/gitlab.go
enterprise/internal/batches/sources/gitlab_test.go
enterprise/internal/batches/sources/testdata/golden/BitbucketServerSource_CloseChangeset_DeleteSourceBranch_success
enterprise/internal/batches/sources/testdata/golden/GitLabSource_CreateChangeset_no-remove-source-branch
enterprise/internal/batches/sources/testdata/golden/GitLabSource_CreateChangeset_yes-remove-source-branch
enterprise/internal/batches/sources/testdata/golden/GithubSource_CloseChangeset_DeleteSourceBranch_success
enterprise/internal/batches/sources/testdata/golden/GitlabSource_LoadChangeset_found
enterprise/internal/batches/sources/testdata/sources/BitbucketServerSource_CloseChangeset_DeleteSourceBranch_success.yaml
enterprise/internal/batches/sources/testdata/sources/GitLabSource_CreateChangeset_no-remove-source-branch.yaml
enterprise/internal/batches/sources/testdata/sources/GitLabSource_CreateChangeset_yes-remove-source-branch.yaml
enterprise/internal/batches/sources/testdata/sources/GithubSource_CloseChangeset_DeleteSourceBranch_success.yaml
internal/extsvc/azuredevops/pull_requests.go
internal/extsvc/azuredevops/types.go
internal/extsvc/bitbucketcloud/pull_requests.go
internal/extsvc/bitbucketserver/client.go
internal/extsvc/github/common.go
internal/extsvc/github/v3.go
internal/extsvc/github/v4.go
internal/extsvc/gitlab/merge_requests.go
@sashaostrikov internal/extsvc/azuredevops/pull_requests.go
internal/extsvc/azuredevops/types.go
internal/extsvc/bitbucketcloud/pull_requests.go
internal/extsvc/bitbucketserver/client.go
internal/extsvc/github/common.go
internal/extsvc/github/v3.go
internal/extsvc/github/v4.go
internal/extsvc/gitlab/merge_requests.go
@sourcegraph/delivery doc/admin/config/batch_changes.md

@BolajiOlajide BolajiOlajide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.
I haven't tested with all code hosts yet (only GitHub & GitLab), but I plan to get around that later today.

@courier-new

Copy link
Copy Markdown
Contributor Author

Thanks! Don't feel like you have to test them all right away, either. I want to get the setting enabled on S2 and pay extra attention to it between now and when we cut the patch release. :D

@BolajiOlajide

Copy link
Copy Markdown
Contributor

Thanks! Don't feel like you have to test them all right away, either. I want to get the setting enabled on S2 and pay extra attention to it between now and when we cut the patch release. :D

Alright. Let's test further on S2. Excited for this.

@courier-new courier-new merged commit 5d1f7e6 into main May 24, 2023
@courier-new courier-new deleted the aa+kr/auto-delete-branch branch May 24, 2023 18:58
ErikaRS pushed a commit that referenced this pull request Jun 22, 2023
…d with site config setting (#52055)

This adds a new site setting, batchChanges.autoDeleteBranch, which, when enabled, will have Sourcegraph attempt to delete the branches for any changesets it has created when the changesets are merged or closed.
- If a code host supports this natively with a flag on the changeset, great! At any point when we would create or update the properties of a changeset on the code host, we check the setting in site config and make sure to send its updated value along in the payload, ensuring the flag always stays closely synced with the setting on Sourcegraph.
- If a code host does not support this natively, at any point when we close or merge a changeset on the code host, we check the setting in site config and, on successful close/merge, we send a follow-up request to delete the branch associated with the changeset.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

batch-changes Issues related to Batch Changes cla-signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow branches created by Batch Changes to be deleted after closing/deleting a Batch Change

4 participants