|
| 1 | +package gong |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "gopkg.in/salsita/go-pivotaltracker.v1/v5/pivotal" |
| 6 | + "strconv" |
| 7 | + "regexp" |
| 8 | +) |
| 9 | + |
| 10 | +// PivotalClient : Struct implementing the generic Client interface |
| 11 | +type PivotalClient struct { |
| 12 | + client *pivotal.Client |
| 13 | + config map[string]string |
| 14 | +} |
| 15 | + |
| 16 | +// NewPivotalClient : Returns a pointer to PivotalClient |
| 17 | +func NewPivotalClient() *PivotalClient { |
| 18 | + return &PivotalClient{} |
| 19 | +} |
| 20 | + |
| 21 | +func (p *PivotalClient) Create() (string, error) { |
| 22 | + fields, err := Load() |
| 23 | + |
| 24 | + if err != nil { |
| 25 | + fmt.Println(err) |
| 26 | + } |
| 27 | + |
| 28 | + projectId := fields["projectId"] |
| 29 | + |
| 30 | + domain := "https://www.pivotaltracker.com/n/projects" |
| 31 | + url := fmt.Sprintf("%s/%s", domain, projectId) |
| 32 | + |
| 33 | + if err != nil { |
| 34 | + return "", err |
| 35 | + } |
| 36 | + |
| 37 | + return url, nil |
| 38 | +} |
| 39 | + |
| 40 | +// GetName : Return the string name of the struct eg: pivotal |
| 41 | +func (p *PivotalClient) GetName() string { |
| 42 | + return "pivotal" |
| 43 | +} |
| 44 | + |
| 45 | +// Browse : Browse to the URL of the issue related to the branch name |
| 46 | +func (p *PivotalClient) Browse(branchName string) (string, error) { |
| 47 | + issueID := GetPivotalIssueID(branchName) |
| 48 | + domain := "https://www.pivotaltracker.com/story/show" |
| 49 | + url := fmt.Sprintf("%s/%s", domain, issueID) |
| 50 | + return url, nil |
| 51 | +} |
| 52 | + |
| 53 | +func GetPivotalIssueID(branchName string) string { |
| 54 | + re := regexp.MustCompile(`([\d]+)`) |
| 55 | + matches := re.FindAllString(branchName, -1) |
| 56 | + |
| 57 | + if len(matches) == 0 { |
| 58 | + return "" |
| 59 | + } |
| 60 | + |
| 61 | + return matches[0] |
| 62 | +} |
| 63 | + |
| 64 | +// Start : Start an issue |
| 65 | +func (p *PivotalClient) Start(issueType string, issueID string) (string, error) { |
| 66 | + projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + } |
| 71 | + |
| 72 | + request := &pivotal.StoryRequest{} |
| 73 | + request.State = "started" |
| 74 | + |
| 75 | + _, _, err = p.client.Stories.Update(projectIdInt, issueIdInt, request) |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + fmt.Println(err) |
| 79 | + } |
| 80 | + |
| 81 | + branchName, err := p.GetBranchName(issueType, issueID) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return "", err |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Println("branch name:", branchName) |
| 88 | + return branchName, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (p *PivotalClient) Comment(branchName, comment string) error { |
| 92 | + fmt.Println(branchName) |
| 93 | + issueID := GetPivotalIssueID(branchName) |
| 94 | + fmt.Println(issueID) |
| 95 | + |
| 96 | + projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + fmt.Println(err) |
| 100 | + } |
| 101 | + |
| 102 | + commentObject := &pivotal.Comment{} |
| 103 | + commentObject.Text = comment |
| 104 | + |
| 105 | + _, _, err = p.client.Stories.AddComment(projectIdInt, issueIdInt, commentObject) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + fmt.Println(err) |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Println("added comment", comment) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (p *PivotalClient) PrepareCommitMessage(branchName, commitMessage string) string { |
| 116 | + issueID := GetPivotalIssueID(branchName) |
| 117 | + url, err := p.Browse(branchName) |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + return commitMessage |
| 121 | + } |
| 122 | + |
| 123 | + patchedCommitMessage := fmt.Sprintf(`[%s](%s)`, issueID, url) |
| 124 | + |
| 125 | + return patchedCommitMessage |
| 126 | +} |
| 127 | + |
| 128 | +func (p *PivotalClient) GetBranchName(issueType string, issueID string) (string, error) { |
| 129 | + projectIdInt, issueIdInt, _ := p.GetProjectIdAndIssueId(issueID) |
| 130 | + |
| 131 | + story, _, err := p.client.Stories.Get(projectIdInt, issueIdInt) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + fmt.Println(err) |
| 135 | + } |
| 136 | + |
| 137 | + issueTitleSlug := SlugifyTitle(story.Name) |
| 138 | + |
| 139 | + return fmt.Sprintf("%s/%s-%s", issueType, issueID, issueTitleSlug), nil |
| 140 | +} |
| 141 | + |
| 142 | +func (p *PivotalClient) GetProjectIdAndIssueId(issueID string) (int, int, error) { |
| 143 | + fields, err := Load() |
| 144 | + if err != nil { |
| 145 | + return 0, 0, err |
| 146 | + } |
| 147 | + |
| 148 | + projectIdInt, err := strconv.Atoi(fields["projectId"]) |
| 149 | + if err != nil { |
| 150 | + return 0, 0, err |
| 151 | + } |
| 152 | + |
| 153 | + fmt.Println("projectIdInt", projectIdInt) |
| 154 | + |
| 155 | + issueIdInt, err := strconv.Atoi(issueID) |
| 156 | + if err != nil { |
| 157 | + return 0, 0, err |
| 158 | + } |
| 159 | + |
| 160 | + fmt.Println("issueIdInt", issueIdInt) |
| 161 | + return projectIdInt, issueIdInt, nil |
| 162 | +} |
| 163 | + |
| 164 | +// FormatField : Returns a formatted field based on internal rules |
| 165 | +func (p *PivotalClient) FormatField(fieldName string, value string) string { |
| 166 | + return value |
| 167 | +} |
| 168 | + |
| 169 | +// Authenticate : Authenticates using the fields passed in |
| 170 | +func (p *PivotalClient) Authenticate(fields map[string]string) bool { |
| 171 | + pivotalClient := pivotal.NewClient(fields["apiToken"]) |
| 172 | + _, _, err := pivotalClient.Me.Get() |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + return false |
| 176 | + } |
| 177 | + |
| 178 | + p.client = pivotalClient |
| 179 | + p.config = fields |
| 180 | + |
| 181 | + return true |
| 182 | +} |
| 183 | + |
| 184 | +func (p *PivotalClient) GetAuthFields() map[string]bool { |
| 185 | + return map[string]bool{ |
| 186 | + "projectId": false, |
| 187 | + "apiToken": true, |
| 188 | + } |
| 189 | +} |
0 commit comments