Skip to content

Commit 37c0f5d

Browse files
jajitarizwanreza
authored andcommitted
Remove github_host var, rename github_token to github_access_token, match the release sources to the release by org
1 parent be572bb commit 37c0f5d

File tree

17 files changed

+146
-133
lines changed

17 files changed

+146
-133
lines changed

.github/workflows/create-debugging-artifact.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
RELEEN_GITHUB_TOKEN: ${{ secrets.RELEEN_GITHUB_TOKEN }}
4242
run: |
4343
export GITHUB_TOKEN="${RELEEN_GITHUB_TOKEN}"
44+
export GITHUB_ACCESS_TOKEN="${RELEEN_GITHUB_TOKEN}"
4445
go test ./...
4546
4647
- name: Acceptance Tests

.github/workflows/release.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
RELEEN_GITHUB_TOKEN: ${{ secrets.RELEEN_GITHUB_TOKEN }}
3939
run: |
4040
export GITHUB_TOKEN="${RELEEN_GITHUB_TOKEN}"
41+
export GITHUB_ACCESS_TOKEN="${RELEEN_GITHUB_TOKEN}"
4142
go test --covermode=atomic --coverprofile=kiln-${{github.sha}}-unit-test-code-coverage.out ./...
4243
4344
- name: Archive Unit Test Code Coverage Output
@@ -53,6 +54,7 @@ jobs:
5354
run: |
5455
set -euo pipefail
5556
export GITHUB_TOKEN="${RELEEN_GITHUB_TOKEN}"
57+
export GITHUB_ACCESS_TOKEN="${RELEEN_GITHUB_TOKEN}"
5658
5759
set -x
5860
go test -v --timeout 24h --tags acceptance github.com/pivotal-cf/kiln/internal/acceptance/workflows
@@ -80,3 +82,5 @@ jobs:
8082
args: release --clean
8183
env:
8284
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
run: |
5252
set -euo pipefail
5353
export GITHUB_TOKEN="${RELEEN_GITHUB_TOKEN}"
54+
export GITHUB_ACCESS_TOKEN="${RELEEN_GITHUB_TOKEN}"
5455
5556
set -x
5657
go test -v --timeout 15m --tags acceptance github.com/pivotal-cf/kiln/internal/acceptance/workflows

internal/acceptance/workflows/generating_release_notes.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Feature: As a robot, I want to generate release notes
1010
| --name=hello-release |
1111
| --version=v0.1.5 |
1212
| --without-download |
13-
| --variable=github_token="${GITHUB_TOKEN}" |
13+
| --variable=github_access_token="${GITHUB_TOKEN}" |
1414
And I write file "version"
1515
| 0.1.4 |
1616
And I execute git add Kilnfile.lock version

