-
Notifications
You must be signed in to change notification settings - Fork 7
/
reporter.go
66 lines (54 loc) · 1.55 KB
/
reporter.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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
func generateIssueBody(report Report, releaseNotes []ReleaseNote) string {
var body string
var dependency Dependency
for i := 0; i < len(report.Outdated.Dependencies); i++ {
dependency = report.Outdated.Dependencies[i]
body += fmt.Sprintf("* [ ] `%v:%v`", dependency.Pkg(), dependency.Available.Release)
if len(releaseNotes[i].URL) > 1 {
body += fmt.Sprintf("([Release Note](%v))\n", releaseNotes[i].URL)
} else {
body += "\n"
}
}
return body
}
func reportToGithub(report Report, githubAccessToken, userName, repositoryName string) error {
var pkgs string
for _, dependency := range report.Outdated.Dependencies {
pkgs += dependency.Pkg() + ","
}
pkgs = strings.TrimRight(pkgs, ",")
releaseNotes, err := getReleaseNotes(pkgs)
if err != nil {
return err
}
body := generateIssueBody(report, releaseNotes)
if len(body) == 0 {
// No libraries need to update
return nil
}
currentTime := time.Now()
title := "dependency-updates-" + currentTime.Format("20060102150405")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubAccessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
issueRequest := &github.IssueRequest{Title: &title, Body: &body}
ctx := context.Background()
_, _, err = client.Issues.Create(ctx, userName, repositoryName, issueRequest)
if err != nil {
return errors.Wrap(err, "issue create failed")
}
return nil
}