@@ -97,6 +97,75 @@ func (rs *ReviewsService) GetRepository(tx *gorm.DB, repoID uint) (*responses.Ge
9797 return response , nil
9898}
9999
100+ func (rs * ReviewsService ) RefreshPullRequests (ctx context.Context , tx * gorm.DB , githubClient utils.GithubClient , userID , repoID uint ) error {
101+ repository , err := rs .reviewsRepository .GetRepository (tx , repoID )
102+ if err != nil {
103+ return err
104+ }
105+
106+ existingPRs , err := rs .reviewsRepository .GetPullRequests (tx , userID , repoID )
107+ if err != nil {
108+ return err
109+ }
110+
111+ currentOpenPRs , err := githubClient .ListPullRequests (ctx , repository .Name , repository .Owner , userID )
112+ if err != nil {
113+ return err
114+ }
115+
116+ // find all new open PRs that were added since last refresh
117+ var newPRs []* models.PullRequest
118+ for _ , pr := range currentOpenPRs {
119+ found := false
120+ for _ , existingPR := range existingPRs {
121+ if pr .Number == existingPR .Number {
122+ found = true
123+ break
124+ }
125+ }
126+ if ! found {
127+ newPRs = append (newPRs , & models.PullRequest {
128+ RepositoryID : repoID ,
129+ Number : pr .Number ,
130+ Title : pr .Title ,
131+ URL : pr .URL ,
132+ State : pr .State ,
133+ LastCommit : pr .LastCommit ,
134+ })
135+ }
136+ }
137+ log .Printf ("Found %d new PRs\n " , len (newPRs ))
138+
139+ // find PRs that are no longer open
140+ var closedPRs []* models.PullRequest
141+ for _ , existingPR := range existingPRs {
142+ if existingPR .State == constants .PRStateClosed {
143+ continue
144+ }
145+ found := false
146+ for _ , pr := range currentOpenPRs {
147+ if pr .Number == existingPR .Number {
148+ found = true
149+ break
150+ }
151+ }
152+ if ! found {
153+ existingPR .State = constants .PRStateClosed
154+ closedPRs = append (closedPRs , existingPR )
155+ }
156+ }
157+ log .Printf ("Found %d now closed PRs\n " , len (closedPRs ))
158+
159+ if err := rs .reviewsRepository .CreatePullRequests (tx , newPRs ); err != nil {
160+ return err
161+ }
162+ if err := rs .reviewsRepository .UpdatePullRequestStatuses (tx , closedPRs ); err != nil {
163+ return err
164+ }
165+
166+ return nil
167+ }
168+
100169func (rs * ReviewsService ) findPullRequests (ctx context.Context , githubClient utils.GithubClient , repo * models.Repository , userID uint ) ([]* models.PullRequest , error ) {
101170 fetched_prs , err := githubClient .ListPullRequests (ctx , repo .Name , repo .Owner , userID )
102171 if err != nil {
0 commit comments