-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
79 lines (65 loc) · 2.43 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
package main
import (
"fmt"
"os"
"strings"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-tools/go-steputils/stepconf"
"github.com/kvvzr/bitrise-step-comment-on-github-pull-request/github"
)
type Config struct {
AuthToken stepconf.Secret `env:"personal_access_token,required"`
Body string `env:"body,required"`
RepositoryURL string `env:"repository_url,required"`
IssueNumber int `env:"issue_number,required"`
APIBaseURL string `env:"api_base_url,required"`
UpdateCommentTag string `env:"update_comment_tag"`
}
func ownerAndRepo(url string) (string, string) {
url = strings.TrimPrefix(strings.TrimPrefix(url, "https://"), "git@")
paths := strings.FieldsFunc(url, func(r rune) bool { return r == '/' || r == ':' })
return paths[1], strings.TrimSuffix(paths[2], ".git")
}
// wraps the tag in some special characters to avoid colliding with random text
func decoratedTag(tag string) string {
return fmt.Sprintf("<!-- %s -->", tag)
}
func main() {
var conf Config
if err := stepconf.Parse(&conf); err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
stepconf.Print(conf)
owner, repo := ownerAndRepo(conf.RepositoryURL)
commentBody := conf.Body
var githubClient *github.GithubClient
if conf.APIBaseURL == "" {
githubClient = github.NewClient(string(conf.AuthToken))
} else {
githubClient = github.NewEnterpriseClient(conf.APIBaseURL, string(conf.AuthToken))
}
// if tag is set, try to find and update existing comment
if conf.UpdateCommentTag != "" {
commentBody = fmt.Sprintf("%s\n\n%s", conf.Body, decoratedTag(conf.UpdateCommentTag))
taggedComment, err := githubClient.GetFirstCommentWithTag(owner, repo, conf.IssueNumber, decoratedTag(conf.UpdateCommentTag))
if err == nil { // comment with the given tag found
comment, err := githubClient.UpdateComment(owner, repo, *taggedComment.ID, commentBody)
if err != nil {
log.Errorf("Github API call failed when updating comment: %w\n", err)
os.Exit(1)
} else {
log.Successf("Success: %v\n", comment)
os.Exit(0)
}
}
}
// creating a new comment (either no update tag is set or no existing comment with the given tag found)
comment, err := githubClient.CreateComment(owner, repo, conf.IssueNumber, commentBody)
if err != nil {
log.Errorf("Github API call failed: %w\n", conf.IssueNumber, err)
os.Exit(1)
}
log.Successf("Success: %v\n", comment)
os.Exit(0)
}