@@ -9,41 +9,23 @@ import (
99 "encoding/json"
1010 "errors"
1111 "fmt"
12- "log"
13- "os"
1412
15- gb "github.com/elastic/elastic-agent-changelog-tool/internal/github"
16- "github.com/google/go-github/v32/github"
13+ "github.com/elastic/elastic-agent-changelog-tool/internal/github"
1714 "github.com/spf13/afero"
1815 "github.com/spf13/cobra"
19- "golang.org/x/oauth2"
2016)
2117
2218var errListPRCmdMissingCommitHash = errors .New ("find-pr requires commit hash argument" )
2319
2420const defaultOwner = "elastic"
2521const defaultRepo = "beats"
2622
27- const repoFlagName = "repo"
28- const repoFlagDescription = "target repository"
2923const findPRLongDescription = `Use this command to find the original PR that included the commit in the repository.
3024
3125argument with commit hash is required
3226--repo flag is optional and will default to elastic/beats if left unspecified.`
3327
34- type PRInfo struct {
35- CommitHash string `json:"commit"`
36- PullRequestID string `json:"pull-request"`
37- }
38-
39- func prToDomain (commitHash string , pr * github.PullRequest ) PRInfo {
40- return PRInfo {
41- CommitHash : commitHash ,
42- PullRequestID : fmt .Sprintf ("%d" , * pr .Number ),
43- }
44- }
45-
46- func FindPRCommand () * cobra.Command {
28+ func FindPRCommand (appFs afero.Fs ) * cobra.Command {
4729 findPRCommand := & cobra.Command {
4830 Use : "find-pr" ,
4931 Long : findPRLongDescription ,
@@ -55,66 +37,34 @@ func FindPRCommand() *cobra.Command {
5537 return nil
5638 },
5739 RunE : func (cmd * cobra.Command , args []string ) error {
58- authToken := gb .NewAuthToken (& afero.OsFs {})
59-
60- var GithubClient * gb.GithubClient
61-
62- githubAccessToken , err := authToken .AuthToken ()
63- switch {
64- case errors .Is (err , os .ErrNotExist ):
65- // Github authorization token is not found
66- // we continue using an unauthorized github client
67- GithubClient = gb .NewUnauthorizedClient ()
68-
69- case err != nil :
70- // If github token is found but couldn't read it's content
71- log .Fatal (err )
72-
73- default :
74- // Github token is found and read successfully
75- GithubClient , err = gb .NewClient (gb .NewWrapper (github .NewClient (oauth2 .NewClient (context .Background (), oauth2 .StaticTokenSource (
76- & oauth2.Token {
77- AccessToken : githubAccessToken ,
78- }),
79- ))))
80- if err != nil {
81- log .Fatal (err )
82- }
40+ hc , err := github .GetHTTPClient (appFs )
41+ if err != nil {
42+ return fmt .Errorf ("cannot initialize http client: %w" , err )
8343 }
8444
85- var commit string
45+ c := github . NewClient ( hc )
8646
87- repo , err := cmd .Flags ().GetString (repoFlagName )
47+ repo , err := cmd .Flags ().GetString ("repo" )
8848 if err != nil {
8949 return fmt .Errorf ("repo flag malformed: %w" , err )
9050 }
9151
92- commit = args [0 ]
93-
94- if repo == "" {
95- repo = defaultRepo
96- }
97-
98- prs , _ , err := GithubClient .ListPullRequestsWithCommit (context .Background (), defaultOwner , repo , commit , nil )
52+ owner , err := cmd .Flags ().GetString ("owner" )
9953 if err != nil {
100- return fmt .Errorf ("failed listing prs with commit: %w" , err )
101- }
102-
103- type resp struct {
104- Items []PRInfo `json:"items"`
54+ return fmt .Errorf ("owner flag malformed: %w" , err )
10555 }
10656
107- respData := resp {
108- Items : make ([]PRInfo , len (prs )),
109- }
57+ commit := args [0 ]
58+ ctx := context .Background ()
11059
111- for i , pr := range prs {
112- respData .Items [i ] = prToDomain (commit , pr )
60+ res , err := github .FindPR (ctx , c , owner , repo , commit )
61+ if err != nil {
62+ return fmt .Errorf ("failed listing prs with commit: %w" , err )
11363 }
11464
115- respJSON , err := json .Marshal (respData )
65+ respJSON , err := json .Marshal (res )
11666 if err != nil {
117- return fmt .Errorf ("failed listing prs with commit : %w" , err )
67+ return fmt .Errorf ("failed marshalling JSON output : %w" , err )
11868 }
11969
12070 cmd .Println (string (respJSON ))
@@ -123,7 +73,8 @@ func FindPRCommand() *cobra.Command {
12373 },
12474 }
12575
126- findPRCommand .Flags ().String (repoFlagName , "" , repoFlagDescription )
76+ findPRCommand .Flags ().String ("repo" , defaultRepo , "target repository" )
77+ findPRCommand .Flags ().String ("owner" , defaultOwner , "target repository owner" )
12778
12879 return findPRCommand
12980}
0 commit comments