-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (164 loc) · 4.98 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/google/go-github/v60/github"
)
// https://goreleaser.com/cookbooks/using-main.version/
var (
version = "unset"
commit = "none"
date = "unknown"
)
type Options struct {
// from flag
from string
to string
labels []string
template *string
json bool
disableGeneratedByMessage bool
customParameters any
// from env
owner string
repo string
gitHubToken string
apiUrl *url.URL
}
func getOptions() (Options, error) {
from := flag.String("from", "", "The base branch name.")
to := flag.String("to", "", "The target branch name.")
labelsFlag := flag.String("labels", "", "Specify the labels to add to the pull request as a comma-separated list of strings.")
template := flag.String("template", "", "The path to the template file.")
enableJsonOutput := flag.Bool("json", false, "Output the release pull request data in JSON format.")
disableGeneratedByMessage := flag.Bool("disable-generated-by-message", false, "Disable the generated by message in the release pull request body.")
customParametersString := flag.String("custom-parameters", "{}", "Passed to the template as an object.")
flag.Parse()
githubToken := os.Getenv("GITHUB_TOKEN")
repository := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
owner := repository[0]
repo := repository[1]
rawApiUrl := os.Getenv("GITHUB_API_URL")
apiUrl, _ := url.Parse(rawApiUrl)
var labels []string
if *labelsFlag != "" {
labels = strings.Split(*labelsFlag, ",")
}
var customParameters any
err := json.Unmarshal([]byte(*customParametersString), &customParameters)
if err != nil {
return Options{}, err
}
return Options{
from: *from,
to: *to,
labels: labels,
template: template,
json: *enableJsonOutput,
disableGeneratedByMessage: *disableGeneratedByMessage,
customParameters: customParameters,
owner: owner,
repo: repo,
gitHubToken: githubToken,
apiUrl: apiUrl,
}, nil
}
type Result struct {
IsCreated bool `json:"is_created,omitempty"`
ReleasePullRequest *github.PullRequest `json:"release_pull_request,omitempty"`
}
func getResultJson(result Result) (string, error) {
resultJson, err := json.Marshal(result)
if err != nil {
return "", err
}
return string(resultJson), nil
}
func exitWithError(err error) {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(1)
}
func run(options Options) (*Result, error) {
logger = GetLogger()
logger.Printf("version: %s, commit: %s, date: %s\n", version, commit, date)
ctx := context.Background()
from := options.from
to := options.to
client := NewClient(GithubClientOptions{owner: options.owner, repo: options.repo, githubToken: options.gitHubToken, apiUrl: options.apiUrl})
totalCommits, pullRequests, commits, err := client.FetchChanges(ctx, from, to)
if err != nil {
return nil, err
}
if totalCommits == 0 {
logger.Println("No pull requests or commits were found for the release. Nothing to do.")
return nil, nil
}
logger.Println("Found pull requests: ", len(pullRequests))
logger.Println("Found commits: ", len(commits))
currentTime := time.Now()
date := currentTime.Format("2006-01-02")
renderTemplateData := RenderTemplateData{
PullRequests: pullRequests,
Commits: commits,
Date: date,
From: from,
To: to,
CustomParameters: options.customParameters,
}
data, err := RenderTemplate(options.template, renderTemplateData, options.disableGeneratedByMessage)
if err != nil {
return nil, err
}
parts := strings.SplitN(data, "\n", 2)
title := parts[0]
body := parts[1]
logger.Println("Title of pull request: ", title)
pr, created, err := client.CreatePullRequest(ctx, title, body, from, to)
if err != nil {
return nil, err
}
if created {
logger.Println("Created new a pull request.", pr.GetNumber())
} else {
_, err := client.UpdatePullRequest(ctx, pr.GetNumber(), title, body)
if err != nil {
return nil, err
}
logger.Println("The pull request already exists. The body was updated.", pr.GetNumber())
}
if len(options.labels) > 0 {
err := client.AddLabelsToPullRequest(ctx, pr.GetNumber(), options.labels)
if err != nil {
return nil, err
}
logger.Println("Added labels to the pull request.", pr.GetNumber())
}
result := Result{IsCreated: created, ReleasePullRequest: pr}
return &result, nil
}
func main() {
options, err := getOptions()
if err != nil {
exitWithError(err)
}
result, err := run(options)
if err != nil {
exitWithError(err)
}
if options.json {
if result == nil {
result = &Result{}
}
resultJson, err := getResultJson(*result)
if err != nil {
exitWithError(err)
}
fmt.Println(resultJson)
}
}