@@ -13,6 +13,7 @@ import (
1313 "os/exec"
1414 "path"
1515 "path/filepath"
16+ "strconv"
1617 "strings"
1718
1819 "github.com/elastic/elastic-agent-changelog-tool/internal/changelog/fragment"
@@ -110,20 +111,20 @@ func (b Builder) Build(owner, repo string) error {
110111 // Applying heuristics to PR fields
111112 originalPR , err := FindOriginalPR (entry .LinkedPR [0 ], owner , repo , c )
112113 if err != nil {
113- log .Printf ("%s: check if the PR field is correct in changelog" , entry .File .Name )
114+ log .Printf ("%s: check if the PR field is correct in changelog: %s " , entry .File .Name , err . Error () )
114115 continue
115116 }
116117
117- b .changelog .Entries [i ].LinkedPR = []int {originalPR }
118+ b .changelog .Entries [i ].LinkedPR = []string {originalPR }
118119 }
119120
120121 if len (entry .LinkedIssue ) == 0 && len (b .changelog .Entries [i ].LinkedPR ) > 0 {
121- linkedIssues := []int {}
122+ linkedIssues := []string {}
122123
123- for _ , pr := range b .changelog .Entries [i ].LinkedPR {
124- tempIssues , err := FindIssues (graphqlClient , context .Background (), owner , repo , pr , 50 )
124+ for _ , prURL := range b .changelog .Entries [i ].LinkedPR {
125+ tempIssues , err := FindIssues (graphqlClient , context .Background (), owner , repo , prURL , 50 )
125126 if err != nil {
126- log .Printf ("%s: could not find linked issues for pr: %s/pull/%d " , entry .File .Name , entry .Repository , entry . LinkedPR )
127+ log .Printf ("%s: could not find linked issues for pr: %s: %s " , entry .File .Name , entry .LinkedPR , err . Error () )
127128 continue
128129 }
129130
@@ -134,7 +135,13 @@ func (b Builder) Build(owner, repo string) error {
134135 }
135136
136137 b .changelog .Entries [i ].LinkedIssue = linkedIssues
138+ } else if len (entry .LinkedIssue ) == 1 {
139+ _ , err := ExtractEventNumber ("issue" , entry .LinkedIssue [0 ])
140+ if err != nil {
141+ log .Printf ("%s: check if the issue field is correct in changelog: %s" , entry .File .Name , err .Error ())
142+ }
137143 }
144+
138145 }
139146
140147 data , err := yaml .Marshal (& b .changelog )
@@ -161,6 +168,39 @@ func collectFragment(fs afero.Fs, path string, info os.FileInfo, err error, file
161168 return nil
162169}
163170
171+ func ExtractEventNumber (linkType , eventURL string ) (string , error ) {
172+ urlParts := strings .Split (eventURL , "/" )
173+
174+ if len (urlParts ) < 1 {
175+ return "" , fmt .Errorf ("cant get event number" )
176+ }
177+
178+ switch linkType {
179+ // maybe use regex to validate instead of a simple string check
180+ case "pr" :
181+ if ! strings .Contains (eventURL , "pull" ) {
182+ return "" , fmt .Errorf ("link is invalid for pr" )
183+ }
184+ case "issue" :
185+ if ! strings .Contains (eventURL , "issues" ) {
186+ return "" , fmt .Errorf ("link is invalid for issue" )
187+ }
188+ }
189+
190+ return urlParts [len (urlParts )- 1 ], nil
191+ }
192+
193+ func CreateEventLink (linkType , owner , repo , eventID string ) string {
194+ switch linkType {
195+ case "issue" :
196+ return fmt .Sprintf ("https://github.com/%s/%s/issues/%s" , owner , repo , eventID )
197+ case "pr" :
198+ return fmt .Sprintf ("https://github.com/%s/%s/pull/%s" , owner , repo , eventID )
199+ default :
200+ panic ("wrong linkType" )
201+ }
202+ }
203+
164204func GetLatestCommitHash (fileName string ) (string , error ) {
165205 response , err := exec .Command ("git" , "log" , "--diff-filter=A" , "--format=%H" , "changelog/fragments/" + fileName ).Output ()
166206 if err != nil {
@@ -170,40 +210,61 @@ func GetLatestCommitHash(fileName string) (string, error) {
170210 return strings .ReplaceAll (string (response ), "\n " , "" ), nil
171211}
172212
173- func FindIssues (graphqlClient * github.ClientGraphQL , ctx context.Context , owner , name string , prID , issuesLen int ) ([]int , error ) {
174- issues , err := graphqlClient . PR . FindIssues ( ctx , owner , name , prID , issuesLen )
213+ func FindIssues (graphqlClient * github.ClientGraphQL , ctx context.Context , owner , name string , prURL string , issuesLen int ) ([]string , error ) {
214+ prID , err := ExtractEventNumber ( "pr" , prURL )
175215 if err != nil {
176216 return nil , err
177217 }
178218
179- return issues , nil
219+ prIDInt , _ := strconv .Atoi (prID )
220+
221+ issues , err := graphqlClient .PR .FindIssues (ctx , owner , name , prIDInt , issuesLen )
222+ if err != nil {
223+ return nil , err
224+ }
225+
226+ issueLinks := make ([]string , len (issues ))
227+
228+ for i , issue := range issues {
229+ issueLinks [i ] = CreateEventLink ("issue" , owner , name , issue )
230+ }
231+
232+ return issueLinks , nil
180233}
181234
182- func FillEmptyPRField (commitHash , owner , repo string , c * github.Client ) ([]int , error ) {
235+ func FillEmptyPRField (commitHash , owner , repo string , c * github.Client ) ([]string , error ) {
183236 pr , err := github .FindPR (context .Background (), c , owner , repo , commitHash )
184237 if err != nil {
185- return []int {}, err
238+ return []string {}, err
186239 }
187240
188- var prIDs []int
241+ prLinks := []string {}
189242
190243 for _ , item := range pr .Items {
191- prIDs = append (prIDs , item .PullRequestID )
244+ prLinks = append (prLinks , CreateEventLink ( "pr" , owner , repo , strconv . Itoa ( item .PullRequestID )) )
192245 }
193246
194- return prIDs , nil
247+ return prLinks , nil
195248}
196249
197- func FindOriginalPR (linkedPR int , owner , repo string , c * github.Client ) (int , error ) {
198- pr , _ , err := c . PullRequests . Get ( context . Background (), owner , repo , linkedPR )
250+ func FindOriginalPR (prURL string , owner , repo string , c * github.Client ) (string , error ) {
251+ linkedPR , err := ExtractEventNumber ( "pr" , prURL )
199252 if err != nil {
200- return 0 , err
253+ return "" , err
254+ }
255+
256+ linkedPRString , _ := strconv .Atoi (linkedPR )
257+
258+ pr , _ , err := c .PullRequests .Get (context .Background (), owner , repo , linkedPRString )
259+ if err != nil {
260+ return "" , err
201261 }
202262
203263 prID , err := github .TestStrategies (pr , & github.BackportPRNumber {}, & github.PRNumber {})
204264 if err != nil {
205- return 0 , err
265+ return "" , err
206266 }
207267
208- return prID , nil
268+ prLink := CreateEventLink ("pr" , owner , repo , strconv .Itoa (prID ))
269+ return prLink , nil
209270}
0 commit comments