|
| 1 | +package org.jabref.gui.git; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import javafx.beans.property.BooleanProperty; |
| 8 | +import javafx.beans.property.SimpleBooleanProperty; |
| 9 | +import javafx.beans.property.SimpleStringProperty; |
| 10 | +import javafx.beans.property.StringProperty; |
| 11 | + |
| 12 | +import org.jabref.gui.AbstractViewModel; |
| 13 | +import org.jabref.gui.DialogService; |
| 14 | +import org.jabref.gui.StateManager; |
| 15 | +import org.jabref.logic.JabRefException; |
| 16 | +import org.jabref.logic.git.GitHandler; |
| 17 | +import org.jabref.logic.git.prefs.GitPreferences; |
| 18 | +import org.jabref.logic.git.status.GitStatusChecker; |
| 19 | +import org.jabref.logic.git.status.GitStatusSnapshot; |
| 20 | +import org.jabref.logic.git.status.SyncStatus; |
| 21 | +import org.jabref.logic.git.util.GitHandlerRegistry; |
| 22 | +import org.jabref.logic.git.util.GitInitService; |
| 23 | +import org.jabref.logic.l10n.Localization; |
| 24 | +import org.jabref.model.database.BibDatabaseContext; |
| 25 | + |
| 26 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +public class GitShareToGitHubDialogViewModel extends AbstractViewModel { |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(GitShareToGitHubDialogViewModel.class); |
| 32 | + |
| 33 | + private final StateManager stateManager; |
| 34 | + private final DialogService dialogService; |
| 35 | + private final GitPreferences gitPreferences = new GitPreferences(); |
| 36 | + |
| 37 | + private final StringProperty githubUsername = new SimpleStringProperty(); |
| 38 | + private final StringProperty githubPat = new SimpleStringProperty(); |
| 39 | + private final StringProperty repositoryUrl = new SimpleStringProperty(); |
| 40 | + private final BooleanProperty rememberSettings = new SimpleBooleanProperty(); |
| 41 | + |
| 42 | + public GitShareToGitHubDialogViewModel(StateManager stateManager, DialogService dialogService) { |
| 43 | + this.stateManager = stateManager; |
| 44 | + this.dialogService = dialogService; |
| 45 | + |
| 46 | + applyGitPreferences(); |
| 47 | + } |
| 48 | + |
| 49 | + public boolean shareToGitHub() { |
| 50 | + String url = trimOrEmpty(repositoryUrl.get()); |
| 51 | + String user = trimOrEmpty(githubUsername.get()); |
| 52 | + String pat = trimOrEmpty(githubPat.get()); |
| 53 | + |
| 54 | + if (url.isBlank()) { |
| 55 | + dialogService.showErrorDialogAndWait(Localization.lang("GitHub repository URL is required")); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if (pat.isBlank()) { |
| 60 | + dialogService.showErrorDialogAndWait(Localization.lang("Personal Access Token is required to push")); |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (user.isBlank()) { |
| 64 | + dialogService.showErrorDialogAndWait(Localization.lang("GitHub username is required")); |
| 65 | + return false; |
| 66 | + } |
| 67 | + Optional<BibDatabaseContext> activeDatabaseOpt = stateManager.getActiveDatabase(); |
| 68 | + if (activeDatabaseOpt.isEmpty()) { |
| 69 | + dialogService.showErrorDialogAndWait( |
| 70 | + Localization.lang("No library open") |
| 71 | + ); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + BibDatabaseContext activeDatabase = activeDatabaseOpt.get(); |
| 76 | + Optional<Path> bibFilePathOpt = activeDatabase.getDatabasePath(); |
| 77 | + if (bibFilePathOpt.isEmpty()) { |
| 78 | + dialogService.showErrorDialogAndWait( |
| 79 | + Localization.lang("No library file path"), |
| 80 | + Localization.lang("Cannot share: Please save the library to a file first.") |
| 81 | + ); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + Path bibPath = bibFilePathOpt.get(); |
| 86 | + |
| 87 | + try { |
| 88 | + GitInitService.initRepoAndSetRemote(bibPath, url); |
| 89 | + |
| 90 | + GitHandlerRegistry registry = new GitHandlerRegistry(); |
| 91 | + GitHandler handler = registry.get(bibPath.getParent()); |
| 92 | + |
| 93 | + boolean hasStoredPat = gitPreferences.getPersonalAccessToken().isPresent(); |
| 94 | + if (!rememberSettingsProperty().get() || !hasStoredPat) { |
| 95 | + handler.setCredentials(user, pat); |
| 96 | + } |
| 97 | + GitStatusSnapshot status = GitStatusChecker.checkStatusAndFetch(handler); |
| 98 | + |
| 99 | + if (status.syncStatus() == SyncStatus.BEHIND) { |
| 100 | + dialogService.showWarningDialogAndWait( |
| 101 | + Localization.lang("Remote repository is not empty"), |
| 102 | + Localization.lang("Please pull changes before pushing.") |
| 103 | + ); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + handler.createCommitOnCurrentBranch(Localization.lang("Share library to GitHub"), false); |
| 108 | + try { |
| 109 | + if (status.syncStatus() == SyncStatus.REMOTE_EMPTY) { |
| 110 | + handler.pushCurrentBranchCreatingUpstream(); |
| 111 | + } else { |
| 112 | + handler.pushCommitsToRemoteRepository(); |
| 113 | + } |
| 114 | + } catch (IOException | GitAPIException e) { |
| 115 | + LOGGER.error("Push failed", e); |
| 116 | + dialogService.showErrorDialogAndWait(Localization.lang("Git error"), e); |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + setGitPreferences(url, user, pat); |
| 121 | + |
| 122 | + dialogService.showInformationDialogAndWait( |
| 123 | + Localization.lang("GitHub Share"), |
| 124 | + Localization.lang("Successfully pushed to %0", url) |
| 125 | + ); |
| 126 | + return true; |
| 127 | + } catch (GitAPIException | |
| 128 | + IOException e) { |
| 129 | + LOGGER.error("Error sharing to GitHub", e); |
| 130 | + dialogService.showErrorDialogAndWait(Localization.lang("Git error"), e); |
| 131 | + return false; |
| 132 | + } catch (JabRefException e) { |
| 133 | + dialogService.showErrorDialogAndWait(Localization.lang("Git error"), e); |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void applyGitPreferences() { |
| 139 | + gitPreferences.getUsername().ifPresent(githubUsername::set); |
| 140 | + gitPreferences.getPersonalAccessToken().ifPresent(token -> { |
| 141 | + githubPat.set(token); |
| 142 | + rememberSettings.set(true); |
| 143 | + }); |
| 144 | + gitPreferences.getRepositoryUrl().ifPresent(repositoryUrl::set); |
| 145 | + rememberSettings.set(gitPreferences.getRememberPat() || rememberSettings.get()); |
| 146 | + } |
| 147 | + |
| 148 | + private void setGitPreferences(String url, String user, String pat) { |
| 149 | + gitPreferences.setUsername(user); |
| 150 | + gitPreferences.setRepositoryUrl(url); |
| 151 | + gitPreferences.setRememberPat(rememberSettings.get()); |
| 152 | + |
| 153 | + if (rememberSettings.get()) { |
| 154 | + gitPreferences.savePersonalAccessToken(pat, user); |
| 155 | + } else { |
| 156 | + gitPreferences.clearGitHubPersonalAccessToken(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static String trimOrEmpty(String s) { |
| 161 | + return (s == null) ? "" : s.trim(); |
| 162 | + } |
| 163 | + |
| 164 | + public StringProperty githubUsernameProperty() { |
| 165 | + return githubUsername; |
| 166 | + } |
| 167 | + |
| 168 | + public StringProperty githubPatProperty() { |
| 169 | + return githubPat; |
| 170 | + } |
| 171 | + |
| 172 | + public BooleanProperty rememberSettingsProperty() { |
| 173 | + return rememberSettings; |
| 174 | + } |
| 175 | + |
| 176 | + public StringProperty repositoryUrlProperty() { |
| 177 | + return repositoryUrl; |
| 178 | + } |
| 179 | +} |
0 commit comments