1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.exoplatform.acceptance.service.vcs;
22
23 import org.exoplatform.acceptance.model.vcs.VCSCoordinates;
24 import org.exoplatform.acceptance.model.vcs.VCSRef;
25 import org.exoplatform.acceptance.model.vcs.VCSRepository;
26 import org.exoplatform.acceptance.service.AbstractMongoCRUDService;
27 import org.exoplatform.acceptance.service.CRUDService;
28 import org.exoplatform.acceptance.service.ConfigurationService;
29 import org.exoplatform.acceptance.storage.vcs.VCSRepositoryMongoStorage;
30
31 import com.google.common.base.Function;
32 import com.google.common.collect.FluentIterable;
33 import java.io.File;
34 import java.util.List;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import org.eclipse.jgit.api.Git;
38 import org.eclipse.jgit.api.errors.GitAPIException;
39 import org.eclipse.jgit.lib.Constants;
40 import org.eclipse.jgit.lib.Ref;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.scheduling.annotation.Scheduled;
44
45
46
47
48
49
50
51 @Named
52 public class VCSRepositoryService extends AbstractMongoCRUDService<VCSRepository> implements CRUDService<VCSRepository> {
53
54
55
56
57 private static final Logger LOGGER = LoggerFactory.getLogger(VCSRepositoryService.class);
58
59
60
61 private static final Function<Ref, VCSRef> REF_TO_VCSREF_FUNCTION = new Function<Ref, VCSRef>() {
62 @Override
63
64
65
66 public VCSRef apply(Ref input) {
67 if (null == input) {
68 return null;
69 }
70 if (input.getName().startsWith(Constants.R_HEADS)) {
71 return new VCSRef(VCSRef.Type.BRANCH, input.getName().substring(Constants.R_HEADS.length()),
72 input.getObjectId().getName());
73 }
74 if (input.getName().startsWith(Constants.R_TAGS)) {
75 return new VCSRef(VCSRef.Type.TAG, input.getName().substring(Constants.R_TAGS.length()), input.getObjectId().getName());
76 }
77
78 return null;
79 }
80 };
81
82 @Inject
83 private ConfigurationService configurationService;
84 @Inject
85 private VCSRepositoryMongoStorage vcsRepositoryMongoStorage;
86
87
88
89
90 @Override
91 protected VCSRepositoryMongoStorage getMongoStorage() {
92 return vcsRepositoryMongoStorage;
93 }
94
95
96
97
98
99
100
101 private File getCheckoutDir(VCSRepository repository) {
102 return getCheckoutDir(repository.getId());
103 }
104
105
106
107
108
109
110
111 private File getCheckoutDir(String repositoryId) {
112 return new File(configurationService.getVCSCheckoutDirectory(), repositoryId);
113 }
114
115
116
117
118
119
120
121 public VCSRepository findByName(String name) {
122 return vcsRepositoryMongoStorage.findByName(name);
123 }
124
125
126
127
128
129
130 @Scheduled(fixedRate = 120000)
131 public void updateRepositories() {
132 for (VCSRepository repository : vcsRepositoryMongoStorage.findAll()) {
133 loadData(repository);
134 }
135 LOGGER.info("VCS data updated.");
136 }
137
138
139
140
141
142
143 private void loadData(VCSRepository repository) {
144 switch (repository.getType()) {
145 case GIT:
146 for (VCSCoordinates remote : repository.getRemoteRepositories()) {
147 try {
148 remote.setReferences(loadReferencesFromRemote(remote));
149 } catch (GitAPIException e) {
150 LOGGER.warn("Error while listing branches and tags of repository {} from its remote {} : {}",
151 repository.getName(), remote, e.getMessage());
152 }
153 }
154 break;
155 default:
156 LOGGER.error("Unknown VCS repository type {}", repository.getType());
157 return;
158 }
159 vcsRepositoryMongoStorage.save(repository);
160 }
161
162
163
164
165
166
167
168
169 private List<VCSRef> loadReferencesFromRemote(VCSCoordinates remote) throws GitAPIException {
170 return FluentIterable.from(Git.lsRemoteRepository()
171 .setHeads(true)
172 .setTags(true)
173 .setRemote(remote.getUrl())
174 .call()).transform(REF_TO_VCSREF_FUNCTION).toList();
175 }
176 }