wrapper

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-gitlab-wrapper - Work in Progress

My wrapper for some go-gitlab operations.

Usage

Only some parts of the API are wrapped. However, the wrapper embeds the go-gitlab client so we can call its method directly. Please see the docs at https://pkg.go.dev/github.com/xanzy/go-gitlab.

Create a Wrapper

This must be done first.

  • project: This is an interface similar to go-gitlab. This can be either an int with the project ID or a string with the project's path like parsiya/project.
  • token: This can be a project or personal token as a string. Make sure the access token has the api scope.
  • baseURL: The base URL of your GitLab instance. E.g., gitlab.com or gitlab.example.net.
  • email and name: Email address and name used in the commits and notes.
//            func Client(project interface{}, token,       baseURL,              email,             name string)
wr, err := wrapper.Client("parsiya/project"  , "yourtoken", "https://gitlab.com", "bot@example.net", "My GitLab Bot")
if err != nil {
    panic(err)
}

Create a New Branch

This code creates a new branch named new-branch from main.

br, err := wr.CreateBranch("new-branch", "main")
if err != nil {
    panic(err)
}
fmt.Println(br)

Create a New File

Now, we can create a new file in the branch above.

commitMsg := "Add new-new-file.txt from the API"
content := "new-new-file.txt content"

file, err := wr.NewFile("new-branch", commitMsg, "new-new-file.txt", content)
if err != nil {
    panic(err)
}
fmt.Println(file)

Create a Commit

If we need add/modify/delete multiple files, we can create a commit with multiple actions. We need one action per operation.

// Add some files to BranchName
// Each file should have its own action.
file1Contents := "File 1 contents"
file2Contents := "File 2 contents"

file1Opt := wrapper.CreateFileAction("dir1/file1.txt", file1Contents)
file2Opt := wrapper.CreateFileAction("dir2/file2.txt", file2Contents)

// Create an empty directory - creates a directory with just the ".gitkeep" file.
dir3Opt := wrapper.NewDirectoryAction("dir3")
dir4Opt := wrapper.NewDirectoryAction("dir3/dir4")

options := [4]*CommitAction{
    file1Opt, file2Opt, dir3Opt, dir4Opt,
}

Add all actions to a commit

commitMessage := "add a bunch of files and directories"
commit, err := wr.Commit(options[:], "new-branch", commitMessage)
if err != nil {
	panic(err)
}
fmt.Println(commit)

Create a Merge Request

Create a merge request from new-branch branch to main.

mr, err := wr.CreateMerge("Merge request title", "Merge request description", "new-branch", "main", true)
if err != nil {
	panic(err)
}
fmt.Println(mr)
// mr.IID is used in the rest of the example to modify this merge request.

List Merge Requests

