Skip to content
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

Resolve threads on discussions #893

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ stages:
- release

default:
image: golang:1.18
image: golang:1.21
tags:
- gitlab-org

Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux

go:
- 1.18.x
- 1.21.x

# use containers which run faster and have cache
sudo: false
Expand Down
58 changes: 48 additions & 10 deletions cmd/mr_discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ var mrCreateDiscussionCmd = &cobra.Command{
lab mr discussion my-topic-branch
lab mr discussion origin 123
lab mr discussion origin my-topic-branch
lab mr discussion --commit abcdef123456 --position=main.c:+100,100`),
lab mr discussion --commit abcdef123456 --position=main.c:+100,100
lab mr discussion upstream 613278108 --resolve`),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, mrNum, err := parseArgsWithGitBranchMR(args)

reply, branchArgs, err := filterCommentArg(args)
if err != nil {
log.Fatal(err)
}

rn, mrNum, err := parseArgsWithGitBranchMR(branchArgs)
if err != nil {
log.Fatal(err)
}
Expand All @@ -57,6 +64,15 @@ var mrCreateDiscussionCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
resolve, err := cmd.Flags().GetBool("resolve")
if err != nil {
log.Fatal(err)
}

var currentDiscussion *gitlab.Discussion
var discussions []*gitlab.Discussion
var NoteURL string

var posFile string
var posLineType byte
var posLineNumberNew, posLineNumberOld uint64
Expand Down Expand Up @@ -98,11 +114,33 @@ var mrCreateDiscussionCmd = &cobra.Command{

body := ""
if filename != "" {
fmt.Println("1")
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
body = string(content)
} else if resolve {

discussions, err = lab.MRListDiscussions(rn, int(mrNum))
if err != nil {
log.Fatal(err)
}

// Find discussion sha based on discussion
for _, discussion := range discussions {
if len(discussion.Notes) > 0 && discussion.Notes[0].ID == reply {
currentDiscussion = discussion
break
}
}

NoteURL, err = lab.ResolveMRDiscussion(rn, int(mrNum), currentDiscussion, reply)
if err != nil {
log.Fatal(err)
}
fmt.Println(NoteURL)
return
} else if position != "" || commit == "" {
// TODO If we are commenting on a specific position in the diff, we should include some context in the template.
body, err = mrDiscussionMsg(int(mrNum), state, commit, msgs, "\n")
Expand All @@ -121,7 +159,7 @@ var mrCreateDiscussionCmd = &cobra.Command{
return
}

var notePos gitlab.PositionOptions
var discussionOpts gitlab.CreateMergeRequestDiscussionOptions
if position != "" {
if commit == "" {
// We currently only support "--position" when commenting on individual commits within an MR.
Expand Down Expand Up @@ -150,7 +188,7 @@ var mrCreateDiscussionCmd = &cobra.Command{
oldLine := int(oldLine64)

positionType := "text"
notePos = gitlab.PositionOptions{
discussionOpts.Position = &gitlab.PositionOptions{
BaseSHA: &parentSHA,
StartSHA: &parentSHA,
HeadSHA: &commit,
Expand All @@ -165,16 +203,15 @@ var mrCreateDiscussionCmd = &cobra.Command{
if body == "" {
log.Fatal("aborting discussion due to empty discussion msg")
}
discussionOpts.Body = &body

var commitID *string
if commit != "" {
commitID = &commit
}
discussionOpts.CommitID = commitID

discussionURL, err := lab.MRCreateDiscussion(rn, int(mrNum), &gitlab.CreateMergeRequestDiscussionOptions{
Body: &body,
CommitID: commitID,
Position: &notePos,
})
discussionURL, err := lab.MRCreateDiscussion(rn, int(mrNum), &discussionOpts)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -213,6 +250,7 @@ func init() {
mrCreateDiscussionCmd.Flags().StringSliceP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
mrCreateDiscussionCmd.Flags().StringP("file", "F", "", "use the given file as the message")
mrCreateDiscussionCmd.Flags().StringP("commit", "c", "", "start a thread on a commit")
mrCreateDiscussionCmd.Flags().Bool("resolve", false, "mark thread resolved")

mrCreateDiscussionCmd.Flags().StringP("position", "", "", heredoc.Doc(`
start a thread on a specific line of the diff
Expand All @@ -224,7 +262,7 @@ func init() {
<old_line> is ignored. If the line type is "-", then <new_line> is ignored.

Here's an example diff that explains how to determine the old/new line numbers:

--- a/README.md old new
+++ b/README.md
@@ -100,3 +100,4 @@
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func Test_mrFilterByTargetBranch(t *testing.T) {
}

var (
latestCreatedTestMR = "!1447 Test for mr approval rules"
latestCreatedTestMR = "!1476 create mr request"
latestUpdatedTestMR = "!329 MR for assign and review commands"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/note_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo
NoteURL, err = lab.AddMRDiscussionNote(rn, idNum, discussion.ID, body)
}
if resolve {
NoteURL, err = lab.ResolveMRDiscussion(rn, idNum, discussion.ID, reply)
NoteURL, err = lab.ResolveMRDiscussion(rn, idNum, discussion, reply)
}
} else {
NoteURL, err = lab.AddIssueDiscussionNote(rn, idNum, discussion.ID, body)
Expand Down
1 change: 1 addition & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ func mapLabelsAsLabelOptions(rn string, labelTerms []string) (gitlab.LabelOption

return gitlab.LabelOptions(matches), nil
}

// dumpToken dumps information about a specific Personal Access Token
func dumpToken(tokendata *gitlab.PersonalAccessToken) {
fmt.Println("ID: ", tokendata.ID)
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

require (
Expand Down Expand Up @@ -68,7 +66,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/whilp/git-urls v1.0.0
Expand All @@ -87,4 +84,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.17
go 1.21
Loading