internal/acceptance/workflows/scenario/step_funcs_github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func githubRepoHasReleaseWithTag(ctx context.Context, repoOrg, repoName, tag str
1313
if err != nil {
1414
return err
1515
}
16-
ghAPI, err := gh.Client(ctx, "", accessToken)
16+
ghAPI, err := gh.Client(ctx, "", accessToken, accessToken)
1717
if err != nil {
1818
return fmt.Errorf("failed to setup github client: %w", err)
1919
}

internal/acceptance/workflows/testdata/tiles/v1/Kilnfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ slug: crhntr-hello
33
release_sources:
44
- type: github
55
org: crhntr
6-
github_token: $(variable "github_token")
6+
github_token: $(variable "github_access_token")
77
- type: bosh.io
88
releases:
99
- name: bpm

internal/acceptance/workflows/testdata/tiles/v2/Kilnfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ slug: crhntr-hello
33
release_sources:
44
- type: github
55
org: crhntr
6-
github_token: $(variable "github_token")
6+
github_token: $(variable "github_access_token")
77
- type: bosh.io
88
releases:
99
- name: bpm

internal/commands/release_notes.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const releaseDateFormat = "2006-01-02"
2828

2929
type ReleaseNotes struct {
3030
Options struct {
31-
ReleaseDate string `long:"release-date" short:"d" description:"release date of the tile"`
32-
TemplateName string `long:"template" short:"t" description:"path to template"`
33-
GithubIssuesServiceToken string `long:"github-token" short:"g" description:"auth token for fetching issues and milestones with labels" env:"GITHUB_TOKEN"`
34-
GithubHost string `long:"github-host" description:"set this when you are using GitHub enterprise to fetch issues, milestones or notes for bosh releases" env:"GITHUB_HOST"`
35-
Kilnfile string `long:"kilnfile" short:"k" description:"path to Kilnfile"`
36-
DocsFile string `long:"update-docs" short:"u" description:"path to docs file to update"`
37-
Window string `long:"window" short:"w" description:"GA window for release notes" default:"ga"`
38-
VariableFiles []string `long:"variables-file" short:"vf" description:"path to a file containing variables to interpolate"`
39-
Variables []string `long:"variable" short:"vr" description:"key value pairs of variables to interpolate"`
31+
ReleaseDate string `long:"release-date" short:"d" description:"release date of the tile"`
32+
TemplateName string `long:"template" short:"t" description:"path to template"`
33+
GithubAccessToken string `long:"github_access_token" short:"g" description:"auth token for github.com" env:"GITHUB_ACCESS_TOKEN"`
34+
GithubEnterpriseAccessToken string `long:"github_enterprise_access_token" short:"ge" description:"auth token for github enterprise" env:"GITHUB_ENTERPRISE_ACCESS_TOKEN"`
35+
Kilnfile string `long:"kilnfile" short:"k" description:"path to Kilnfile"`
36+
DocsFile string `long:"update-docs" short:"u" description:"path to docs file to update"`
37+
Window string `long:"window" short:"w" description:"GA window for release notes" default:"ga"`
38+
VariableFiles []string `long:"variables-file" short:"vf" description:"path to a file containing variables to interpolate"`
39+
Variables []string `long:"variable" short:"vr" description:"key value pairs of variables to interpolate"`
4040
notes.IssuesQuery
4141
notes.TrainstatQuery
4242
}
@@ -49,10 +49,10 @@ type ReleaseNotes struct {
4949
fetchNotesData FetchNotesData
5050
variablesService baking.TemplateVariablesService
5151

52-
repoOwner, repoName string
52+
repoHost, repoOwner, repoName string
5353
}
5454

55-
type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery notes.IssuesQuery, trainstatClient notes.TrainstatNotesFetcher, variables map[string]any) (notes.Data, error)
55+
type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoHost, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery notes.IssuesQuery, trainstatClient notes.TrainstatNotesFetcher, variables map[string]any) (notes.Data, error)
5656

