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

chore: gitlab tools #314

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions gitlab/.gitignore
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these should be added to the top-level .gitignore file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin/
.idea/
.vscode/
3 changes: 3 additions & 0 deletions gitlab/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: build
build:
go build -o bin/gptscript-go-tool .
7 changes: 7 additions & 0 deletions gitlab/credential/tool.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name: GitLab Credential
Share Credential: github.com/gptscript-ai/credential as gitlabToken
Copy link
Member

Choose a reason for hiding this comment

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

nit: we'll probably want to pull this into the tools repo at some point

with "Please enter your GitLab token" as message and
token as field and
"GITLAB_TOKEN" as env and
sensitive as true
Type: credential
16 changes: 16 additions & 0 deletions gitlab/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/gptscript-ai/tools/gitlab

go 1.23.4

require gitlab.com/gitlab-org/api/client-go v0.118.0

require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.36.0 // indirect
)
69 changes: 69 additions & 0 deletions gitlab/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/gitlab-org/api/client-go v0.118.0 h1:qHIEw+XHt+2xuk4iZGW8fc6t+gTLAGEmTA5Bzp/brxs=
gitlab.com/gitlab-org/api/client-go v0.118.0/go.mod h1:E+X2dndIYDuUfKVP0C3jhkWvTSE00BkLbCsXTY3edDo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
254 changes: 254 additions & 0 deletions gitlab/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package main

import (
"encoding/json"
"fmt"
"github.com/gptscript-ai/tools/gitlab/pkg/commands"
gitlab "gitlab.com/gitlab-org/api/client-go"
"os"
"strconv"
)

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: gitlab <command>")
os.Exit(1)
}

command := os.Args[1]

git, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"))
if err != nil {
fmt.Printf("Failed to create gitlab client: %v\n", err)
os.Exit(1)
}
user, _, err := git.Users.CurrentUser()
if err != nil {
fmt.Printf("Error getting current user: %v\n", err)
os.Exit(1)
}

