| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | package cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/drone/drone/model" | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 	"github.com/drone/drone/remote" | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	"golang.org/x/net/context" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | // GetPerm returns the user permissions repositories from the cache | 
					
						
							|  |  |  | // associated with the current repository. | 
					
						
							|  |  |  | func GetPerms(c context.Context, user *model.User, owner, name string) (*model.Perm, error) { | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	key := fmt.Sprintf("perms:%s:%s/%s", | 
					
						
							|  |  |  | 		user.Login, | 
					
						
							|  |  |  | 		owner, | 
					
						
							|  |  |  | 		name, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 	// if we fetch from the cache we can return immediately | 
					
						
							|  |  |  | 	val, err := Get(c, key) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return val.(*model.Perm), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// else we try to grab from the remote system and | 
					
						
							|  |  |  | 	// populate our cache. | 
					
						
							|  |  |  | 	perm, err := remote.Perm(c, user, owner, name) | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 	Set(c, key, perm) | 
					
						
							|  |  |  | 	return perm, nil | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetRepos returns the list of user repositories from the cache | 
					
						
							|  |  |  | // associated with the current context. | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | func GetRepos(c context.Context, user *model.User) ([]*model.RepoLite, error) { | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	key := fmt.Sprintf("repos:%s", | 
					
						
							|  |  |  | 		user.Login, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 	// if we fetch from the cache we can return immediately | 
					
						
							|  |  |  | 	val, err := Get(c, key) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return val.([]*model.RepoLite), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// else we try to grab from the remote system and | 
					
						
							|  |  |  | 	// populate our cache. | 
					
						
							|  |  |  | 	repos, err := remote.Repos(c, user) | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 21:15:50 -08:00
										 |  |  | 	Set(c, key, repos) | 
					
						
							|  |  |  | 	return repos, nil | 
					
						
							| 
									
										
										
										
											2015-10-20 16:45:24 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-14 13:07:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 15:40:29 -07:00
										 |  |  | // GetRepoMap returns the list of user repositories from the cache | 
					
						
							|  |  |  | // associated with the current context in a map structure. | 
					
						
							|  |  |  | func GetRepoMap(c context.Context, user *model.User) (map[string]bool, error) { | 
					
						
							|  |  |  | 	repos, err := GetRepos(c, user) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	repom := map[string]bool{} | 
					
						
							|  |  |  | 	for _, repo := range repos { | 
					
						
							|  |  |  | 		repom[repo.FullName] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return repom, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 13:07:05 -07:00
										 |  |  | // DeleteRepos evicts the cached user repositories from the cache associated | 
					
						
							|  |  |  | // with the current context. | 
					
						
							|  |  |  | func DeleteRepos(c context.Context, user *model.User) error { | 
					
						
							|  |  |  | 	key := fmt.Sprintf("repos:%s", | 
					
						
							|  |  |  | 		user.Login, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	return Delete(c, key) | 
					
						
							|  |  |  | } |