Skip to content

Commit 7452b5d

Browse files
authored
Merge pull request #84 from fullstack-devops/feature/hotfix-release
Feature/hotfix release
2 parents aaed04e + c8520ff commit 7452b5d

File tree

16 files changed

+1009
-41
lines changed

16 files changed

+1009
-41
lines changed

.github/workflows/Release.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: Publish Release
22

3+
concurrency:
4+
group: ci-${{ github.repository }}-release
5+
cancel-in-progress: false
6+
37
on:
48
push:
59
branches:

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ lint: ## Lint the files
1111
@golint -set_exit_status ${PKG_LIST}
1212

1313
test: ## Run unittests
14-
@go test -short ${PKG_LIST}
14+
export ACI_TEST_REPO=fullstack-devops/awesome-ci-test
15+
@go test ./...
1516

1617
race: dep ## Run data race detector
1718
@go test -race -short -v main.go

go.mod

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,46 @@ module awesome-ci
33
go 1.17
44

55
require (
6+
github.com/coreos/go-semver v0.3.0
7+
github.com/go-git/go-git/v5 v5.4.2
68
github.com/google/go-github/v39 v39.2.0
7-
github.com/xanzy/go-gitlab v0.55.1
9+
github.com/xanzy/go-gitlab v0.58.0
810
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
911
gopkg.in/yaml.v2 v2.4.0
1012
)
1113

1214
require (
15+
github.com/Microsoft/go-winio v0.5.2 // indirect
16+
github.com/ProtonMail/go-crypto v0.0.0-20220113124808-70ae35bab23f // indirect
17+
github.com/acomagu/bufpipe v1.0.3 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/emirpasic/gods v1.12.0 // indirect
1320
github.com/fatih/color v1.13.0 // indirect
21+
github.com/go-git/gcfg v1.5.0 // indirect
22+
github.com/go-git/go-billy/v5 v5.3.1 // indirect
1423
github.com/golang/protobuf v1.5.2 // indirect
1524
github.com/google/go-cmp v0.5.7 // indirect
1625
github.com/google/go-querystring v1.1.0 // indirect
1726
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
1827
github.com/hashicorp/go-hclog v1.1.0 // indirect
1928
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
29+
github.com/imdario/mergo v0.3.12 // indirect
30+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
31+
github.com/kevinburke/ssh_config v1.1.0 // indirect
2032
github.com/mattn/go-colorable v0.1.12 // indirect
2133
github.com/mattn/go-isatty v0.0.14 // indirect
34+
github.com/mitchellh/go-homedir v1.1.0 // indirect
35+
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/sergi/go-diff v1.2.0 // indirect
37+
github.com/stretchr/objx v0.1.1 // indirect
2238
github.com/stretchr/testify v1.7.0 // indirect
23-
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
39+
github.com/xanzy/ssh-agent v0.3.1 // indirect
40+
golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 // indirect
2441
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
25-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
42+
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect
2643
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
2744
google.golang.org/appengine v1.6.7 // indirect
2845
google.golang.org/protobuf v1.27.1 // indirect
46+
gopkg.in/warnings.v0 v0.1.2 // indirect
2947
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
3048
)

go.sum

Lines changed: 84 additions & 16 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func init() {
6262
releaseSet.Create.Fs.StringVar(&releaseSet.Create.Body, "body", "", "custom release message (markdow string or file)")
6363
releaseSet.Create.Fs.StringVar(&releaseSet.Create.PatchLevel, "patchLevel", "", "predefine version to Update")
6464
releaseSet.Create.Fs.BoolVar(&releaseSet.Create.DryRun, "dry-run", false, "make dry-run before writing version to Git by calling it")
65+
releaseSet.Create.Fs.BoolVar(&releaseSet.Create.Hotfix, "hotfix", false, "create a hotfix release")
6566
releaseSet.Create.Fs.Usage = func() {
6667
fmt.Println("Available options:")
6768
releaseSet.Create.Fs.PrintDefaults()
@@ -76,6 +77,7 @@ func init() {
7677
releaseSet.Publish.Fs.StringVar(&releaseSet.Publish.Assets, "assets", "", "upload assets to release. eg.: \"file=awesome-ci\"")
7778
releaseSet.Publish.Fs.Int64Var(&releaseSet.Publish.ReleaseId, "releaseid", 0, "publish an early defined release")
7879
releaseSet.Publish.Fs.BoolVar(&releaseSet.Publish.DryRun, "dry-run", false, "make dry-run before writing version to Git by calling it")
80+
releaseSet.Publish.Fs.BoolVar(&releaseSet.Publish.Hotfix, "hotfix", false, "create a hotfix release")
7981

8082
// parseJSON
8183
parseSet.Fs = flag.NewFlagSet("parse", flag.ExitOnError)
@@ -116,6 +118,7 @@ func printNoValidCommand(usage func()) {
116118
}
117119

118120
func main() {
121+
119122
flag.Usage = func() {
120123
fmt.Println("awesome-ci makes your CI easy.")
121124
fmt.Println(" Find more information and examples at: https://github.com/fullstack-devops/awesome-ci")

src/acigithub/github-client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package acigithub
22

33
import (
4+
"awesome-ci/src/tools"
45
"context"
56
"fmt"
67
"log"
@@ -17,15 +18,17 @@ var (
1718
githubServerUrl, isgithubServerUrl = os.LookupEnv("GITHUB_ENTERPRISE_SERVER_URL")
1819
githubRepository, isgithubRepository = os.LookupEnv("GITHUB_REPOSITORY")
1920
githubToken, isgithubToken = os.LookupEnv("GITHUB_TOKEN")
21+
owner, repo string
2022
)
2123

2224
// NewGitHubClient Creates a new GitHub Client
2325
// Needs the Environment Variables: GITHUB_TOKEN
2426
// Needs the optional Environment Variables: GITHUB_ENTERPRISE_SERVER_URL
2527
func NewGitHubClient() (githubClient *github.Client, err error) {
2628
if !isgithubToken {
27-
log.Fatalln("pleas set the GITHUB_TOKEN as environment variable!")
29+
log.Fatalln("please set the GITHUB_TOKEN as environment variable!")
2830
}
31+
2932
gitHubTs := oauth2.StaticTokenSource(
3033
&oauth2.Token{AccessToken: githubToken},
3134
)
@@ -44,6 +47,11 @@ func NewGitHubClient() (githubClient *github.Client, err error) {
4447
} else {
4548
githubClient = github.NewClient(githubTc)
4649
}
50+
51+
if isgithubRepository {
52+
owner, repo = tools.DevideOwnerAndRepo(githubRepository)
53+
}
54+
4755
GithubClient = githubClient
4856
return
4957
}

src/acigithub/issues.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
direction, sort = "asc", "created"
1414
)
1515

16-
func GetIssueComments(issueNumber int, owner string, repo string) (issueComments []*github.IssueComment, err error) {
16+
func GetIssueComments(issueNumber int) (issueComments []*github.IssueComment, err error) {
1717
var commentOpts = &github.IssueListCommentsOptions{
1818
Direction: &direction,
1919
// Sort: &sort,
@@ -30,7 +30,6 @@ func CommentHelpToPullRequest(number int) (err error) {
3030
if !isgithubRepository {
3131
log.Fatalln("make shure the GITHUB_REPOSITORY is available!")
3232
}
33-
owner, repo := tools.DevideOwnerAndRepo(githubRepository)
3433

3534
var commentOpts = &github.IssueListCommentsOptions{
3635
Direction: &direction,

src/acigithub/pullrequest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func GetPrInfos(prNumber int, mergeCommitSha string) (standardPrInfos *models.St
7272
var version = ""
7373
var latestVersion = ""
7474
// if an comment exists with aci_patch_level=major, make a major version!
75-
issueComments, err := GetIssueComments(prNumber, owner, repo)
75+
issueComments, err := GetIssueComments(prNumber)
7676
if err != nil {
7777
return nil, nil, err
7878
}
@@ -95,7 +95,7 @@ func GetPrInfos(prNumber int, mergeCommitSha string) (standardPrInfos *models.St
9595
}
9696

9797
if version == "" {
98-
repositoryRelease, err := GetLatestReleaseVersion(owner, repo)
98+
repositoryRelease, err := GetLatestReleaseVersion()
9999
if err == nil {
100100
latestVersion = *repositoryRelease.TagName
101101
version, err = semver.IncreaseVersion(patchLevel, latestVersion)

src/acigithub/release.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package acigithub
22

33
import (
44
"awesome-ci/src/tools"
5+
"errors"
56
"fmt"
67
"log"
78
"os"
89
"strconv"
910
"time"
1011

12+
"github.com/go-git/go-git/v5"
1113
"github.com/google/go-github/v39/github"
1214
)
1315

@@ -153,7 +155,54 @@ func PublishRelease(version string, releaseBranch string, body string, releaseId
153155
}
154156

155157
// GetLatestReleaseVersion
156-
func GetLatestReleaseVersion(owner string, repo string) (latestRelease *github.RepositoryRelease, err error) {
157-
latestRelease, _, err = GithubClient.Repositories.GetLatestRelease(ctx, owner, repo)
158-
return
158+
func GetLatestReleaseVersion() (latestRelease *github.RepositoryRelease, err error) {
159+
owner, repo := tools.DevideOwnerAndRepo(githubRepository)
160+
var releaseMap = make(map[string]*github.RepositoryRelease)
161+
162+
var listOptions github.ListOptions = github.ListOptions{
163+
PerPage: 100,
164+
Page: 0,
165+
}
166+
167+
for listOptions.Page >= 0 {
168+
releases, response, err := GithubClient.Repositories.ListReleases(ctx, owner, repo, &listOptions)
169+
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
for _, release := range releases {
175+
releaseMap[*release.TagName] = release
176+
}
177+
178+
if listOptions.Page == response.NextPage {
179+
break
180+
}
181+
182+
listOptions.Page = response.NextPage
183+
}
184+
185+
return findLatestRelease(`.`, releaseMap)
186+
}
187+
188+
func findLatestRelease(directory string, githubReleaseMap map[string]*github.RepositoryRelease) (latestRelease *github.RepositoryRelease, err error) {
189+
gitRepo, err := git.PlainOpen(directory)
190+
191+
if err != nil {
192+
return nil, err
193+
}
194+
195+
tags, err := tools.GetGitTagsUpToHead(gitRepo)
196+
197+
if err != nil {
198+
return nil, err
199+
}
200+
201+
for i := len(tags) - 1; i >= 0; i-- {
202+
if latestRelease, found := githubReleaseMap[tags[i].String()]; found {
203+
return latestRelease, nil
204+
}
205+
}
206+
207+
return nil, errors.New("could not find latest release")
159208
}

0 commit comments

Comments
 (0)