use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project zaproxy by zaproxy.
the class CreatePullRequest method pullRequest.
@TaskAction
public void pullRequest() throws Exception {
GitHubRepo ghRepo = getRepo().get();
Repository repository = new FileRepositoryBuilder().setGitDir(new File(ghRepo.getDir(), ".git")).build();
try (Git git = new Git(repository)) {
if (git.status().call().getModified().isEmpty()) {
return;
}
GitHubUser ghUser = getUser().get();
URIish originUri = new URIish(GITHUB_BASE_URL + ghUser.getName() + "/" + ghRepo.getName());
git.remoteSetUrl().setRemoteName(GIT_REMOTE_ORIGIN).setRemoteUri(originUri).call();
git.checkout().setCreateBranch(true).setName(getBranchName().get()).setStartPoint(GIT_REMOTE_ORIGIN + "/" + baseBranchName.get()).call();
PersonIdent personIdent = new PersonIdent(ghUser.getName(), ghUser.getEmail());
git.commit().setAll(true).setSign(false).setAuthor(personIdent).setCommitter(personIdent).setMessage(getCommitSummary().get() + "\n\n" + getCommitDescription().get() + signedOffBy(personIdent)).call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(ghUser.getName(), ghUser.getAuthToken())).setForce(true).add(getBranchName().get()).call();
GHRepository ghRepository = GitHub.connect(ghUser.getName(), ghUser.getAuthToken()).getRepository(ghRepo.toString());
List<GHPullRequest> pulls = ghRepository.queryPullRequests().base(baseBranchName.get()).head(ghUser.getName() + ":" + getBranchName().get()).state(GHIssueState.OPEN).list().asList();
String description = getPullRequestDescription().getOrElse(getCommitDescription().get());
if (pulls.isEmpty()) {
String title = getPullRequestTitle().getOrElse(getCommitSummary().get());
createPullRequest(ghRepository, title, description);
} else {
pulls.get(0).setBody(description);
}
}
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gitblit by gitblit.
the class FederationPullService method pull.
/**
* Mirrors a repository and, optionally, the server's users, and/or
* configuration settings from a origin Gitblit instance.
*
* @param registration
* @throws Exception
*/
private void pull(FederationModel registration) throws Exception {
Map<String, RepositoryModel> repositories = FederationUtils.getRepositories(registration, true);
String registrationFolder = registration.folder.toLowerCase().trim();
// confirm valid characters in server alias
Character c = StringUtils.findInvalidCharacter(registrationFolder);
if (c != null) {
logger.error(MessageFormat.format("Illegal character ''{0}'' in folder name ''{1}'' of federation registration {2}!", c, registrationFolder, registration.name));
return;
}
File repositoriesFolder = gitblit.getRepositoriesFolder();
File registrationFolderFile = new File(repositoriesFolder, registrationFolder);
registrationFolderFile.mkdirs();
// Clone/Pull the repository
for (Map.Entry<String, RepositoryModel> entry : repositories.entrySet()) {
String cloneUrl = entry.getKey();
RepositoryModel repository = entry.getValue();
if (!repository.hasCommits) {
logger.warn(MessageFormat.format("Skipping federated repository {0} from {1} @ {2}. Repository is EMPTY.", repository.name, registration.name, registration.url));
registration.updateStatus(repository, FederationPullStatus.SKIPPED);
continue;
}
// Determine local repository name
String repositoryName;
if (StringUtils.isEmpty(registrationFolder)) {
repositoryName = repository.name;
} else {
repositoryName = registrationFolder + "/" + repository.name;
}
if (registration.bare) {
// bare repository, ensure .git suffix
if (!repositoryName.toLowerCase().endsWith(DOT_GIT_EXT)) {
repositoryName += DOT_GIT_EXT;
}
} else {
// normal repository, strip .git suffix
if (repositoryName.toLowerCase().endsWith(DOT_GIT_EXT)) {
repositoryName = repositoryName.substring(0, repositoryName.indexOf(DOT_GIT_EXT));
}
}
// confirm that the origin of any pre-existing repository matches
// the clone url
String fetchHead = null;
Repository existingRepository = gitblit.getRepository(repositoryName);
if (existingRepository == null && gitblit.isCollectingGarbage(repositoryName)) {
logger.warn(MessageFormat.format("Skipping local repository {0}, busy collecting garbage", repositoryName));
continue;
}
if (existingRepository != null) {
StoredConfig config = existingRepository.getConfig();
config.load();
String origin = config.getString("remote", "origin", "url");
RevCommit commit = JGitUtils.getCommit(existingRepository, org.eclipse.jgit.lib.Constants.FETCH_HEAD);
if (commit != null) {
fetchHead = commit.getName();
}
existingRepository.close();
if (!origin.startsWith(registration.url)) {
logger.warn(MessageFormat.format("Skipping federated repository {0} from {1} @ {2}. Origin does not match, consider EXCLUDING.", repository.name, registration.name, registration.url));
registration.updateStatus(repository, FederationPullStatus.SKIPPED);
continue;
}
}
// clone/pull this repository
CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(Constants.FEDERATION_USER, registration.token);
logger.info(MessageFormat.format("Pulling federated repository {0} from {1} @ {2}", repository.name, registration.name, registration.url));
CloneResult result = JGitUtils.cloneRepository(registrationFolderFile, repository.name, cloneUrl, registration.bare, credentials);
Repository r = gitblit.getRepository(repositoryName);
RepositoryModel rm = gitblit.getRepositoryModel(repositoryName);
repository.isFrozen = registration.mirror;
if (result.createdRepository) {
// default local settings
repository.federationStrategy = FederationStrategy.EXCLUDE;
repository.isFrozen = registration.mirror;
repository.showRemoteBranches = !registration.mirror;
logger.info(MessageFormat.format(" cloning {0}", repository.name));
registration.updateStatus(repository, FederationPullStatus.MIRRORED);
} else {
// fetch and update
boolean fetched = false;
RevCommit commit = JGitUtils.getCommit(r, org.eclipse.jgit.lib.Constants.FETCH_HEAD);
String newFetchHead = commit.getName();
fetched = fetchHead == null || !fetchHead.equals(newFetchHead);
if (registration.mirror) {
// mirror
if (fetched) {
// update local branches to match the remote tracking branches
for (RefModel ref : JGitUtils.getRemoteBranches(r, false, -1)) {
if (ref.displayName.startsWith("origin/")) {
String branch = org.eclipse.jgit.lib.Constants.R_HEADS + ref.displayName.substring(ref.displayName.indexOf('/') + 1);
String hash = ref.getReferencedObjectId().getName();
JGitUtils.setBranchRef(r, branch, hash);
logger.info(MessageFormat.format(" resetting {0} of {1} to {2}", branch, repository.name, hash));
}
}
String newHead;
if (StringUtils.isEmpty(repository.HEAD)) {
newHead = newFetchHead;
} else {
newHead = repository.HEAD;
}
JGitUtils.setHEADtoRef(r, newHead);
logger.info(MessageFormat.format(" resetting HEAD of {0} to {1}", repository.name, newHead));
registration.updateStatus(repository, FederationPullStatus.MIRRORED);
} else {
// indicate no commits pulled
registration.updateStatus(repository, FederationPullStatus.NOCHANGE);
}
} else {
// non-mirror
if (fetched) {
// indicate commits pulled to origin/master
registration.updateStatus(repository, FederationPullStatus.PULLED);
} else {
// indicate no commits pulled
registration.updateStatus(repository, FederationPullStatus.NOCHANGE);
}
}
// preserve local settings
repository.isFrozen = rm.isFrozen;
repository.federationStrategy = rm.federationStrategy;
// merge federation sets
Set<String> federationSets = new HashSet<String>();
if (rm.federationSets != null) {
federationSets.addAll(rm.federationSets);
}
if (repository.federationSets != null) {
federationSets.addAll(repository.federationSets);
}
repository.federationSets = new ArrayList<String>(federationSets);
// merge indexed branches
Set<String> indexedBranches = new HashSet<String>();
if (rm.indexedBranches != null) {
indexedBranches.addAll(rm.indexedBranches);
}
if (repository.indexedBranches != null) {
indexedBranches.addAll(repository.indexedBranches);
}
repository.indexedBranches = new ArrayList<String>(indexedBranches);
}
// only repositories that are actually _cloned_ from the origin
// Gitblit repository are marked as federated. If the origin
// is from somewhere else, these repositories are not considered
// "federated" repositories.
repository.isFederated = cloneUrl.startsWith(registration.url);
gitblit.updateConfiguration(r, repository);
r.close();
}
IUserService userService = null;
try {
// Pull USERS
// TeamModels are automatically pulled because they are contained
// within the UserModel. The UserService creates unknown teams
// and updates existing teams.
Collection<UserModel> users = FederationUtils.getUsers(registration);
if (users != null && users.size() > 0) {
File realmFile = new File(registrationFolderFile, registration.name + "_users.conf");
realmFile.delete();
userService = new ConfigUserService(realmFile);
for (UserModel user : users) {
userService.updateUserModel(user.username, user);
// the user accounts of this Gitblit instance
if (registration.mergeAccounts) {
// repositories are stored within subfolders
if (!StringUtils.isEmpty(registrationFolder)) {
if (user.permissions != null) {
// pulling from >= 1.2 version
Map<String, AccessPermission> copy = new HashMap<String, AccessPermission>(user.permissions);
user.permissions.clear();
for (Map.Entry<String, AccessPermission> entry : copy.entrySet()) {
user.setRepositoryPermission(registrationFolder + "/" + entry.getKey(), entry.getValue());
}
} else {
// pulling from <= 1.1 version
List<String> permissions = new ArrayList<String>(user.repositories);
user.repositories.clear();
for (String permission : permissions) {
user.addRepositoryPermission(registrationFolder + "/" + permission);
}
}
}
// insert new user or update local user
UserModel localUser = gitblit.getUserModel(user.username);
if (localUser == null) {
// create new local user
gitblit.addUser(user);
} else {
// update repository permissions of local user
if (user.permissions != null) {
// pulling from >= 1.2 version
Map<String, AccessPermission> copy = new HashMap<String, AccessPermission>(user.permissions);
for (Map.Entry<String, AccessPermission> entry : copy.entrySet()) {
localUser.setRepositoryPermission(entry.getKey(), entry.getValue());
}
} else {
// pulling from <= 1.1 version
for (String repository : user.repositories) {
localUser.addRepositoryPermission(repository);
}
}
localUser.password = user.password;
localUser.canAdmin = user.canAdmin;
gitblit.reviseUser(localUser.username, localUser);
}
for (String teamname : gitblit.getAllTeamNames()) {
TeamModel team = gitblit.getTeamModel(teamname);
if (user.isTeamMember(teamname) && !team.hasUser(user.username)) {
// new team member
team.addUser(user.username);
gitblit.updateTeamModel(teamname, team);
} else if (!user.isTeamMember(teamname) && team.hasUser(user.username)) {
// remove team member
team.removeUser(user.username);
gitblit.updateTeamModel(teamname, team);
}
// update team repositories
TeamModel remoteTeam = user.getTeam(teamname);
if (remoteTeam != null) {
if (remoteTeam.permissions != null) {
// pulling from >= 1.2
for (Map.Entry<String, AccessPermission> entry : remoteTeam.permissions.entrySet()) {
team.setRepositoryPermission(entry.getKey(), entry.getValue());
}
gitblit.updateTeamModel(teamname, team);
} else if (!ArrayUtils.isEmpty(remoteTeam.repositories)) {
// pulling from <= 1.1
team.addRepositoryPermissions(remoteTeam.repositories);
gitblit.updateTeamModel(teamname, team);
}
}
}
}
}
}
} catch (ForbiddenException e) {
// ignore forbidden exceptions
} catch (IOException e) {
logger.warn(MessageFormat.format("Failed to retrieve USERS from federated gitblit ({0} @ {1})", registration.name, registration.url), e);
}
try {
// mailing lists or push scripts without specifying users.
if (userService != null) {
Collection<TeamModel> teams = FederationUtils.getTeams(registration);
if (teams != null && teams.size() > 0) {
for (TeamModel team : teams) {
userService.updateTeamModel(team);
}
}
}
} catch (ForbiddenException e) {
// ignore forbidden exceptions
} catch (IOException e) {
logger.warn(MessageFormat.format("Failed to retrieve TEAMS from federated gitblit ({0} @ {1})", registration.name, registration.url), e);
}
try {
// Pull SETTINGS
Map<String, String> settings = FederationUtils.getSettings(registration);
if (settings != null && settings.size() > 0) {
Properties properties = new Properties();
properties.putAll(settings);
FileOutputStream os = new FileOutputStream(new File(registrationFolderFile, registration.name + "_" + Constants.PROPERTIES_FILE));
properties.store(os, null);
os.close();
}
} catch (ForbiddenException e) {
// ignore forbidden exceptions
} catch (IOException e) {
logger.warn(MessageFormat.format("Failed to retrieve SETTINGS from federated gitblit ({0} @ {1})", registration.name, registration.url), e);
}
try {
// Pull SCRIPTS
Map<String, String> scripts = FederationUtils.getScripts(registration);
if (scripts != null && scripts.size() > 0) {
for (Map.Entry<String, String> script : scripts.entrySet()) {
String scriptName = script.getKey();
if (scriptName.endsWith(".groovy")) {
scriptName = scriptName.substring(0, scriptName.indexOf(".groovy"));
}
File file = new File(registrationFolderFile, registration.name + "_" + scriptName + ".groovy");
FileUtils.writeContent(file, script.getValue());
}
}
} catch (ForbiddenException e) {
// ignore forbidden exceptions
} catch (IOException e) {
logger.warn(MessageFormat.format("Failed to retrieve SCRIPTS from federated gitblit ({0} @ {1})", registration.name, registration.url), e);
}
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gitblit by gitblit.
the class TicketReferenceTest method configure.
@BeforeClass
public static void configure() throws Exception {
File repositoryName = new File(repoName);
GitBlitSuite.close(repositoryName);
if (repositoryName.exists()) {
FileUtils.delete(repositoryName, FileUtils.RECURSIVE | FileUtils.RETRY);
}
repo = new RepositoryModel(repoName, null, null, null);
if (gitblit().hasRepository(repo.name)) {
gitblit().deleteRepositoryModel(repo);
}
gitblit().updateRepositoryModel(repo.name, repo, true);
user = new UserModel(account);
user.displayName = account;
user.emailAddress = account + "@example.com";
user.password = password;
cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
if (gitblit().getUserModel(user.username) != null) {
gitblit().deleteUser(user.username);
}
repo.authorizationControl = AuthorizationControl.NAMED;
repo.accessRestriction = AccessRestrictionType.PUSH;
gitblit().updateRepositoryModel(repo.name, repo, false);
// grant user push permission
user.setRepositoryPermission(repo.name, AccessPermission.REWIND);
gitblit().updateUserModel(user);
ticketService = gitblit().getTicketService();
assertTrue(ticketService.deleteAll(repo));
GitBlitSuite.close(workingCopy);
if (workingCopy.exists()) {
FileUtils.delete(workingCopy, FileUtils.RECURSIVE | FileUtils.RETRY);
}
CloneCommand clone = Git.cloneRepository();
clone.setURI(MessageFormat.format("{0}/{1}", url, repo.name));
clone.setDirectory(workingCopy);
clone.setBare(false);
clone.setBranch("master");
clone.setCredentialsProvider(cp);
GitBlitSuite.close(clone.call());
git = Git.open(workingCopy);
git.getRepository().getConfig().setString("user", null, "name", user.displayName);
git.getRepository().getConfig().setString("user", null, "email", user.emailAddress);
git.getRepository().getConfig().save();
final RevCommit revCommit1 = makeCommit("initial commit");
final String initialSha = revCommit1.name();
Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(cp).call();
GitBlitSuite.close(git);
for (PushResult result : results) {
for (RemoteRefUpdate update : result.getRemoteUpdates()) {
assertEquals(Status.OK, update.getStatus());
assertEquals(initialSha, update.getNewObjectId().name());
}
}
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gitblit by gitblit.
the class MirrorService method run.
@Override
public void run() {
if (!isReady()) {
return;
}
running.set(true);
for (String repositoryName : repositoryManager.getRepositoryList()) {
if (forceClose.get()) {
break;
}
if (repositoryManager.isCollectingGarbage(repositoryName)) {
logger.debug("mirror is skipping {} garbagecollection", repositoryName);
continue;
}
RepositoryModel model = null;
Repository repository = null;
try {
model = repositoryManager.getRepositoryModel(repositoryName);
if (!model.isMirror && !model.isBare) {
// repository must be a valid bare git mirror
logger.debug("mirror is skipping {} !mirror !bare", repositoryName);
continue;
}
repository = repositoryManager.getRepository(repositoryName);
if (repository == null) {
logger.warn(MessageFormat.format("MirrorExecutor is missing repository {0}?!?", repositoryName));
continue;
}
// automatically repair (some) invalid fetch ref specs
if (!repairAttempted.contains(repositoryName)) {
repairAttempted.add(repositoryName);
JGitUtils.repairFetchSpecs(repository);
}
// find the first mirror remote - there should only be one
StoredConfig rc = repository.getConfig();
RemoteConfig mirror = null;
List<RemoteConfig> configs = RemoteConfig.getAllRemoteConfigs(rc);
for (RemoteConfig config : configs) {
if (config.isMirror()) {
mirror = config;
break;
}
}
if (mirror == null) {
// repository does not have a mirror remote
logger.debug("mirror is skipping {} no mirror remote found", repositoryName);
continue;
}
logger.debug("checking {} remote {} for ref updates", repositoryName, mirror.getName());
final boolean testing = false;
Git git = new Git(repository);
CredentialsProvider creds = null;
URIish fetchUri = mirror.getURIs().get(0);
if (fetchUri.getUser() != null && fetchUri.getPass() != null) {
creds = new UsernamePasswordCredentialsProvider(fetchUri.getUser(), fetchUri.getPass());
}
FetchResult result = git.fetch().setCredentialsProvider(creds).setRemote(mirror.getName()).setDryRun(testing).call();
Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();
if (refUpdates.size() > 0) {
ReceiveCommand ticketBranchCmd = null;
for (TrackingRefUpdate ru : refUpdates) {
StringBuilder sb = new StringBuilder();
sb.append("updated mirror ");
sb.append(repositoryName);
sb.append(" ");
sb.append(ru.getRemoteName());
sb.append(" -> ");
sb.append(ru.getLocalName());
if (ru.getResult() == Result.FORCED) {
sb.append(" (forced)");
}
sb.append(" ");
sb.append(ru.getOldObjectId() == null ? "" : ru.getOldObjectId().abbreviate(7).name());
sb.append("..");
sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());
logger.info(sb.toString());
if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {
ReceiveCommand.Type type = null;
switch(ru.getResult()) {
case NEW:
type = Type.CREATE;
break;
case FAST_FORWARD:
type = Type.UPDATE;
break;
case FORCED:
type = Type.UPDATE_NONFASTFORWARD;
break;
default:
type = null;
break;
}
if (type != null) {
ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getLocalName(), type);
}
}
}
if (ticketBranchCmd != null) {
repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));
}
}
} catch (Exception e) {
logger.error("Error updating mirror " + repositoryName, e);
} finally {
// cleanup
if (repository != null) {
repository.close();
}
}
}
running.set(false);
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gitblit by gitblit.
the class GitServletTest method testSubfolderPush.
@Test
public void testSubfolderPush() throws Exception {
GitBlitSuite.close(jgitFolder);
if (jgitFolder.exists()) {
FileUtils.delete(jgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
}
CloneCommand clone = Git.cloneRepository();
clone.setURI(MessageFormat.format("{0}/test/jgit.git", url));
clone.setDirectory(jgitFolder);
clone.setBare(false);
clone.setCloneAllBranches(true);
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
GitBlitSuite.close(clone.call());
assertTrue(true);
Git git = Git.open(jgitFolder);
File file = new File(jgitFolder, "TODO");
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
BufferedWriter w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
git.add().addFilepattern(file.getName()).call();
git.commit().setMessage("test commit").call();
Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
GitBlitSuite.close(git);
for (PushResult result : results) {
for (RemoteRefUpdate update : result.getRemoteUpdates()) {
assertEquals(Status.OK, update.getStatus());
}
}
}
Aggregations