Skip to content

Commit a62e97e

Browse files
authored
Fix build logic (#109)
1 parent 44da1d6 commit a62e97e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Fix build logic for Github API calls
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
#description:
19+
20+
# Affected component; a word indicating the component this changeset affects.
21+
component:
22+
23+
# PR number; optional; the PR number that added the changeset.
24+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
25+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
26+
# Please provide it if you are adding a fragment for a different PR.
27+
#pr: 1234
28+
29+
# Issue number; optional; the GitHub issue related to this changeset (either closes or is part of).
30+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
31+
#issue: 1234

internal/changelog/builder.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"log"
12+
"net/url"
1213
"os"
1314
"os/exec"
1415
"path"
@@ -109,6 +110,12 @@ func (b Builder) Build(owner, repo string) error {
109110
b.changelog.Entries[i].LinkedPR = prIDs
110111
} else {
111112
// Applying heuristics to PR fields
113+
owner, repo, err := ExtractOwnerRepo(entry.LinkedPR[0])
114+
if err != nil {
115+
log.Printf("%s: check if the PR field is correct in changelog: %s", entry.File.Name, err.Error())
116+
continue
117+
}
118+
112119
originalPR, err := FindOriginalPR(entry.LinkedPR[0], owner, repo, c)
113120
if err != nil {
114121
log.Printf("%s: check if the PR field is correct in changelog: %s", entry.File.Name, err.Error())
@@ -122,6 +129,12 @@ func (b Builder) Build(owner, repo string) error {
122129
linkedIssues := []string{}
123130

124131
for _, prURL := range b.changelog.Entries[i].LinkedPR {
132+
owner, repo, err := ExtractOwnerRepo(prURL)
133+
if err != nil {
134+
log.Printf("%s: check if the PR field is correct in changelog: %s", entry.File.Name, err.Error())
135+
continue
136+
}
137+
125138
tempIssues, err := FindIssues(graphqlClient, context.Background(), owner, repo, prURL, 50)
126139
if err != nil {
127140
log.Printf("%s: could not find linked issues for pr: %s: %s", entry.File.Name, entry.LinkedPR, err.Error())
@@ -136,7 +149,7 @@ func (b Builder) Build(owner, repo string) error {
136149

137150
b.changelog.Entries[i].LinkedIssue = linkedIssues
138151
} else if len(entry.LinkedIssue) == 1 {
139-
_, err := ExtractEventNumber("issue", entry.LinkedIssue[0])
152+
_, err = ExtractEventNumber("issue", entry.LinkedIssue[0])
140153
if err != nil {
141154
log.Printf("%s: check if the issue field is correct in changelog: %s", entry.File.Name, err.Error())
142155
}
@@ -168,11 +181,25 @@ func collectFragment(fs afero.Fs, path string, info os.FileInfo, err error, file
168181
return nil
169182
}
170183

184+
func ExtractOwnerRepo(eventURL string) (string, string, error) {
185+
urlParsed, err := url.Parse(eventURL)
186+
if err != nil {
187+
return "", "", fmt.Errorf("invalid url: %w", err)
188+
}
189+
190+
urlParts := strings.Split(urlParsed.Path, "/")
191+
if len(urlParts) < 1 {
192+
return "", "", fmt.Errorf("can't get owner or repo")
193+
}
194+
195+
return urlParts[1], urlParts[2], nil
196+
}
197+
171198
func ExtractEventNumber(linkType, eventURL string) (string, error) {
172199
urlParts := strings.Split(eventURL, "/")
173200

174201
if len(urlParts) < 1 {
175-
return "", fmt.Errorf("cant get event number")
202+
return "", fmt.Errorf("can't get event number")
176203
}
177204

178205
switch linkType {

internal/changelog/builder_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ func TestExtractEventNumber(t *testing.T) {
7474
require.NoError(t, err)
7575
require.Equal(t, id, "99")
7676
}
77+
func TestExtractOwnerRepo(t *testing.T) {
78+
owner, repo, err := changelog.ExtractOwnerRepo("https://github.com/elastic/beats/pull/20186")
79+
require.NoError(t, err)
80+
require.Equal(t, owner, "elastic")
81+
require.Equal(t, repo, "beats")
82+
}

0 commit comments

Comments
 (0)