-
Notifications
You must be signed in to change notification settings - Fork 54
Draft: feat: implement rebasing #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakubbortlik
wants to merge
3
commits into
harrisoncramer:develop
Choose a base branch
from
jakubbortlik:feat/implement-rebasing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| gitlab "gitlab.com/gitlab-org/api/client-go" | ||
| ) | ||
|
|
||
| type RebaseMrRequest struct { | ||
| SkipCI bool `json:"skip_ci,omitempty"` | ||
| } | ||
|
|
||
| type MergeRequestRebaser interface { | ||
| RebaseMergeRequest(pid interface{}, mergeRequest int64, opt *gitlab.RebaseMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) | ||
| } | ||
|
|
||
| type mergeRequestRebaserService struct { | ||
| data | ||
| client MergeRequestRebaser | ||
| } | ||
|
|
||
| /* Rebases a merge request on the server */ | ||
| func (a mergeRequestRebaserService) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| payload := r.Context().Value(payload("payload")).(*RebaseMrRequest) | ||
|
|
||
| opts := gitlab.RebaseMergeRequestOptions{ | ||
| SkipCI: &payload.SkipCI, | ||
| } | ||
|
|
||
| res, err := a.client.RebaseMergeRequest(a.projectInfo.ProjectId, a.projectInfo.MergeId, &opts) | ||
|
|
||
| if err != nil { | ||
| handleError(w, err, "Could not rebase MR", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| if res.StatusCode >= 300 { | ||
| handleError(w, GenericError{r.URL.Path}, "Could not rebase MR", res.StatusCode) | ||
| return | ||
| } | ||
|
|
||
| skippingCI := "" | ||
| if payload.SkipCI { | ||
| skippingCI = " (skipping CI)" | ||
| } | ||
| response := SuccessResponse{Message: fmt.Sprintf("MR rebased successfully%s", skippingCI)} | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
|
|
||
| err = json.NewEncoder(w).Encode(response) | ||
| if err != nil { | ||
| handleError(w, err, "Could not encode response", http.StatusInternalServerError) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| gitlab "gitlab.com/gitlab-org/api/client-go" | ||
| ) | ||
|
|
||
| type fakeMergeRequestRebaser struct { | ||
| testBase | ||
| } | ||
|
|
||
| func (f fakeMergeRequestRebaser) RebaseMergeRequest(pid interface{}, mergeRequest int64, opt *gitlab.RebaseMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) { | ||
| resp, err := f.handleGitlabError() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp, err | ||
| } | ||
|
|
||
| func TestRebaseHandler(t *testing.T) { | ||
| var testRebaseMrPayload = RebaseMrRequest{SkipCI: false} | ||
| t.Run("Rebases merge request", func(t *testing.T) { | ||
| request := makeRequest(t, http.MethodPost, "/mr/rebase", testRebaseMrPayload) | ||
| svc := middleware( | ||
| mergeRequestRebaserService{testProjectData, fakeMergeRequestRebaser{}}, | ||
| withMr(testProjectData, fakeMergeRequestLister{}), | ||
| withPayloadValidation(methodToPayload{ | ||
| http.MethodPost: newPayload[RebaseMrRequest], | ||
| }), | ||
| withMethodCheck(http.MethodPost), | ||
| ) | ||
| data := getSuccessData(t, svc, request) | ||
| assert(t, data.Message, "MR rebased successfully") | ||
| }) | ||
| var testRebaseMrPayloadSkipCI = RebaseMrRequest{SkipCI: true} | ||
| t.Run("Rebases merge request and skips CI", func(t *testing.T) { | ||
| request := makeRequest(t, http.MethodPost, "/mr/rebase", testRebaseMrPayloadSkipCI) | ||
| svc := middleware( | ||
| mergeRequestRebaserService{testProjectData, fakeMergeRequestRebaser{}}, | ||
| withMr(testProjectData, fakeMergeRequestLister{}), | ||
| withPayloadValidation(methodToPayload{ | ||
| http.MethodPost: newPayload[RebaseMrRequest], | ||
| }), | ||
| withMethodCheck(http.MethodPost), | ||
| ) | ||
| data := getSuccessData(t, svc, request) | ||
| assert(t, data.Message, "MR rebased successfully (skipping CI)") | ||
| }) | ||
| t.Run("Handles errors from Gitlab client", func(t *testing.T) { | ||
| request := makeRequest(t, http.MethodPost, "/mr/rebase", testRebaseMrPayload) | ||
| svc := middleware( | ||
| mergeRequestRebaserService{testProjectData, fakeMergeRequestRebaser{testBase{errFromGitlab: true}}}, | ||
| withMr(testProjectData, fakeMergeRequestLister{}), | ||
| withPayloadValidation(methodToPayload{ | ||
| http.MethodPost: newPayload[RebaseMrRequest], | ||
| }), | ||
| withMethodCheck(http.MethodPost), | ||
| ) | ||
| data, _ := getFailData(t, svc, request) | ||
| checkErrorFromGitlab(t, data, "Could not rebase MR") | ||
| }) | ||
| t.Run("Handles non-200s from Gitlab", func(t *testing.T) { | ||
| request := makeRequest(t, http.MethodPost, "/mr/rebase", testRebaseMrPayload) | ||
| svc := middleware( | ||
| mergeRequestRebaserService{testProjectData, fakeMergeRequestRebaser{testBase{status: http.StatusSeeOther}}}, | ||
| withMr(testProjectData, fakeMergeRequestLister{}), | ||
| withPayloadValidation(methodToPayload{ | ||
| http.MethodPost: newPayload[RebaseMrRequest], | ||
| }), | ||
| withMethodCheck(http.MethodPost), | ||
| ) | ||
| data, _ := getFailData(t, svc, request) | ||
| checkNon200(t, data, "Could not rebase MR", "/mr/rebase") | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| local u = require("gitlab.utils") | ||
|
|
||
| local M = {} | ||
|
|
||
| ---@class RebaseOpts | ||
| ---@field skip_ci boolean? | ||
|
|
||
| local can_rebase = function() | ||
| local git = require("gitlab.git") | ||
| -- Check if there are local changes (we wouldn't be able to run `git pull` after rebasing) | ||
| local has_clean_tree, err = git.has_clean_tree() | ||
| if not has_clean_tree then | ||
| u.notify("Cannot rebase when there are changed files", vim.log.levels.ERROR) | ||
| return false | ||
| elseif err ~= nil then | ||
| u.notify("Error while inspecting working tree", vim.log.levels.ERROR) | ||
| return false | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| ---@param opts RebaseOpts | ||
| M.rebase = function(opts) | ||
| if not can_rebase() then | ||
| return | ||
| end | ||
|
|
||
| -- TODO: check that MR needs rebasing (requires https://github.com/harrisoncramer/gitlab.nvim/pull/532) | ||
|
|
||
| local state = require("gitlab.state") | ||
| local rebase_body = { skip_ci = state.settings.rebase_mr.skip_ci } | ||
| if opts and opts.skip_ci ~= nil then | ||
| rebase_body.skip_ci = opts.skip_ci | ||
| end | ||
|
|
||
| M.confirm_rebase(rebase_body) | ||
| end | ||
|
|
||
| ---@param merge_body RebaseOpts | ||
| M.confirm_rebase = function(merge_body) | ||
| local job = require("gitlab.job") | ||
| job.run_job("/mr/rebase", "POST", merge_body, function(data) | ||
| u.notify(data.message, vim.log.levels.INFO) | ||
| local git = require("gitlab.git") | ||
| local state = require("gitlab.state") | ||
| local success = git.pull(state.settings.connection_settings.remote, state.INFO.source_branch, { "--rebase" }) | ||
| if success then | ||
| u.notify( | ||
| string.format( | ||
| "Pulled `%s %s` successfully", | ||
| state.settings.connection_settings.remote, | ||
| state.INFO.source_branch | ||
| ), | ||
| vim.log.levels.INFO | ||
| ) | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| return M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using branch names to open the Diffview the view automatically updates after fetching the remote target branch and pulling the source branch which has been rebased on the server.
I still need to find out if this doesn't break anything or if the diff_refs should not be replaced in other parts of the codebase as well.