We can list all merge requests that the token has access to (if it's not a project token). To see all merge requests, pass empty strings to this method.

// List all merge requests from new-branch to main.
mrList, err := wr.ListMergesByBranch("new-branch", "main")
if err != nil {
	panic(err)
}
fmt.Println(mrList)

We can also list all merge requests accessible to the token for a specific project with a different method.

// List all merge requests for a project.
prjMRList, err := wr.ListProjectMergeRequests("", "")
if err != nil {
	panic(err)
}
fmt.Println(prjMRList)

See Merge Request Notes

Comments for merge requests are called notes. Using the merge request IID from before we can see all of its comments.

// List all notes for a merge request.
notes, err := wr.ListMergeRequestNotes(mr.IID)
if err != nil {
	panic(err)
}
// fmt.Println(notes)
noteID := notes[0].ID

Add a Note to a Merge Request

The note text will be rendered in markdown in the web interface. We can add notes to closed merge requests, too.

// Create a new note for the merge request.
note, err := wr.CreateMergeRequestNote(mr.IID, "test merge request note")
if err != nil {
	panic(err)
}
fmt.Println(note)

List Notes for a Merge Request

See all notes for the previous merge request including the new note.

notes, err := wr.ListMergeRequestNotes(mr.IID,)
if err != nil {
	panic(err)
}
fmt.Println(notes)

Update a Merge Request Note

Update the previous note.

// Add a note to the merge request.
note, err := wr.UpdateMergeRequestNote(mr.IID, noteID, "test merge request note")
if err != nil {
	panic(err)
}
fmt.Println(note)

Delete a Merge Request Note

Delete the previous note.

// Delete the note from the merge reques.
if err := wr.DeleteMergeRequestNote(mr.IID, noteID); err != nil {
	panic(err)
}

License

The original project is licensed under the Apache Version 2.0 license. This wrapper has the same license. Please see LICENSE for details.

Documentation

Overview

branch wrappers.

Merge request wrappers.

Package wrapper provides a wrapper for the go-gitlab package for common functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommitAction

type CommitAction = gl.CommitActionOptions

CommitAction is an alias of CommitActionOptions.

func ChmodFileAction

func ChmodFileAction(path string, executeFilemode bool) *CommitAction

ChmodFileAction returns a CommitActionOptions that changes the executable status of the file at path to execute_filemode.

func CreateFileAction

func CreateFileAction(path, content string) *CommitAction

CreateFileAction returns a CommitActionOptions to create a new file at path with content.

func DeleteFileAction

func DeleteFileAction(path string) *CommitAction

DeleteFileAction returns a CommitActionOptions to delete the file at path.

func MoveFileAction

func MoveFileAction(path, previousPath string) *CommitAction

MoveFileAction returns a CommitActionOptions to move a file from previousPath to path.

func NewDirectoryAction

func NewDirectoryAction(path string) *CommitAction

NewDirectoryAction returns a CommitActionOptions to create a .gitkeep file in the new directory. This tells git to create the directory and store that file there.

path: Just supply the path to the directory. .gitkeep will be added by the function.

func UpdateFileAction

func UpdateFileAction(path, content string) *CommitAction

UpdateFileAction returns a CommitActionOptions to update the file at path with content.

type Wrapper

type Wrapper struct {
	*gl.Client
	// contains filtered or unexported fields
}

A Wrapper is a go-gitlab and communicates with the GitLab endpoint.

func Client

func Client(project interface{}, token, baseURL, email, name string) (*Wrapper, error)

Client creates a new Wrapper client.

func (*Wrapper) ClearBranch

func (w *Wrapper) ClearBranch(branch, commitMessage string) (*gl.Commit, error)

ClearBranch deletes everything in branch with a single commit.

func (*Wrapper) Commit

func (w *Wrapper) Commit(actions []*CommitAction, branch, commitMessage string) (*gl.Commit, error)

Commit creates a new commit with the included array of CommitActionOptions.

func (*Wrapper) CreateBranch

func (w *Wrapper) CreateBranch(branch string, refBranch string) (*gl.Branch, error)

CreateBranch creates a new branch in the target repository. branch: name of the new branch. refBranch: name of the parent branch. Cannot be empty.

func (*Wrapper) CreateMerge

func (w *Wrapper) CreateMerge(title, description, srcBranch, destBranch string, squash bool) (*gl.MergeRequest, error)

CreateMerge creates a merge request from srcBranch to destBranch.

func (*Wrapper) CreateMergeRequestNote

func (w *Wrapper) CreateMergeRequestNote(mergeRequestID int, noteBody string) (*gl.Note, error)

CreateMergeRequestNote adds a note/comment to mergeRequestID.

func (*Wrapper) DeleteMergeRequestNote

func (w *Wrapper) DeleteMergeRequestNote(mergeRequestID, noteID int) error

DeleteMergeRequestNote deletes mergeRequestID's noteID.

func (*Wrapper) ListMergeRequestNotes

func (w *Wrapper) ListMergeRequestNotes(mergeRequestID int) ([]*gl.Note, error)

ListMergeRequestNotes lists all notes for mergeRequestID.

func (*Wrapper) ListMergeRequests

func (w *Wrapper) ListMergeRequests(srcBranch, destBranch string) ([]*gl.MergeRequest, error)

ListMergeRequests lists all merge requests from srcBranch to destBranch across all projects. Pass an empty value to search everything. E.g., ListMergeRequests("", "") displays all merge requests.

func (*Wrapper) ListProjectMergeRequests

func (w *Wrapper) ListProjectMergeRequests(srcBranch, destBranch string) ([]*gl.MergeRequest, error)

ListProjectMergeRequests lists all merge requests for a project. Pass empty values to show everything.

func (*Wrapper) ListRepo

func (w *Wrapper) ListRepo(path, branch string) ([]*gl.TreeNode, error)

ListRepo list all files in the repository. path is the path inside the repository to list. branch is the target branch.

func (*Wrapper) NewFile

func (w *Wrapper) NewFile(branch, commitMsg, fileName string, content []byte) (*gl.FileInfo, error)

NewFile creates a single new file in the target repository and branch.

func (*Wrapper) UpdateMergeRequestNote

func (w *Wrapper) UpdateMergeRequestNote(mergeRequestID, noteID int, noteBody string) (*gl.Note, error)

UpdateMergeRequestNote replaces mergeRequestID's noteID with noteBody.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL