Skip to content

Commit fdae68f

Browse files
authored
Feature/pr comments issue 70 (#73)
1 parent fd8955f commit fdae68f

File tree

5 files changed

+129
-26
lines changed

5 files changed

+129
-26
lines changed
File renamed without changes.

.github/workflows/branchPR.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ jobs:
2525
- name: Setup awesome-ci
2626
uses: fullstack-devops/awesome-ci-action@main
2727

28+
- name: Build "${{ matrix.arch }}"
29+
run: go build -v -ldflags "-X main.version=${{ needs.generate_infos.outputs.version }}" -o out/awesome-ci_${{ needs.generate_infos.outputs.version }}_${{ matrix.arch }}
30+
env:
31+
GOOS: linux
32+
GOARCH: "${{ matrix.arch }}"
33+
2834
- name: debugging git
35+
if: matrix.arch == 'amd64'
2936
run: |
3037
echo "git name-rev HEAD: $(git name-rev HEAD)"
3138
echo "git log -1 --pretty=format:"%s": $(git log -1 --pretty=format:"%s")"
3239
echo "git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@': $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
33-
- name: set build Infos
34-
run: awesome-ci pr info -number ${{ github.event.pull_request.number }}
40+
- name: test build Infos
41+
if: matrix.arch == 'amd64'
42+
run: out/awesome-ci_${{ needs.generate_infos.outputs.version }}_${{ matrix.arch }} pr info -number ${{ github.event.pull_request.number }}
3543
env:
3644
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3745

38-
- name: Build "${{ matrix.arch }}"
39-
run: go build -v -ldflags "-X main.version=${{ needs.generate_infos.outputs.version }}" -o out/awesome-ci_${{ needs.generate_infos.outputs.version }}_${{ matrix.arch }}
40-
env:
41-
GOOS: linux
42-
GOARCH: "${{ matrix.arch }}"
43-
4446
- name: Cache build outputs
4547
uses: actions/cache@v2
4648
env:

src/acigithub/issues.go

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,86 @@
11
package acigithub
22

33
import (
4+
"awesome-ci/src/tools"
5+
"fmt"
6+
"log"
7+
"strings"
8+
49
"github.com/google/go-github/v39/github"
510
)
611

12+
var (
13+
direction, sort = "asc", "created"
14+
)
15+
716
func GetIssueComments(issueNumber int, owner string, repo string) (issueComments []*github.IssueComment, err error) {
8-
// opts := github.IssueListCommentsOptions{}
9-
issueComments, _, err = GithubClient.Issues.ListComments(ctx, owner, repo, issueNumber, nil)
17+
var commentOpts = &github.IssueListCommentsOptions{
18+
Direction: &direction,
19+
// Sort: &sort,
20+
ListOptions: github.ListOptions{
21+
PerPage: 100,
22+
Page: 1,
23+
},
24+
}
25+
issueComments, _, err = GithubClient.Issues.ListComments(ctx, owner, repo, issueNumber, commentOpts)
26+
return
27+
}
28+
29+
func CommentHelpToPullRequest(number int) (err error) {
30+
if !isgithubRepository {
31+
log.Fatalln("make shure the GITHUB_REPOSITORY is available!")
32+
}
33+
owner, repo := tools.DevideOwnerAndRepo(githubRepository)
34+
35+
var commentOpts = &github.IssueListCommentsOptions{
36+
Direction: &direction,
37+
Sort: &sort,
38+
ListOptions: github.ListOptions{
39+
PerPage: 30,
40+
Page: 1,
41+
},
42+
}
43+
comments, _, err := GithubClient.Issues.ListComments(ctx, owner, repo, number, commentOpts)
44+
if err != nil {
45+
return fmt.Errorf("unable to list comments: %x", err)
46+
}
47+
48+
body := `<details><summary>Possible awesome-ci commands for this Pull Request</summary>` +
49+
`</br>aci_patch_level: major</br>aci_version_override: 2.1.0` +
50+
`</br></br>Need more help?</br>Have a look at <a href="https://github.com/fullstack-devops/awesome-ci" target="_blank">my repo</a>` +
51+
`</br>This message was created by awesome-ci and can be disabled by the env variable <code>ACI_SILENT=true</code></details>`
52+
53+
var prComment *github.IssueComment
54+
for _, prc := range comments {
55+
if strings.HasPrefix(*prc.Body, `<details><summary>Possible awesome-ci commands for this Pull Request</summary>`) {
56+
prComment = prc
57+
}
58+
}
59+
60+
if prComment == nil {
61+
var prComment = &github.IssueComment{
62+
Body: &body,
63+
}
64+
65+
err = CommentPullRequest(number, prComment)
66+
} else {
67+
// edit command if newer version deployed and commands changed
68+
if *prComment.Body != body {
69+
editedComment := &github.IssueComment{
70+
Body: &body,
71+
}
72+
_, _, err = GithubClient.Issues.EditComment(ctx, owner, repo, *prComment.ID, editedComment)
73+
}
74+
}
75+
return
76+
}
77+
78+
func CommentPullRequest(number int, comment *github.IssueComment) (err error) {
79+
if !isgithubRepository {
80+
log.Fatalln("make shure the GITHUB_REPOSITORY is available!")
81+
}
82+
owner, repo := tools.DevideOwnerAndRepo(githubRepository)
83+
84+
_, _, err = GithubClient.Issues.CreateComment(ctx, owner, repo, number, comment)
1085
return
1186
}

src/acigithub/pullrequest.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"awesome-ci/src/tools"
77
"fmt"
88
"log"
9+
"os"
10+
"regexp"
911
"strings"
1012

1113
"github.com/google/go-github/v39/github"
@@ -53,31 +55,55 @@ func GetPrInfos(prNumber int, mergeCommitSha string) (standardPrInfos *models.St
5355
return nil, nil, fmt.Errorf("no pull request found, please check if all resources are specified")
5456
}
5557

58+
isCI, isCIBool := os.LookupEnv("CI")
59+
_, isSilentBool := os.LookupEnv("ACI_SILENT")
60+
if isCIBool && !isSilentBool {
61+
if *prInfos.State == "open" && isCI == "true" {
62+
err = CommentHelpToPullRequest(*prInfos.Number)
63+
if err != nil {
64+
log.Println(err)
65+
}
66+
}
67+
}
68+
5669
prSHA := *prInfos.Head.SHA
5770
branchName := *prInfos.Head.Ref
5871
patchLevel := branchName[:strings.Index(branchName, "/")]
5972

60-
// if an comment exists with aci=major, make a major version!
73+
var version = ""
74+
// if an comment exists with aci_patch_level=major, make a major version!
6175
issueComments, err := GetIssueComments(prNumber, owner, repo)
6276
if err != nil {
6377
return nil, nil, err
6478
}
6579
for _, comment := range issueComments {
6680
// Must have permission in the repo to create a major version
6781
// MANNEQUIN|NONE https://docs.github.com/en/graphql/reference/enums#commentauthorassociation
68-
if strings.Contains(*comment.Body, "aci=major") && strings.Contains("OWNER|CONTRIBUTOR|COLLABORATOR", *comment.AuthorAssociation) {
69-
patchLevel = "major"
70-
break
82+
if strings.Contains("OWNER|CONTRIBUTOR|COLLABORATOR", *comment.AuthorAssociation) {
83+
aciVersionOverride := regexp.MustCompile(`aci_version_override: ([0-9]+\.[0-9]+\.[0-9]+)`)
84+
aciPatchLevel := regexp.MustCompile(`aci_patch_level: ([a-zA-Z]+)`)
85+
86+
if aciPatchLevel.MatchString(*comment.Body) {
87+
patchLevel = aciVersionOverride.FindStringSubmatch(*comment.Body)[1]
88+
break
89+
}
90+
if aciVersionOverride.MatchString(*comment.Body) {
91+
version = aciVersionOverride.FindStringSubmatch(*comment.Body)[1]
92+
break
93+
}
7194
}
7295
}
7396

7497
repositoryRelease, err := GetLatestReleaseVersion(owner, repo)
7598
if err != nil {
7699
return nil, nil, err
77100
}
78-
nextVersion, err := semver.IncreaseVersion(patchLevel, *repositoryRelease.TagName)
79-
if err != nil {
80-
return nil, nil, err
101+
102+
if version == "" {
103+
version, err = semver.IncreaseVersion(patchLevel, *repositoryRelease.TagName)
104+
if err != nil {
105+
return nil, nil, err
106+
}
81107
}
82108

83109
standardPrInfos = &models.StandardPrInfos{
@@ -90,7 +116,7 @@ func GetPrInfos(prNumber int, mergeCommitSha string) (standardPrInfos *models.St
90116
PatchLevel: patchLevel,
91117
LatestVersion: *repositoryRelease.TagName,
92118
CurrentVersion: "",
93-
NextVersion: nextVersion,
119+
NextVersion: version,
94120
MergeCommitSha: *prInfos.MergeCommitSHA,
95121
}
96122
return

tests/github_env

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
ACI_PR=0
2-
ACI_PR_SHA=790b5b99184e2ea1b4b2dc6912f3f4bf7bf8e1bc
3-
ACI_PR_SHA_SHORT=790b5b99
4-
ACI_PR_BRANCH=feature/awesome-feature
1+
ACI_PR=6
2+
ACI_PR_SHA=c2d509ad0f5291b0ce59ac636848177ea6c9d490
3+
ACI_PR_SHA_SHORT=c2d509ad
4+
ACI_PR_BRANCH=bugfix/test-else
55
ACI_OWNER=eksrha
66
ACI_REPO=playground
7-
ACI_PATCH_LEVEL=major
8-
ACI_VERSION=3.0.0
9-
ACI_LATEST_VERSION=2.0.0
10-
ACI_MERGE_COMMIT_SHA=de7de7bc7e09fb244fb1cd022efa49f7cfc9b289
7+
ACI_PATCH_LEVEL=bugfix
8+
ACI_VERSION=3.0.1
9+
ACI_LATEST_VERSION=3.0.0
10+
ACI_MERGE_COMMIT_SHA=d54c83dc7e2017c91931d4a6041abb240e0fe445
1111
ACI_RELEASE_ID=58357424

0 commit comments

Comments
 (0)