-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (93 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/xanzy/go-gitlab"
)
func getGitlab() *gitlab.Client {
url := os.Getenv("GITLAB_URL")
token := os.Getenv("GITLAB_TOKEN")
gl, err := gitlab.NewClient(token, gitlab.WithBaseURL(url))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return gl
}
func getNewFileContent(localFilePath string) string {
contents, err := os.ReadFile(localFilePath)
if err != nil {
log.Fatal("Could not open file")
}
return string(contents)
}
type args struct {
file string
repo string
repoFile string
commitMessage string
force bool
branchName string
}
func parseArgs() *args {
fileFlag := flag.String("file", "", "Specify the location of the local file")
repoFlag := flag.String("repo", "", "The location of the GitLab repo. E.g. 'gitlab-org/gitlab'")
repoFileFlag := flag.String("repo-file", "", "The repository file to replace")
branchNameFlag := flag.String("branch", "", "The branch name to use. Optional. Defaults to refactor-change-file-<filename>")
commitMessageFlag := flag.String("m", "", "The commit message")
forceFlag := flag.Bool("force", false, "Force branch creation")
flag.Parse()
arguments := args{}
arguments.file = *fileFlag
arguments.repo = *repoFlag
arguments.repoFile = *repoFileFlag
arguments.commitMessage = *commitMessageFlag
arguments.force = *forceFlag
if *branchNameFlag == "" {
arguments.branchName = fmt.Sprintf("refactor-change-file-%s", filepath.Base(*fileFlag))
} else {
arguments.branchName = *branchNameFlag
}
if arguments.file == "" || arguments.repo == "" || arguments.repoFile == "" || arguments.commitMessage == "" {
log.Fatal("file, repo, repo-file and commitMessage (-m) are required.")
}
return &arguments
}
func getBranchOptions(branchName string, defaultBranch string) *gitlab.CreateBranchOptions {
return &gitlab.CreateBranchOptions{Branch: &branchName, Ref: &defaultBranch}
}
func getDefaultBranch(gl *gitlab.Client, repo string) string {
project, _, err := gl.Projects.GetProject(repo, nil, nil)
if err != nil {
panic(err)
}
return project.DefaultBranch
}
func createBranch(gl *gitlab.Client, branchName string, defaultBranch string, arguments *args) {
if arguments.force {
gl.Branches.DeleteBranch(arguments.repo, branchName)
}
_, _, err := gl.Branches.CreateBranch(arguments.repo, getBranchOptions(branchName, defaultBranch))
if err != nil {
fmt.Println(fmt.Sprintf("Could not create branch '%s'. Does it already exist? Use -force to ignore.", branchName))
os.Exit(1)
}
}
func main() {
arguments := parseArgs()
gl := getGitlab()
defaultBranch := getDefaultBranch(gl, arguments.repo)
createBranch(gl, arguments.branchName, defaultBranch, arguments)
options := &gitlab.UpdateFileOptions{
Branch: gitlab.String(arguments.branchName),
Content: gitlab.String(getNewFileContent(arguments.file)),
CommitMessage: gitlab.String(arguments.commitMessage),
}
_, _, err := gl.RepositoryFiles.UpdateFile(arguments.repo, arguments.repoFile, options)
if err != nil {
panic(err)
}
gl.MergeRequests.CreateMergeRequest(arguments.repo, &gitlab.CreateMergeRequestOptions{Title: &arguments.commitMessage, SourceBranch: &arguments.branchName, TargetBranch: &defaultBranch, RemoveSourceBranch: gitlab.Bool(true)})
}