@@ -2597,28 +2597,76 @@ func GetReturnURL(ctx context.Context, installationID, repositoryID int64, pullR
25972597 "pullRequestID" : pullRequestID ,
25982598 }
25992599
2600- client , err := NewGithubAppClient (installationID )
2600+ if installationID <= 0 {
2601+ err := errors .New ("invalid installation ID" )
2602+ log .WithFields (f ).WithError (err ).Warn ("invalid installation ID" )
2603+ return "" , err
2604+ }
2605+ if repositoryID <= 0 {
2606+ err := errors .New ("invalid repository ID" )
2607+ log .WithFields (f ).WithError (err ).Warn ("invalid repository ID" )
2608+ return "" , err
2609+ }
2610+ if pullRequestID <= 0 {
2611+ err := errors .New ("invalid pull request ID" )
2612+ log .WithFields (f ).WithError (err ).Warn ("invalid pull request ID" )
2613+ return "" , err
2614+ }
26012615
2616+ client , err := NewGithubAppClient (installationID )
26022617 if err != nil {
26032618 log .WithFields (f ).WithError (err ).Warn ("unable to create Github client" )
26042619 return "" , err
26052620 }
26062621
26072622 log .WithFields (f ).Debugf ("getting github repository by id: %d" , repositoryID )
2608- repo , _ , err := client .Repositories .GetByID (ctx , repositoryID )
2623+ repo , resp , err := client .Repositories .GetByID (ctx , repositoryID )
26092624 if err != nil {
2625+ if ok , wrapped := CheckAndWrapForKnownErrors (resp , err ); ok {
2626+ log .WithFields (f ).WithError (wrapped ).Warnf ("unable to get repository by ID: %d" , repositoryID )
2627+ return "" , wrapped
2628+ }
26102629 log .WithFields (f ).WithError (err ).Warnf ("unable to get repository by ID: %d" , repositoryID )
26112630 return "" , err
26122631 }
2632+ if repo == nil {
2633+ err := fmt .Errorf ("missing repository for repository ID %d" , repositoryID )
2634+ log .WithFields (f ).WithError (err ).Warn ("invalid repository metadata" )
2635+ return "" , err
2636+ }
2637+
2638+ owner := repo .GetOwner ().GetLogin ()
2639+ name := repo .GetName ()
2640+ if owner == "" || name == "" {
2641+ err := fmt .Errorf ("invalid repository owner/name for repository ID %d" , repositoryID )
2642+ log .WithFields (f ).WithError (err ).Warn ("invalid repository metadata" )
2643+ return "" , err
2644+ }
26132645
26142646 log .WithFields (f ).Debugf ("getting pull request by id: %d" , pullRequestID )
2615- pullRequest , _ , err := client .PullRequests .Get (ctx , * repo . Owner . Login , * repo . Name , pullRequestID )
2647+ pullRequest , resp , err := client .PullRequests .Get (ctx , owner , name , pullRequestID )
26162648 if err != nil {
2649+ if ok , wrapped := CheckAndWrapForKnownErrors (resp , err ); ok {
2650+ log .WithFields (f ).WithError (wrapped ).Warnf ("unable to get pull request by ID: %d" , pullRequestID )
2651+ return "" , wrapped
2652+ }
26172653 log .WithFields (f ).WithError (err ).Warnf ("unable to get pull request by ID: %d" , pullRequestID )
26182654 return "" , err
26192655 }
2656+ if pullRequest == nil {
2657+ err := fmt .Errorf ("missing pull request %d/%s/%s" , pullRequestID , owner , name )
2658+ log .WithFields (f ).WithError (err ).Warn ("invalid pull request metadata" )
2659+ return "" , err
2660+ }
2661+
2662+ htmlURL := pullRequest .GetHTMLURL ()
2663+ if htmlURL == "" {
2664+ err := fmt .Errorf ("missing html url for pull request %d/%s/%s" , pullRequestID , owner , name )
2665+ log .WithFields (f ).WithError (err ).Warn ("invalid pull request metadata" )
2666+ return "" , err
2667+ }
26202668
2621- log .WithFields (f ).Debugf ("returning pull request html url: %s" , * pullRequest . HTMLURL )
2669+ log .WithFields (f ).Debugf ("returning pull request html url: %s" , htmlURL )
26222670
2623- return * pullRequest . HTMLURL , nil
2671+ return htmlURL , nil
26242672}
0 commit comments