5757
func NewReleaseNotesCommand() (ReleaseNotes, error) {
5858
return ReleaseNotes{
@@ -82,16 +82,16 @@ func (r ReleaseNotes) Execute(args []string) error {
8282
if err != nil {
8383
return fmt.Errorf("failed to parse template variables: %s", err)
8484
}
85-
if varValue, ok := templateVariables["github_token"]; !ok && r.Options.GithubIssuesServiceToken != "" {
86-
templateVariables["github_token"] = r.Options.GithubIssuesServiceToken
87-
} else if ok && r.Options.GithubIssuesServiceToken == "" {
88-
r.Options.GithubIssuesServiceToken = varValue.(string)
85+
if varValue, ok := templateVariables["github_access_token"]; !ok && r.Options.GithubAccessToken != "" {
86+
templateVariables["github_access_token"] = r.Options.GithubAccessToken
87+
} else if ok && r.Options.GithubAccessToken == "" {
88+
r.Options.GithubAccessToken = varValue.(string)
8989
}
9090

91-
if varValue, ok := templateVariables["github_host"]; !ok && r.Options.GithubHost != "" {
92-
templateVariables["github_host"] = r.Options.GithubHost
93-
} else if ok && r.Options.GithubHost == "" {
94-
r.Options.GithubHost = varValue.(string)
91+
if varValue, ok := templateVariables["github_enterprise_access_token"]; !ok && r.Options.GithubAccessToken != "" {
92+
templateVariables["github_enterprise_access_token"] = r.Options.GithubEnterpriseAccessToken
93+
} else if ok && r.Options.GithubEnterpriseAccessToken == "" {
94+
r.Options.GithubEnterpriseAccessToken = varValue.(string)
9595
}
9696

9797
ctx := context.Background()
@@ -106,18 +106,17 @@ func (r ReleaseNotes) Execute(args []string) error {
106106
}
107107

108108
var client *github.Client
109-
if r.Options.GithubIssuesServiceToken != "" {
110-
client, err = gh.Client(ctx, r.Options.GithubHost, r.Options.GithubIssuesServiceToken)
111-
if err != nil {
112-
return fmt.Errorf("failed to setup github client: %w", err)
113-
}
109+
110+
client, err = gh.Client(ctx, r.repoHost, r.Options.GithubAccessToken, r.Options.GithubEnterpriseAccessToken)
111+
if err != nil {
112+
return fmt.Errorf("failed to setup github client: %w", err)
114113
}
115114

116115
trainstatClient := notes.NewTrainstatClient(r.Options.TrainstatQuery.TrainstatURL)
117116

118117
_ = notes.FetchData // fetchNotesData is github.com/pivotal/kiln/internal/notes.FetchData
119118
data, err := r.fetchNotesData(ctx,
120-
r.repository, client, r.repoOwner, r.repoName,
119+
r.repository, client, r.repoHost, r.repoOwner, r.repoName,
121120
r.Options.Kilnfile,
122121
nonFlagArgs[0], nonFlagArgs[1],
123122
r.Options.IssuesQuery,
@@ -196,12 +195,13 @@ func (r *ReleaseNotes) initRepo() error {
196195
return fmt.Errorf("release-notes must be run from the root of the repository (use --kilnfile flag to specify which tile to build)")
197196
}
198197

199-
repoOwner, repoName, err := getGithubRemoteRepoOwnerAndName(repo)
198+
repoHost, repoOwner, repoName, err := getGithubRemoteHostRepoOwnerAndName(repo)
200199
if err != nil {
201200
return err
202201
}
203202

204203
r.repository = repo
204+
r.repoHost = repoHost
205205
r.repoName = repoName
206206
r.repoOwner = repoOwner
207207

@@ -243,11 +243,8 @@ func (r ReleaseNotes) checkInputs(nonFlagArgs []string) error {
243243
}
244244
}
245245

246-
if r.Options.GithubIssuesServiceToken == "" &&
247-
(r.Options.IssueMilestone != "" ||
248-
len(r.Options.IssueIDs) > 0 ||
249-
len(r.Options.IssueLabels) > 0) {
250-
return errors.New("github-token (env: GITHUB_TOKEN) must be set to interact with the github api")
246+
if r.Options.GithubEnterpriseAccessToken == "" && r.Options.GithubAccessToken == "" {
247+
return errors.New("github_access_token(env: GITHUB_ACCESS_TOKEN) and/or github_enterprise_access_token(env: GITHUB_ENTERPRISE_ACCESS_TOKEN) must be set to interact with the github api")
251248
}
252249

253250
if r.Options.DocsFile != "" {
@@ -279,25 +276,25 @@ func (r ReleaseNotes) parseReleaseDate() (time.Time, error) {
279276
return releaseDate, nil
280277
}
281278

282-
func getGithubRemoteRepoOwnerAndName(repo *git.Repository) (string, string, error) {
279+
func getGithubRemoteHostRepoOwnerAndName(repo *git.Repository) (string, string, string, error) {
283280
var remoteURL string
284281
remote, err := repo.Remote("origin")
285282
if err != nil {
286-
return "", "", err
283+
return "", "", "", err
287284
}
288285
config := remote.Config()
289286
for _, u := range config.URLs {
290287
remoteURL = u
291288
break
292289
}
293290
if remoteURL == "" {
294-
return "", "", fmt.Errorf("remote github URL not found for repo")
291+
return "", "", "", fmt.Errorf("remote github URL not found for repo")
295292
}
296293

297-
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(remoteURL)
294+
repoHost, repoOwner, repoName, err := gh.RepositoryHostOwnerAndNameFromPath(remoteURL)
298295
if err != nil {
299-
return "", "", err
296+
return "", "", "", err
300297
}
301298

302-
return repoOwner, repoName, nil
299+
return repoHost, repoOwner, repoName, nil
303300
}

internal/commands/release_notes_test.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
5757
}
5858

5959
var (
60-
tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string
60+
tileRepoHost, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string
6161

6262
issuesQuery notes.IssuesQuery
6363
repository *git.Repository
@@ -69,12 +69,13 @@ func TestReleaseNotes_Execute(t *testing.T) {
6969
rn := ReleaseNotes{
7070
Writer: &out,
7171
repository: nonNilRepo,
72+
repoHost: "github.com",
7273
repoOwner: "bunch",
7374
repoName: "banana",
7475
readFile: readFileFunc,
75-
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, tro, trn, kfp, ir, fr string, iq notes.IssuesQuery, _ notes.TrainstatNotesFetcher, __ map[string]any) (notes.Data, error) {
76+
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, trh, tro, trn, kfp, ir, fr string, iq notes.IssuesQuery, _ notes.TrainstatNotesFetcher, __ map[string]any) (notes.Data, error) {
7677
ctx, repository, client = c, repo, ghc
77-
tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision = tro, trn, kfp, ir, fr
78+
tileRepoHost, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision = trh, tro, trn, kfp, ir, fr
7879
issuesQuery = iq
7980
return notes.Data{
8081
ReleaseDate: mustParseTime(time.Parse(releaseDateFormat, "2021-11-04")),
@@ -104,12 +105,12 @@ func TestReleaseNotes_Execute(t *testing.T) {
104105
},
105106
}
106107

107-
rn.Options.GithubIssuesServiceToken = "secret"
108+
rn.Options.GithubAccessToken = "secret"
108109

109110
err := rn.Execute([]string{
110111
"--kilnfile=tile/Kilnfile",
111112
"--release-date=2021-11-04",
112-
"--github-token=lemon",
113+
"--github_access_token=lemon",
113114
"--github-issue-milestone=smoothie",
114115
"--github-issue-label=tropical",
115116
"--github-issue=54000",
@@ -124,6 +125,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
124125
please.Expect(repository).NotTo(BeNil())
125126
please.Expect(client).NotTo(BeNil())
126127

128+
please.Expect(tileRepoHost).To(Equal("github.com"))
127129
please.Expect(tileRepoOwner).To(Equal("bunch"))
128130
please.Expect(tileRepoName).To(Equal("banana"))
129131
please.Expect(kilnfilePath).To(Equal("tile/Kilnfile"))
@@ -188,6 +190,7 @@ func TestReleaseNotes_checkInputs(t *testing.T) {
188190

189191
rn := ReleaseNotes{}
190192
rn.Options.ReleaseDate = `some-date`
193+
rn.Options.GithubAccessToken = "test-token"
191194
err := rn.checkInputs([]string{"a", "b"})
192195
please.Expect(err).To(MatchError(ContainSubstring("cannot parse")))
193196
})
@@ -197,34 +200,17 @@ func TestReleaseNotes_checkInputs(t *testing.T) {
197200
please := NewWithT(t)
198201

199202
rn := ReleaseNotes{}
200-
rn.Options.IssueMilestone = "s"
201203
err := rn.checkInputs([]string{"a", "b"})
202-
please.Expect(err).To(MatchError(ContainSubstring("github-token")))
203-
})
204-
205-
t.Run("ids", func(t *testing.T) {
206-
please := NewWithT(t)
207-
208-
rn := ReleaseNotes{}
209-
rn.Options.IssueIDs = []string{"s"}
210-
err := rn.checkInputs([]string{"a", "b"})
211-
please.Expect(err).To(MatchError(ContainSubstring("github-token")))
212-
})
213-
214-
t.Run("labels", func(t *testing.T) {
215-
please := NewWithT(t)
216-
217-
rn := ReleaseNotes{}
218-
rn.Options.IssueLabels = []string{"s"}
219-
err := rn.checkInputs([]string{"a", "b"})
220-
please.Expect(err).To(MatchError(ContainSubstring("github-token")))
204+
please.Expect(err).To(MatchError(ContainSubstring("github_access_token")))
205+
please.Expect(err).To(MatchError(ContainSubstring("github_enterprise_access_token")))
221206
})
222207

223208
t.Run("exp", func(t *testing.T) {
224209
please := NewWithT(t)
225210

226211
rn := ReleaseNotes{}
227212
rn.Options.IssueTitleExp = "s"
213+
rn.Options.GithubEnterpriseAccessToken = "test-token"
228214
err := rn.checkInputs([]string{"a", "b"})
229215
please.Expect(err).NotTo(HaveOccurred())
230216
})
@@ -243,8 +229,9 @@ func Test_getGithubRemoteRepoOwnerAndName(t *testing.T) {
243229
"https://github.com/pivotal-cf/kiln",
244230
},
245231
})
246-
o, r, err := getGithubRemoteRepoOwnerAndName(repo)
232+
h, o, r, err := getGithubRemoteHostRepoOwnerAndName(repo)
247233
please.Expect(err).NotTo(HaveOccurred())
234+
please.Expect(h).To(Equal("github.com"))
248235
please.Expect(o).To(Equal("pivotal-cf"))
249236
please.Expect(r).To(Equal("kiln"))
250237
})
@@ -259,8 +246,9 @@ func Test_getGithubRemoteRepoOwnerAndName(t *testing.T) {
259246
"[email protected]:pivotal-cf/kiln.git",
260247
},
261248
})
262-
o, r, err := getGithubRemoteRepoOwnerAndName(repo)
249+
h, o, r, err := getGithubRemoteHostRepoOwnerAndName(repo)
263250
please.Expect(err).NotTo(HaveOccurred())
251+
please.Expect(h).To(Equal("github.com"))
264252
please.Expect(o).To(Equal("pivotal-cf"))
265253
please.Expect(r).To(Equal("kiln"))
266254
})
@@ -269,7 +257,7 @@ func Test_getGithubRemoteRepoOwnerAndName(t *testing.T) {
269257
please := NewWithT(t)
270258

271259
repo, _ := git.Init(memory.NewStorage(), memfs.New())
272-
_, _, err := getGithubRemoteRepoOwnerAndName(repo)
260+
_, _, _, err := getGithubRemoteHostRepoOwnerAndName(repo)
273261
please.Expect(err).To(MatchError(ContainSubstring("not found")))
274262
})
275263

@@ -289,8 +277,9 @@ func Test_getGithubRemoteRepoOwnerAndName(t *testing.T) {
289277
"[email protected]:pivotal-cf/kiln.git",
290278
},
291279
})
292-
o, _, err := getGithubRemoteRepoOwnerAndName(repo)
280+
h, o, _, err := getGithubRemoteHostRepoOwnerAndName(repo)
293281
please.Expect(err).NotTo(HaveOccurred())
282+
please.Expect(h).To(Equal("github.com"))
294283
please.Expect(o).To(Equal("pivotal-cf"), "it uses the remote with name 'origin'")
295284
})
296285
}

internal/component/github_release_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewGithubReleaseSource(c cargo.ReleaseSourceConfig) *GithubReleaseSource {
4343
if c.Org == "" {
4444
panic("no github org passed for github release source")
4545
}
46-
githubClient, err := c.GitHubClient(context.TODO())
46+
githubClient, err := gh.Client(context.TODO(), "", c.GithubToken, c.GithubToken) // host is github.com by default
4747
if err != nil {
4848
panic(err)
4949
}

0 commit comments

Comments
 (0)