-
Notifications
You must be signed in to change notification settings - Fork 803
/
changelog.go
159 lines (145 loc) · 4.19 KB
/
changelog.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
)
var changelogCmd = &cobra.Command{
Use: "changelog <package search dir> <base commit> <target commit>",
Short: "Generates a CHANGELOG report in markdown format for the packages under the specified directory.",
Long: `The changelog command generates a CHANGELOG for all of the packages under the directory specified in <package dir>.
A table for added, removed, updated, and breaking changes will be created as required.`,
Args: func(cmd *cobra.Command, args []string) error {
// there should be exactly three args, a directory and two commit hashes
if err := cobra.ExactArgs(3)(cmd, args); err != nil {
return err
}
if strings.Index(args[2], ",") > -1 {
return errors.New("sequence of target commits is not supported")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return theChangelogCmd(args)
},
}
func init() {
rootCmd.AddCommand(changelogCmd)
}
func theChangelogCmd(args []string) error {
// TODO: refactor so that we don't depend on the packages command
rpt, err := thePackagesCmd(args)
if err != nil {
return err
}
if rpt.isEmpty() {
return nil
}
// there should only be one report, the delta between the base and target commits
if len(rpt.CommitsReports) > 1 {
panic("expected only one report")
}
for _, cr := range rpt.CommitsReports {
reportAddedPkgs(cr)
reportUpdatedPkgs(cr)
reportBreakingPkgs(cr)
reportRemovedPkgs(cr)
}
return nil
}
func reportAddedPkgs(pr pkgsReport) {
if len(pr.AddedPackages) == 0 {
return
}
fmt.Printf("### New Packages\n\n")
createTable(createTableRows(pr.AddedPackages))
}
func reportUpdatedPkgs(pr pkgsReport) {
if !pr.modPkgHasAdditions {
return
}
fmt.Printf("### Updated Packages\n\n")
updated := []string{}
for pkgName, pkgRpt := range pr.ModifiedPackages {
if pkgRpt.hasAdditiveChanges() && !pkgRpt.hasBreakingChanges() {
updated = append(updated, pkgName)
}
}
createTable(createTableRows(updated))
}
func reportBreakingPkgs(pr pkgsReport) {
if !pr.modPkgHasBreaking {
return
}
fmt.Printf("### BreakingChanges\n\n")
breaking := []string{}
for pkgName, pkgRpt := range pr.ModifiedPackages {
if pkgRpt.hasBreakingChanges() {
breaking = append(breaking, pkgName)
}
}
createTable(createTableRows(breaking))
}
func reportRemovedPkgs(pr pkgsReport) {
if len(pr.RemovedPackages) == 0 {
return
}
fmt.Printf("### Removed Packages\n\n")
createTable(createTableRows(pr.RemovedPackages))
}
type tableRow struct {
pkgName string
apiVersions []string
}
func createTableRows(pkgs []string) []tableRow {
entries := map[string][]string{}
for _, pkg := range pkgs {
// contains entries like "github.com/Azure/azure-sdk-for-go/services/consumption/mgmt/2018-08-31/consumption"
i := strings.LastIndex(pkg, "/")
pkgName := pkg[i+1:]
j := strings.LastIndex(pkg[:i], "/")
apiVer := pkg[j+1 : i]
if apis, ok := entries[pkgName]; ok {
entries[pkgName] = append(apis, apiVer)
} else {
entries[pkgName] = []string{apiVer}
}
}
// convert the map to a slice of tableRows
rows := []tableRow{}
for pkgName, apiVers := range entries {
sort.Strings(apiVers)
tr := tableRow{
pkgName: pkgName,
apiVersions: apiVers,
}
rows = append(rows, tr)
}
sort.Slice(rows, func(i, j int) bool {
return rows[i].pkgName < rows[j].pkgName
})
return rows
}
func createTable(rows []tableRow) {
fmt.Println("| Package Name | API Version |")
fmt.Println("| -----------: | :---------: |")
for _, row := range rows {
fmt.Println("| " + row.pkgName + " | " + strings.Join(row.apiVersions, "<br/>") + " |")
}
fmt.Println()
}