|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + jira "github.com/andygrunwald/go-jira/v2/onpremise" |
| 12 | + "sigs.k8s.io/yaml" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + colorCustomFieldID = "customfield_12320845" |
| 17 | + targetEndDateCustomFieldID = "customfield_12313942" |
| 18 | + reportCommentPrefix = "[report]" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + colorRank = []string{"Red", "Yellow", "Green"} |
| 23 | +) |
| 24 | + |
| 25 | +type Config struct { |
| 26 | + Title string `json:"title"` |
| 27 | + Reports []ReportConfig `json:"reports"` |
| 28 | +} |
| 29 | + |
| 30 | +type ReportConfig struct { |
| 31 | + Title string `json:"title"` |
| 32 | + Label string `json:"label"` |
| 33 | +} |
| 34 | + |
| 35 | +func main() { |
| 36 | + var ( |
| 37 | + jiraToken string |
| 38 | + jiraURL string |
| 39 | + configFile string |
| 40 | + ) |
| 41 | + flag.StringVar(&jiraToken, "jira-token", "", "JIRA Personal Access Token") |
| 42 | + flag.StringVar(&jiraURL, "jira-url", "", "JIRA Server URL") |
| 43 | + flag.StringVar(&configFile, "config-file", "config.yaml", "Config file location") |
| 44 | + |
| 45 | + if len(configFile) == 0 { |
| 46 | + configFile = "config.yaml" |
| 47 | + } |
| 48 | + |
| 49 | + flag.Parse() |
| 50 | + |
| 51 | + tp := jira.BearerAuthTransport{ |
| 52 | + Token: jiraToken, |
| 53 | + } |
| 54 | + client, err := jira.NewClient(jiraURL, tp.Client()) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + |
| 59 | + var config Config |
| 60 | + configFileContent, err := os.ReadFile(configFile) |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + if err := yaml.Unmarshal(configFileContent, &config); err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + |
| 68 | + now := time.Now().UTC() |
| 69 | + fmt.Println(config.Title) |
| 70 | + _, week := now.ISOWeek() |
| 71 | + fmt.Printf("Week %d - %s\n", week, now.Format(time.RFC822)) |
| 72 | + fmt.Println() |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + for _, report := range config.Reports { |
| 76 | + fmt.Println(report.Title) |
| 77 | + if err := generateReport(ctx, report, client); err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + fmt.Println() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func generateReport(ctx context.Context, config ReportConfig, client *jira.Client) error { |
| 85 | + jql := fmt.Sprintf( |
| 86 | + `project = "SDE" AND labels = %q AND Status in ("To Do","In Progress") ORDER BY priority DESC`, |
| 87 | + config.Label, |
| 88 | + ) |
| 89 | + issues, _, err := client.Issue.Search(ctx, jql, &jira.SearchOptions{}) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + issuesByColor := groupIssuesByColor(issues) |
| 95 | + for _, issues := range issuesByColor { |
| 96 | + for _, i := range issues { |
| 97 | + issue, _, err := client.Issue.Get(ctx, i.Key, &jira.GetQueryOptions{}) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + printIssue(issue) |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func printIssue(issue *jira.Issue) { |
| 109 | + fmt.Printf("- [%s] %s\n", issue.Key, issue.Fields.Summary) |
| 110 | + fmt.Printf(" Status:\t %s\n", issue.Fields.Status.Name) |
| 111 | + color := getColor(issue) |
| 112 | + if len(color) > 0 { |
| 113 | + fmt.Printf(" Color:\t %s\n", getColor(issue)) |
| 114 | + } |
| 115 | + targedEnd := getCustomField(issue, targetEndDateCustomFieldID) |
| 116 | + if len(targedEnd) > 0 { |
| 117 | + fmt.Printf(" Target end:\t %s\n", getCustomField(issue, targetEndDateCustomFieldID)) |
| 118 | + } |
| 119 | + comment := statusComment(issue) |
| 120 | + if len(comment) > 0 { |
| 121 | + fmt.Printf(" Comment:\t %s\n", comment) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func getColor(issue *jira.Issue) string { |
| 126 | + colorField := issue.Fields.Unknowns[colorCustomFieldID] |
| 127 | + var c string |
| 128 | + if colorFieldMap, ok := colorField.(map[string]interface{}); ok { |
| 129 | + c = colorFieldMap["value"].(string) |
| 130 | + } |
| 131 | + if c == "Not Selected" { |
| 132 | + return "" |
| 133 | + } |
| 134 | + return c |
| 135 | +} |
| 136 | + |
| 137 | +func getCustomField(issue *jira.Issue, customFieldID string) string { |
| 138 | + field := issue.Fields.Unknowns[customFieldID] |
| 139 | + if field == nil { |
| 140 | + return "" |
| 141 | + } |
| 142 | + return fmt.Sprintf("%s", field) |
| 143 | +} |
| 144 | + |
| 145 | +func statusComment(issue *jira.Issue) string { |
| 146 | + if issue.Fields.Comments == nil { |
| 147 | + return "" |
| 148 | + } |
| 149 | + var latestReportComment string |
| 150 | + for _, c := range issue.Fields.Comments.Comments { |
| 151 | + trimmedBody := strings.TrimSpace(c.Body) |
| 152 | + if strings.HasPrefix(trimmedBody, reportCommentPrefix) { |
| 153 | + latestReportComment = strings.TrimSpace(trimmedBody[len(reportCommentPrefix):]) |
| 154 | + } |
| 155 | + } |
| 156 | + latestReportComment = strings.ReplaceAll(latestReportComment, "\n", "") |
| 157 | + return strings.ReplaceAll(latestReportComment, "\r", "") |
| 158 | +} |
| 159 | + |
| 160 | +func groupIssuesByColor(issues []jira.Issue) [][]jira.Issue { |
| 161 | + issuesByColor := make([][]jira.Issue, len(colorRank)+1) |
| 162 | + colorIndex := map[string]int{} |
| 163 | + for i, color := range colorRank { |
| 164 | + colorIndex[color] = i |
| 165 | + } |
| 166 | + for _, issue := range issues { |
| 167 | + color := getColor(&issue) |
| 168 | + |
| 169 | + i := len(issuesByColor) - 1 |
| 170 | + if colorIndex, ok := colorIndex[color]; ok { |
| 171 | + i = colorIndex |
| 172 | + } |
| 173 | + issuesByColor[i] = append(issuesByColor[i], issue) |
| 174 | + } |
| 175 | + |
| 176 | + return issuesByColor |
| 177 | +} |
0 commit comments