Commit 71a1946b authored by John Jarvis's avatar John Jarvis

Jarv/helper client

parent 805ef220
......@@ -44,5 +44,6 @@ func main() {
title := "OnCall report for period: " +
time.Now().UTC().AddDate(0, 0, -7).Format("2006-01-02") +
" - " + time.Now().UTC().Format("2006-01-02")
report.CreateReportIssue(title)
issue := report.CreateReportIssue(title)
log.Println("Created", issue)
}
......@@ -6,16 +6,45 @@ import (
"time"
gitlab "github.com/xanzy/go-gitlab"
config "gitlab.com/gl-infra/oncall-robot-assistant/config"
)
func getMergeRequestsByProject(config *config.Config) []*gitlab.MergeRequest {
git := gitlab.NewClient(nil, config.APITokens.GitLab)
// GitLabHelper api helper
type GitLabHelper struct {
client *gitlab.Client
apiToken string
projectID int
dayOffset int
}
// GitLabHelperOptions options for the api helper
type GitLabHelperOptions struct {
APIToken string
ProjectID int
DayOffset int
}
// NewGitLabHelper creates a new api helper
func NewGitLabHelper(opts GitLabHelperOptions) *GitLabHelper {
r := GitLabHelper{
client: newGitLabClient(opts.APIToken),
apiToken: opts.APIToken,
projectID: opts.ProjectID,
dayOffset: opts.DayOffset,
}
return &r
}
func newGitLabClient(apiToken string) *gitlab.Client {
return gitlab.NewClient(nil, apiToken)
}
// GetMergeRequestsByProject get merge requests
func (g *GitLabHelper) GetMergeRequestsByProject() []*gitlab.MergeRequest {
opts := gitlab.ListProjectMergeRequestsOptions{}
dayInterval := time.Now().UTC().AddDate(0, 0, -config.DayOffset)
dayInterval := time.Now().UTC().AddDate(0, 0, -g.dayOffset)
opts.CreatedAfter = &dayInterval
if mrs, _, err := git.MergeRequests.ListProjectMergeRequests(config.Projects.Infrastructure.ID, &opts); err != nil {
if mrs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.projectID, &opts); err != nil {
panic(err)
} else {
return mrs
......@@ -23,15 +52,27 @@ func getMergeRequestsByProject(config *config.Config) []*gitlab.MergeRequest {
}
func getIssuesOpenedDuringShift(config *config.Config) []*gitlab.Issue {
git := gitlab.NewClient(nil, config.APITokens.GitLab)
// CreateIssue creates an issue
func (g *GitLabHelper) CreateIssue(title string, desc string, projID int) *gitlab.Issue {
opts := gitlab.CreateIssueOptions{}
opts.Title = &title
opts.Description = &desc
issue, _, err := g.client.Issues.CreateIssue(projID, &opts)
if err != nil {
log.Fatalf("Unable to create issue in GitLab project %d: %s", projID, err.Error())
}
return issue
}
// GetIssuesOpenedDuringShift gets opened issues during shift
func (g *GitLabHelper) GetIssuesOpenedDuringShift() []*gitlab.Issue {
var opts gitlab.ListProjectIssuesOptions
opts.ListOptions.PerPage = 100
dayInterval := time.Now().UTC().AddDate(0, 0, -config.DayOffset)
dayInterval := time.Now().UTC().AddDate(0, 0, -g.dayOffset)
opts.CreatedAfter = &dayInterval
allIssues := make([]*gitlab.Issue, 0)
for {
issues, r, err := git.Issues.ListProjectIssues(config.Projects.Infrastructure.ID, &opts)
issues, r, err := g.client.Issues.ListProjectIssues(g.projectID, &opts)
if err != nil {
panic(err)
}
......@@ -47,15 +88,15 @@ func getIssuesOpenedDuringShift(config *config.Config) []*gitlab.Issue {
return allIssues
}
func getIssuesOpenAll(config *config.Config) []*gitlab.Issue {
git := gitlab.NewClient(nil, config.APITokens.GitLab)
// GetIssuesOpenAll gets all open issues
func (g *GitLabHelper) GetIssuesOpenAll() []*gitlab.Issue {
var opts gitlab.ListProjectIssuesOptions
opts.ListOptions.PerPage = 100
state := "opened"
opts.State = &state
allIssues := make([]*gitlab.Issue, 0)
for {
issues, r, err := git.Issues.ListProjectIssues(config.Projects.Infrastructure.ID, &opts)
issues, r, err := g.client.Issues.ListProjectIssues(g.projectID, &opts)
if err != nil {
panic(err)
}
......@@ -71,6 +112,15 @@ func getIssuesOpenAll(config *config.Config) []*gitlab.Issue {
return allIssues
}
// UploadFile uploads a file to a project
func (g *GitLabHelper) UploadFile(projectID int, fname string) *gitlab.ProjectFile {
pf, _, err := g.client.Projects.UploadFile(projectID, fname)
if err != nil {
log.Fatalf("Unable to upload graphs from file %s to project id %d : %s", fname, projectID, err.Error())
}
return pf
}
func filterIssuesByLabel(label string, issues []*gitlab.Issue) (filteredIssues []*gitlab.Issue) {
for _, p := range issues {
if stringInSlice(label, p.Labels) {
......
......@@ -13,7 +13,6 @@ import (
"time"
"github.com/GeertJohan/go.rice"
gitlab "github.com/xanzy/go-gitlab"
config "gitlab.com/gl-infra/oncall-robot-assistant/config"
)
......@@ -25,15 +24,16 @@ type GraphData struct {
Info string
}
// ReportClients for clients needed for hte report
type ReportClients struct {
GitLab *gitlab.Client
// ReportHelpers for clients needed for hte report
type ReportHelpers struct {
GitLab *GitLabHelper
GitLabDev *GitLabHelper
}
// Report clients and config for report
type Report struct {
Config *config.Config
Clients *ReportClients
Helpers *ReportHelpers
}
// ReportOptions has options for the report
......@@ -44,7 +44,7 @@ type ReportOptions struct {
func NewReport(config *config.Config, opts ReportOptions) *Report {
r := Report{
Config: config,
Clients: newReportClients(opts, config),
Helpers: newReportHelpers(config),
}
return &r
}
......@@ -52,39 +52,24 @@ func NewReport(config *config.Config, opts ReportOptions) *Report {
// CreateReportIssue - creates new report and returns an issue, this is a multi-step process
// * Downloads graphs from grafana (these are specified in the yaml config)
// * Uploads the graphs to the project
// * Creates an issue using the issue template
func (r *Report) CreateReportIssue(title string) { // *gitlab.Issue {
// * Creates an issue using the issue template and returns the link
func (r *Report) CreateReportIssue(title string) string {
graphs := r.downloadGraphs()
weeklyOpsGraphs := r.uploadGraphs(graphs)
desc := r.generateTemplate(weeklyOpsGraphs)
log.Println(desc)
opts := gitlab.CreateIssueOptions{}
opts.Title = &title
opts.Description = &desc
log.Println("Creating issue ...")
log.Println(desc)
start := time.Now()
issue, _, err := r.Clients.GitLab.Issues.CreateIssue(r.Config.Projects.ReportProject.ID, &opts)
if err != nil {
log.Fatalf("Unable to create issue in GitLab project %d: %s", r.Config.Projects.ReportProject.ID, err.Error())
}
secs := time.Since(start).Seconds()
log.Printf(" Created issue %s in %.2f seconds\n", issue.WebURL, secs)
issue := r.Helpers.GitLab.CreateIssue(title, desc, r.Config.Projects.ReportProject.ID)
return issue.WebURL
}
// Upload graphs to the gitlab project
func (r *Report) uploadGraphs(graphs <-chan GraphData) (opsGraphs []WeeklyOpsGraph) {
log.Println("Uploading graphs to GitLab project ...")
start := time.Now()
git := gitlab.NewClient(nil, r.Config.APITokens.GitLab)
for graph := range graphs {
defer os.Remove(graph.Fname)
// defer os.Remove(graph.Fname)
opsGraph := WeeklyOpsGraph{}
log.Println(" uploading", graph.Name)
pf, _, err := git.Projects.UploadFile(r.Config.Projects.ReportProject.ID, graph.Fname)
if err != nil {
log.Fatalf("Unable to upload graphs to project id %d : %s", r.Config.Projects.ReportProject.ID, err.Error())
}
pf := r.Helpers.GitLab.UploadFile(r.Config.Projects.ReportProject.ID, graph.Fname)
opsGraph.URL = pf.URL
opsGraph.Name = graph.Name
opsGraphs = append(opsGraphs, opsGraph)
......@@ -135,8 +120,8 @@ func (r *Report) generateTemplate(weeklyOpsGraphs []WeeklyOpsGraph) string {
inc.CreatedAt = p.CreatedAt
templateData.Incidents = append(templateData.Incidents, inc)
}
shiftIssues := getIssuesOpenedDuringShift(r.Config)
allOpenIssues := getIssuesOpenAll(r.Config)
shiftIssues := r.Helpers.GitLab.GetIssuesOpenedDuringShift()
allOpenIssues := r.Helpers.GitLab.GetIssuesOpenAll()
// Issue stats
templateData.IssuesOpenedDuringShift.Count = len(shiftIssues)
......@@ -188,9 +173,20 @@ func (r *Report) generateTemplate(weeklyOpsGraphs []WeeklyOpsGraph) string {
return desc.String()
}
func newReportClients(opts ReportOptions, config *config.Config) *ReportClients {
tc := ReportClients{
GitLab: gitlab.NewClient(nil, config.APITokens.GitLab),
func newReportHelpers(config *config.Config) *ReportHelpers {
optsGitLab := GitLabHelperOptions{
APIToken: config.APITokens.GitLab,
ProjectID: config.Projects.Infrastructure.ID,
DayOffset: config.DayOffset,
}
optsGitLabDev := GitLabHelperOptions{
APIToken: config.APITokens.GitLabDev,
ProjectID: config.Projects.Infrastructure.ID,
DayOffset: config.DayOffset,
}
tc := ReportHelpers{
GitLab: NewGitLabHelper(optsGitLab),
GitLabDev: NewGitLabHelper(optsGitLabDev),
}
return &tc
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment