Skip to content

Commit 12bb7cf

Browse files
committed
feat: exclude branches.
1 parent 3945ec0 commit 12bb7cf

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

core/core.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func process(workDir string, config *types.Configuration) error {
6666

6767
log.Printf("Latest tag: %s", latestTagName)
6868

69-
branches, err := getBranches(config.ExperimentalBranchName, config.Debug)
69+
branches, err := getBranches(config.ExperimentalBranchName, config.ExcludedBranches, config.Debug)
7070
if err != nil {
7171
return err
7272
}
@@ -129,7 +129,7 @@ func getLatestReleaseTagName(owner, repositoryName string) (string, error) {
129129
return gh.GetLatestReleaseTagName(owner, repositoryName)
130130
}
131131

132-
func getBranches(experimentalBranchName string, debug bool) ([]string, error) {
132+
func getBranches(experimentalBranchName string, excludedBranches []string, debug bool) ([]string, error) {
133133
var branches []string
134134

135135
if len(experimentalBranchName) > 0 {
@@ -140,7 +140,14 @@ func getBranches(experimentalBranchName string, debug bool) ([]string, error) {
140140
if err != nil {
141141
return nil, err
142142
}
143-
branches = append(branches, gitBranches...)
143+
144+
for _, branch := range gitBranches {
145+
if containsBranch(excludedBranches, branch) {
146+
continue
147+
}
148+
149+
branches = append(branches, branch)
150+
}
144151

145152
if len(branches) == 0 {
146153
log.Println("[WARN] no branch.")
@@ -149,6 +156,16 @@ func getBranches(experimentalBranchName string, debug bool) ([]string, error) {
149156
return branches, nil
150157
}
151158

159+
func containsBranch(branches []string, branch string) bool {
160+
for _, v := range branches {
161+
if baseRemote+v == branch {
162+
return true
163+
}
164+
}
165+
166+
return false
167+
}
168+
152169
func createSiteDirectory() (string, error) {
153170
currentDir, err := os.Getwd()
154171
if err != nil {

core/core_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package core
22

33
import (
44
"io/ioutil"
5+
"log"
56
"os"
67
"path/filepath"
8+
"strings"
79
"testing"
810

911
"github.com/containous/structor/types"
12+
"github.com/ldez/go-git-cmd-wrapper/git"
1013
"github.com/stretchr/testify/assert"
1114
"github.com/stretchr/testify/require"
1215
)
@@ -220,3 +223,67 @@ func Test_createDirectory(t *testing.T) {
220223
})
221224
}
222225
}
226+
227+
func Test_getBranches(t *testing.T) {
228+
git.CmdExecutor = func(name string, debug bool, args ...string) (string, error) {
229+
if debug {
230+
log.Println(name, strings.Join(args, " "))
231+
}
232+
return `
233+
origin/v1.3
234+
origin/v1.1
235+
origin/v1.2
236+
`, nil
237+
}
238+
239+
testCases := []struct {
240+
desc string
241+
experimentalBranchName string
242+
excludedBranches []string
243+
expected []string
244+
}{
245+
{
246+
desc: "all existing branches",
247+
expected: []string{
248+
"origin/v1.3",
249+
"origin/v1.2",
250+
"origin/v1.1",
251+
},
252+
},
253+
{
254+
desc: "add experimental branch",
255+
experimentalBranchName: "master",
256+
expected: []string{
257+
"origin/master",
258+
"origin/v1.3",
259+
"origin/v1.2",
260+
"origin/v1.1",
261+
},
262+
},
263+
{
264+
desc: "exclude one branch",
265+
excludedBranches: []string{"v1.1"},
266+
expected: []string{
267+
"origin/v1.3",
268+
"origin/v1.2",
269+
},
270+
},
271+
{
272+
desc: "exclude all branches",
273+
excludedBranches: []string{"v1.1", "v1.2", "v1.3"},
274+
expected: nil,
275+
},
276+
}
277+
278+
for _, test := range testCases {
279+
test := test
280+
t.Run(test.desc, func(t *testing.T) {
281+
t.Parallel()
282+
283+
branches, err := getBranches(test.experimentalBranchName, test.excludedBranches, true)
284+
require.NoError(t, err)
285+
286+
assert.Equal(t, test.expected, branches)
287+
})
288+
}
289+
}

types/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Configuration struct {
1111
DockerfileURL string `short:"d" long:"dockerfile-url" description:"Use this Dockerfile when --dockerfile-name is not found. Can be a file path. [required]"`
1212
DockerfileName string `long:"dockerfile-name" description:"Search and use this Dockerfile in the repository (in './docs/' or in './') for building documentation."`
1313
ExperimentalBranchName string `long:"exp-branch" description:"Build a branch as experimental."`
14+
ExcludedBranches []string `long:"exclude" description:"Exclude branches from the documentation generation."`
1415
DockerImageName string `long:"image-name" description:"Docker image name."`
1516
Menu *MenuFiles `long:"menu" description:"Menu templates files."`
1617
RequirementsURL string `long:"rqts-url" description:"Use this requirements.txt to merge with the current requirements.txt. Can be a file path."`

0 commit comments

Comments
 (0)