switch command {
// Issues
case "queryIssues":
rawQuery := os.Getenv("ISSUE_QUERY")
var query map[string]any
if rawQuery != "" {
query, err = parseStringToMap(rawQuery)
if err != nil {
fmt.Printf("Failed to parse ISSUE_QUERY: %v\n", err)
os.Exit(1)
}
Comment on lines +37 to +41
Copy link
Member

Choose a reason for hiding this comment

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

nit: if you factor this switch out into a separate function, you can return errors and have a single fmt.Println(err); os.Exit(1); in main().

}

if err := commands.QueryIssues(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listProjectIssues":
rawQuery := os.Getenv("ISSUE_QUERY")
var query map[string]any
if rawQuery != "" {
query, err = parseStringToMap(rawQuery)
if err != nil {
fmt.Printf("Failed to parse ISSUE_QUERY: %v\n", err)
os.Exit(1)
}
}

if err := commands.ListProjectIssues(git, os.Getenv("PROJECT_ID"), query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listMyAssignedIssues":
query := map[string]any{
"assignee_username": user.Username,
}
if err := commands.QueryIssues(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listMyCreatedIssues":
query := map[string]any{
"author_id": user.ID,
}
if err := commands.QueryIssues(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "getIssueDetails":
projectID := os.Getenv("PROJECT_ID")
issueIID, err := strconv.Atoi(os.Getenv("ISSUE_IID"))
if err != nil {
fmt.Printf("Failed to parse ISSUE_IID: %v\n", err)
os.Exit(1)
}
if err := commands.GetIssueDetails(git, projectID, issueIID); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "createIssue":
projectID := os.Getenv("PROJECT_ID")
issueName := os.Getenv("ISSUE_NAME")
issueDescription := os.Getenv("ISSUE_DESCRIPTION")
if projectID == "" || issueName == "" {
fmt.Println("PROJECT and ISSUE_NAME fields are both required.")
os.Exit(1)
}
if err := commands.CreateIssue(git, projectID, issueName, issueDescription); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "updateIssue":
projectID := os.Getenv("PROJECT_ID")
issueID := os.Getenv("ISSUE_IID")
updatesMap, err := parseStringToMap(os.Getenv("UPDATES"))
if err != nil {
fmt.Printf("Failed to parse UPDATES: %v\n", err)
os.Exit(1)
}

if err := commands.UpdateIssue(git, projectID, issueID, updatesMap); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "deleteIssue":
projectID := os.Getenv("PROJECT_ID")
issueID := os.Getenv("ISSUE_IID")
if err := commands.DeleteIssue(git, projectID, issueID); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "addIssueComment":
projectID := os.Getenv("PROJECT_ID")
issueID := os.Getenv("ISSUE_IID")
comment := os.Getenv("COMMENT")
if err := commands.AddIssueComment(git, projectID, issueID, comment); err != nil {
fmt.Println(err)
os.Exit(1)
}

// Merge Requests
case "queryMergeRequests":
query, err := parseStringToMap(os.Getenv("MR_QUERY"))
if err != nil {
fmt.Printf("Failed to parse MR_QUERY: %v\n", err)
os.Exit(1)
}
if err := commands.QueryMergeRequests(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listProjectMergeRequests":
query, err := parseStringToMap(os.Getenv("MR_QUERY"))
if err != nil {
fmt.Printf("Failed to parse MR_QUERY: %v\n", err)
os.Exit(1)
}
if err := commands.ListProjectMergeRequests(git, os.Getenv("PROJECT_ID"), query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listMyOpenedMergeRequests":
query := map[string]any{
"author_id": user.ID,
}
if err := commands.QueryMergeRequests(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "listMyAssignedMergeRequests":
query := map[string]any{
"assignee_username": user.Username,
}
if err := commands.QueryMergeRequests(git, query); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "getMergeRequestDetails":
projectID := os.Getenv("PROJECT_ID")
mrID, err := strconv.Atoi(os.Getenv("MR_ID"))
if err != nil {
fmt.Printf("Failed to parse MR_ID: %v\n", err)
os.Exit(1)
}
if err := commands.GetMergeRequestDetails(git, projectID, mrID); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "createMergeRequest":
projectID := os.Getenv("PROJECT_ID")
mrName := os.Getenv("MR_NAME")
mrDescription := os.Getenv("MR_DESCRIPTION")
mrSourceBranch := os.Getenv("MR_SOURCE_BRANCH")
mrTargetBranch := os.Getenv("MR_TARGET_BRANCH")
if projectID == "" || mrName == "" || mrSourceBranch == "" || mrTargetBranch == "" {
fmt.Println("PROJECT, MR_NAME, MR_SOURCE_BRANCH, and MR_TARGET_BRANCH fields are all required.")
os.Exit(1)
}
if err := commands.CreateMergeRequest(git, projectID, mrName, mrDescription, mrSourceBranch, mrTargetBranch); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "updateMergeRequest":
projectID := os.Getenv("PROJECT_ID")
MRID := os.Getenv("MR_ID")
updatesMap, err := parseStringToMap(os.Getenv("UPDATES"))
if err != nil {
fmt.Printf("Failed to parse UPDATES: %v\n", err)
os.Exit(1)
}

if err := commands.UpdateMergeRequest(git, projectID, MRID, updatesMap); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "deleteMergeRequest":
projectID := os.Getenv("PROJECT_ID")
MRID := os.Getenv("MR_ID")
if err := commands.DeleteMergeRequest(git, projectID, MRID); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "addMergeRequestComment":
projectID := os.Getenv("PROJECT_ID")
MRID := os.Getenv("MR_ID")
comment := os.Getenv("COMMENT")
if err := commands.AddMergeRequestComment(git, projectID, MRID, comment); err != nil {
fmt.Println(err)
os.Exit(1)
}
case "approveMergeRequest":
projectID := os.Getenv("PROJECT_ID")
MRID := os.Getenv("MR_ID")

if err := commands.ApproveMergeRequest(git, projectID, MRID); err != nil {
fmt.Println(err)
os.Exit(1)
}

// Utility
case "lookupUserID":
username := os.Getenv("GITLAB_USERNAME")
if username == "" {
fmt.Println("Username is required")
os.Exit(1)
}
if err := commands.LookupUserID(git, username); err != nil {
fmt.Println(err)
os.Exit(1)
}
default:
fmt.Printf("Unknown command: %q\n", command)
os.Exit(1)
}
}

func parseStringToMap(raw string) (map[string]any, error) {
var transformedMap map[string]any
err := json.Unmarshal([]byte(raw), &transformedMap)
if err != nil {
return nil, err
}
return transformedMap, nil
}
11 changes: 11 additions & 0 deletions gitlab/pkg/commands/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands

import "strings"

func IndentString(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
lines[i] = " " + line
}
return strings.Join(lines, "\n")
Comment on lines +6 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this is slightly more efficient?

Suggested change
lines := strings.Split(s, "\n")
for i, line := range lines {
lines[i] = " " + line
}
return strings.Join(lines, "\n")
return " " + strings.ReplaceAll(s, "\n", "\n ")

}
Loading