-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (82 loc) · 2.12 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
package main
import (
"context"
"fmt"
"ghtagreleaseaction/internal/tag"
"github.com/google/go-github/v62/github"
"os"
"strings"
)
const inputCrashOnError = "INPUT_CRASH_ON_ERROR"
func main() {
ctx := context.Background()
client := github.NewClient(nil)
login, repoName := ParseRepositoryInformations()
owner := github.User{Login: &login}
pr, _, err := client.PullRequests.ListPullRequestsWithCommit(ctx, login, repoName, os.Getenv("GITHUB_SHA"), nil)
if err != nil {
fmt.Printf("Error fetching pull requests for %s/%s: %v", login, repoName, err)
if os.Getenv(inputCrashOnError) == "true" {
os.Exit(2)
} else {
return
}
}
if len(pr) == 0 {
fmt.Println("No pull request found")
if os.Getenv(inputCrashOnError) == "true" {
os.Exit(2)
} else {
return
}
}
prNumber := pr[0].Number
if prNumber == nil || *prNumber == 0 {
fmt.Println("No PR found")
if os.Getenv(inputCrashOnError) == "true" {
os.Exit(2)
} else {
return
}
}
var outputs []string
releaseTag := tag.GetReleaseTag(ctx, client, &github.Repository{Owner: &owner, Name: &repoName}, *prNumber)
if releaseTag.Version == "" {
fmt.Println("No release tag found")
if os.Getenv(inputCrashOnError) == "true" {
os.Exit(2)
} else {
return
}
}
outputs = append(outputs, fmt.Sprintf("tag=%s", releaseTag.Version))
output := strings.Join(outputs, "\n")
fmt.Println(output)
printToStdout(output)
}
func ParseRepositoryInformations() (string, string) {
repository := os.Getenv("GITHUB_REPOSITORY")
if repository == "" {
fmt.Println("GITHUB_REPOSITORY is not set")
return "", ""
}
ownerRepo := strings.Split(repository, "/")
return ownerRepo[0], ownerRepo[1]
}
func printToStdout(output string) {
githubOutput := os.Getenv("GITHUB_OUTPUT")
if githubOutput == "" {
fmt.Println("GITHUB_OUTPUT is not set")
return
}
file, err := os.OpenFile(githubOutput, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("Error opening GITHUB_OUTPUT file: %v\n", err)
return
}
defer file.Close()
if _, err = file.WriteString(output); err != nil {
fmt.Printf("Error writing to GITHUB_OUTPUT file: %v\n", err)
return
}
}