Skip to content

Commit

Permalink
close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
odanado committed Aug 12, 2024
1 parent f95c29d commit 0ead87b
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ func getResultJson(result Result) (string, error) {
return string(resultJson), nil
}

func run(options Options) error {
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)

Expand All @@ -107,20 +112,20 @@ func run(options Options) error {

prNumbers, err := client.FetchPullRequestNumbers(ctx, from, to)
if err != nil {
return err
return nil, err
}

if len(prNumbers) == 0 {
logger.Println("No pull requests were found for the release. Nothing to do.")
return nil
return nil, nil
}

logger.Println("Found pull requests: ", prNumbers)

pullRequests, err := client.FetchPullRequests(ctx, prNumbers)

if err != nil {
return err
return nil, err
}

currentTime := time.Now()
Expand All @@ -135,7 +140,7 @@ func run(options Options) error {
data, err := RenderTemplate(options.template, renderTemplateData, options.disableGeneratedByMessage)

if err != nil {
return err
return nil, err
}

parts := strings.SplitN(data, "\n", 2)
Expand All @@ -146,52 +151,52 @@ func run(options Options) error {

pr, created, err := client.CreatePullRequest(ctx, title, body, from, to)
if err != nil {
return err
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 err
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 err
return nil, err
}
logger.Println("Added labels to the pull request.", pr.GetNumber())
}

if options.json {
result := Result{IsCreated: created, ReleasePullRequest: pr}
resultJson, err := getResultJson(result)
if err != nil {
return err
}

fmt.Println(resultJson)
}
result := Result{IsCreated: created, ReleasePullRequest: pr}

return nil
return &result, nil
}

func main() {
options, err := getOptions()

if err != nil {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(1)
exitWithError(err)
}

err = run(options)
result, err := run(options)

if err != nil {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(1)
exitWithError(err)
}

if options.json {
resultJson, err := getResultJson(*result)

if err != nil {
exitWithError(err)
}

fmt.Println(resultJson)
}
}

0 comments on commit 0ead87b

Please sign in to comment.