Add the app struct
This commit is contained in:
parent
8732aee990
commit
114f73f9f5
11 changed files with 208 additions and 198 deletions
75
app/app.go
Normal file
75
app/app.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/google/go-github/v29/github"
|
||||
jira "gopkg.in/andygrunwald/go-jira.v1"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Path string
|
||||
|
||||
jiraClient *jira.Client
|
||||
githubClient *github.Client
|
||||
Campaign *model.Campaign
|
||||
}
|
||||
|
||||
func SaveCampaign(campaign *model.Campaign, path string) error {
|
||||
marshaledCampaign, err := json.MarshalIndent(campaign, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(path, marshaledCampaign, 0600); err != nil {
|
||||
return fmt.Errorf("cannot save campaign: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) Save() error {
|
||||
return SaveCampaign(a.Campaign, a.Path)
|
||||
}
|
||||
|
||||
func (a *App) Load() error {
|
||||
if _, err := os.Stat("."); err != nil {
|
||||
return fmt.Errorf("cannot read campaign: %w", err)
|
||||
}
|
||||
|
||||
fileContents, err := ioutil.ReadFile(a.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("there was a problem reading the campaign file: %w", err)
|
||||
}
|
||||
|
||||
var campaign model.Campaign
|
||||
if err := json.Unmarshal(fileContents, &campaign); err != nil {
|
||||
return fmt.Errorf("there was a problem parsing the campaign file: %w", err)
|
||||
}
|
||||
|
||||
a.Campaign = &campaign
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) InitClients() error {
|
||||
if err := a.InitGithubClient(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.InitJiraClient(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewApp(path string) (*App, error) {
|
||||
app := &App{Path: path}
|
||||
|
||||
if err := app.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return app, nil
|
||||
}
|
125
app/github.go
Normal file
125
app/github.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
||||
|
||||
"github.com/StevenACoffman/j2m"
|
||||
"github.com/google/go-github/v29/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func (a *App) InitGithubClient() error {
|
||||
ctx := context.Background()
|
||||
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: a.Campaign.Github.Token})
|
||||
tc := oauth2.NewClient(ctx, ts)
|
||||
|
||||
a.githubClient = github.NewClient(tc)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getFooterTemplate(ticket *model.Ticket, templatePath string) (string, error) {
|
||||
footerTmpl, err := template.ParseFiles(templatePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var footerBytes bytes.Buffer
|
||||
if err := footerTmpl.Execute(&footerBytes, ticket); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return footerBytes.String(), nil
|
||||
}
|
||||
|
||||
func (a *App) PublishInGithub(ticket *model.Ticket, dryRun bool) (*github.Issue, error) {
|
||||
mdDescription := j2m.JiraToMD(ticket.Description)
|
||||
if a.Campaign.FooterTemplate != "" {
|
||||
footer, err := getFooterTemplate(ticket, a.Campaign.FooterTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mdDescription += "\n" + footer
|
||||
}
|
||||
|
||||
issueRequest := &github.IssueRequest{
|
||||
Title: &ticket.Summary,
|
||||
Body: &mdDescription,
|
||||
Labels: &a.Campaign.Github.Labels,
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
b, _ := json.MarshalIndent(issueRequest, "", " ")
|
||||
fmt.Println(string(b))
|
||||
return &github.Issue{
|
||||
Title: issueRequest.Title,
|
||||
Body: issueRequest.Body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
owner, repo := a.Campaign.RepoComponents()
|
||||
newIssue, _, err := a.githubClient.Issues.Create(context.Background(), owner, repo, issueRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newIssue, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishNextInGithub(dryRun bool) (bool, error) {
|
||||
ticket := a.Campaign.NextGithubUnpublishedTicket()
|
||||
if ticket == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
issue, err := a.PublishInGithub(ticket, dryRun)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
ticket.GithubLink = issue.GetNumber()
|
||||
if user := issue.GetUser(); user != nil {
|
||||
ticket.GithubAssignee = user.GetLogin()
|
||||
}
|
||||
ticket.GithubStatus = issue.GetState()
|
||||
if err := a.Save(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishAllInGithub(dryRun bool) (int, error) {
|
||||
count := 0
|
||||
for {
|
||||
next, err := a.PublishNextInGithub(dryRun)
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
if !next {
|
||||
break
|
||||
}
|
||||
count++
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishBatchInGithub(batch int, dryRun bool) error {
|
||||
for i := 1; i <= batch; i++ {
|
||||
next, err := a.PublishNextInGithub(dryRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !next {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
167
app/jira.go
Normal file
167
app/jira.go
Normal file
|
@ -0,0 +1,167 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
||||
|
||||
jira "gopkg.in/andygrunwald/go-jira.v1"
|
||||
)
|
||||
|
||||
func (a *App) InitJiraClient() error {
|
||||
tp := jira.BasicAuthTransport{
|
||||
Username: a.Campaign.Jira.Username,
|
||||
Password: a.Campaign.Jira.Token,
|
||||
}
|
||||
|
||||
client, err := jira.NewClient(tp.Client(), a.Campaign.Jira.Url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.jiraClient = client
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) GetJiraIssueFromTicket(ticket *model.Ticket) (*jira.Issue, error) {
|
||||
summaryTmpl, err := template.New("").Parse(a.Campaign.Summary)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var summaryBytes bytes.Buffer
|
||||
if err := summaryTmpl.Execute(&summaryBytes, ticket.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
summary := summaryBytes.String()
|
||||
|
||||
descriptionTemplate, err := template.ParseFiles(a.Campaign.IssueTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var descriptionBytes bytes.Buffer
|
||||
if err := descriptionTemplate.Execute(&descriptionBytes, ticket.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
description := descriptionBytes.String()
|
||||
|
||||
data := map[string]string{
|
||||
"Description": description,
|
||||
"Summary": summary,
|
||||
"Project": a.Campaign.Jira.Project,
|
||||
"Issue Type": a.Campaign.Jira.IssueType,
|
||||
"Epic Link": a.Campaign.Jira.Epic,
|
||||
}
|
||||
|
||||
createMetaInfo, _, err := a.jiraClient.Issue.GetCreateMeta(a.Campaign.Jira.Project)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
project := createMetaInfo.GetProjectWithKey(a.Campaign.Jira.Project)
|
||||
if project == nil {
|
||||
return nil, fmt.Errorf("Error retrieving project with key %s", a.Campaign.Jira.Project)
|
||||
}
|
||||
|
||||
issueType := project.GetIssueTypeWithName(a.Campaign.Jira.IssueType)
|
||||
if issueType == nil {
|
||||
return nil, fmt.Errorf("Error retrieving issue type with name Story")
|
||||
}
|
||||
|
||||
issue, err := jira.InitIssueWithMetaAndFields(project, issueType, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return issue, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishInJira(ticket *model.Ticket, dryRun bool) (*jira.Issue, error) {
|
||||
issue, err := a.GetJiraIssueFromTicket(ticket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
b, _ := json.MarshalIndent(issue, "", " ")
|
||||
fmt.Println(string(b))
|
||||
return issue, nil
|
||||
}
|
||||
|
||||
newIssue, _, err := a.jiraClient.Issue.Create(issue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newIssue, nil
|
||||
}
|
||||
|
||||
func (a *App) GetIssue(issueNo string) (*jira.Issue, error) {
|
||||
issue, _, err := a.jiraClient.Issue.Get(issueNo, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return issue, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishNextInJira(dryRun bool) (bool, error) {
|
||||
ticket := a.Campaign.NextJiraUnpublishedTicket()
|
||||
if ticket == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
issue, err := a.PublishInJira(ticket, dryRun)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
issue, _, err = a.jiraClient.Issue.Get(issue.Key, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
ticket.JiraLink = fmt.Sprintf("%s/browse/%s", a.Campaign.Jira.Url, issue.Key)
|
||||
ticket.Summary = issue.Fields.Summary
|
||||
ticket.Description = issue.Fields.Description
|
||||
ticket.JiraStatus = issue.Fields.Status.Name
|
||||
if err := a.Save(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishAllInJira(dryRun bool) (int, error) {
|
||||
count := 0
|
||||
for {
|
||||
next, err := a.PublishNextInJira(dryRun)
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
if !next {
|
||||
break
|
||||
}
|
||||
count++
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (a *App) PublishBatchInJira(batch int, dryRun bool) error {
|
||||
for i := 1; i <= batch; i++ {
|
||||
next, err := a.PublishNextInJira(dryRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !next {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue