Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

258 righe
8.1 KiB

  1. import type { MessageItem } from 'vscode';
  2. import { ConfigurationTarget, env, Uri, window } from 'vscode';
  3. import { SuppressedMessages } from './config';
  4. import { Commands } from './constants';
  5. import type { GitCommit } from './git/models/commit';
  6. import { executeCommand } from './system/command';
  7. import { configuration } from './system/configuration';
  8. import { Logger } from './system/logger';
  9. import { LogLevel } from './system/logger.constants';
  10. export function showCommitHasNoPreviousCommitWarningMessage(commit?: GitCommit): Promise<MessageItem | undefined> {
  11. if (commit == null) {
  12. return showMessage('info', 'There is no previous commit.', SuppressedMessages.CommitHasNoPreviousCommitWarning);
  13. }
  14. return showMessage(
  15. 'info',
  16. `Commit ${commit.shortSha} (${commit.author.name}, ${commit.formattedDate}) has no previous commit.`,
  17. SuppressedMessages.CommitHasNoPreviousCommitWarning,
  18. );
  19. }
  20. export function showCommitNotFoundWarningMessage(message: string): Promise<MessageItem | undefined> {
  21. return showMessage('warn', `${message}. The commit could not be found.`, SuppressedMessages.CommitNotFoundWarning);
  22. }
  23. export async function showCreatePullRequestPrompt(branch: string): Promise<boolean> {
  24. const create = { title: 'Create Pull Request...' };
  25. const result = await showMessage(
  26. 'info',
  27. `Would you like to create a Pull Request for branch '${branch}'?`,
  28. SuppressedMessages.CreatePullRequestPrompt,
  29. { title: "Don't Show Again" },
  30. create,
  31. );
  32. return result === create;
  33. }
  34. export async function showDebugLoggingWarningMessage(): Promise<boolean> {
  35. const disable = { title: 'Disable Debug Logging' };
  36. const result = await showMessage(
  37. 'warn',
  38. 'GitLens debug logging is currently enabled. Unless you are reporting an issue, it is recommended to be disabled. Would you like to disable it?',
  39. SuppressedMessages.SuppressDebugLoggingWarning,
  40. { title: "Don't Show Again" },
  41. disable,
  42. );
  43. return result === disable;
  44. }
  45. export async function showGenericErrorMessage(message: string): Promise<void> {
  46. if (Logger.enabled(LogLevel.Error)) {
  47. const result = await showMessage('error', `${message}. See output channel for more details.`, undefined, null, {
  48. title: 'Open Output Channel',
  49. });
  50. if (result != null) {
  51. Logger.showOutputChannel();
  52. }
  53. } else {
  54. const result = await showMessage(
  55. 'error',
  56. `${message}. If the error persists, please enable debug logging and try again.`,
  57. undefined,
  58. null,
  59. {
  60. title: 'Enable Debug Logging',
  61. },
  62. );
  63. if (result != null) {
  64. void executeCommand(Commands.EnableDebugLogging);
  65. }
  66. }
  67. }
  68. export function showFileNotUnderSourceControlWarningMessage(message: string): Promise<MessageItem | undefined> {
  69. return showMessage(
  70. 'warn',
  71. `${message}. The file is probably not under source control.`,
  72. SuppressedMessages.FileNotUnderSourceControlWarning,
  73. );
  74. }
  75. export function showGitDisabledErrorMessage() {
  76. return showMessage(
  77. 'error',
  78. 'GitLens requires Git to be enabled. Please re-enable Git \u2014 set `git.enabled` to true and reload.',
  79. SuppressedMessages.GitDisabledWarning,
  80. );
  81. }
  82. export function showGitInvalidConfigErrorMessage() {
  83. return showMessage(
  84. 'error',
  85. 'GitLens is unable to use Git. Your Git configuration seems to be invalid. Please resolve any issues with your Git configuration and reload.',
  86. );
  87. }
  88. export function showGitMissingErrorMessage() {
  89. return showMessage(
  90. 'error',
  91. "GitLens was unable to find Git. Please make sure Git is installed. Also ensure that Git is either in the PATH, or that 'git.path' is pointed to its installed location.",
  92. SuppressedMessages.GitMissingWarning,
  93. );
  94. }
  95. export function showGitVersionUnsupportedErrorMessage(
  96. version: string,
  97. required: string,
  98. ): Promise<MessageItem | undefined> {
  99. return showMessage(
  100. 'error',
  101. `GitLens requires a newer version of Git (>= ${required}) than is currently installed (${version}). Please install a more recent version of Git.`,
  102. SuppressedMessages.GitVersionWarning,
  103. );
  104. }
  105. export function showPreReleaseExpiredErrorMessage(version: string) {
  106. return showMessage(
  107. 'error',
  108. `This GitLens pre-release version (${version}) has expired. Please upgrade to a more recent version.`,
  109. );
  110. }
  111. export function showLineUncommittedWarningMessage(message: string): Promise<MessageItem | undefined> {
  112. return showMessage(
  113. 'warn',
  114. `${message}. The line has uncommitted changes.`,
  115. SuppressedMessages.LineUncommittedWarning,
  116. );
  117. }
  118. export function showNoRepositoryWarningMessage(message: string): Promise<MessageItem | undefined> {
  119. return showMessage('warn', `${message}. No repository could be found.`, SuppressedMessages.NoRepositoryWarning);
  120. }
  121. export function showRebaseSwitchToTextWarningMessage(): Promise<MessageItem | undefined> {
  122. return showMessage(
  123. 'warn',
  124. 'Closing either the git-rebase-todo file or the Rebase Editor will start the rebase.',
  125. SuppressedMessages.RebaseSwitchToTextWarning,
  126. );
  127. }
  128. export function showIntegrationDisconnectedTooManyFailedRequestsWarningMessage(
  129. providerName: string,
  130. ): Promise<MessageItem | undefined> {
  131. return showMessage(
  132. 'error',
  133. `Rich integration with ${providerName} has been disconnected for this session, because of too many failed requests.`,
  134. SuppressedMessages.IntegrationDisconnectedTooManyFailedRequestsWarning,
  135. undefined,
  136. {
  137. title: 'OK',
  138. },
  139. );
  140. }
  141. export function showIntegrationRequestFailed500WarningMessage(message: string): Promise<MessageItem | undefined> {
  142. return showMessage('error', message, SuppressedMessages.IntegrationRequestFailed500Warning, undefined, {
  143. title: 'OK',
  144. });
  145. }
  146. export function showIntegrationRequestTimedOutWarningMessage(providerName: string): Promise<MessageItem | undefined> {
  147. return showMessage(
  148. 'error',
  149. `${providerName} request timed out.`,
  150. SuppressedMessages.IntegrationRequestTimedOutWarning,
  151. undefined,
  152. {
  153. title: 'OK',
  154. },
  155. );
  156. }
  157. export async function showWhatsNewMessage(version: string) {
  158. const whatsnew = { title: "See What's New" };
  159. const result = await showMessage(
  160. 'info',
  161. `GitLens ${version} is here — check out what's new!`,
  162. undefined,
  163. null,
  164. whatsnew,
  165. );
  166. if (result === whatsnew) {
  167. void (await env.openExternal(Uri.parse('https://help.gitkraken.com/gitlens/gitlens-release-notes-current/')));
  168. }
  169. }
  170. export async function showMessage(
  171. type: 'info' | 'warn' | 'error',
  172. message: string,
  173. suppressionKey?: SuppressedMessages,
  174. dontShowAgain: MessageItem | null = { title: "Don't Show Again" },
  175. ...actions: MessageItem[]
  176. ): Promise<MessageItem | undefined> {
  177. Logger.log(`ShowMessage(${type}, '${message}', ${suppressionKey}, ${JSON.stringify(dontShowAgain)})`);
  178. if (suppressionKey != null && configuration.get(`advanced.messages.${suppressionKey}` as const)) {
  179. Logger.log(`ShowMessage(${type}, '${message}', ${suppressionKey}, ${JSON.stringify(dontShowAgain)}) skipped`);
  180. return undefined;
  181. }
  182. if (suppressionKey != null && dontShowAgain !== null) {
  183. actions.push(dontShowAgain);
  184. }
  185. let result: MessageItem | undefined = undefined;
  186. switch (type) {
  187. case 'info':
  188. result = await window.showInformationMessage(message, ...actions);
  189. break;
  190. case 'warn':
  191. result = await window.showWarningMessage(message, ...actions);
  192. break;
  193. case 'error':
  194. result = await window.showErrorMessage(message, ...actions);
  195. break;
  196. }
  197. if (suppressionKey != null && (dontShowAgain === null || result === dontShowAgain)) {
  198. Logger.log(
  199. `ShowMessage(${type}, '${message}', ${suppressionKey}, ${JSON.stringify(
  200. dontShowAgain,
  201. )}) don't show again requested`,
  202. );
  203. await suppressedMessage(suppressionKey);
  204. if (result === dontShowAgain) return undefined;
  205. }
  206. Logger.log(
  207. `ShowMessage(${type}, '${message}', ${suppressionKey}, ${JSON.stringify(dontShowAgain)}) returned ${
  208. result != null ? result.title : result
  209. }`,
  210. );
  211. return result;
  212. }
  213. function suppressedMessage(suppressionKey: SuppressedMessages) {
  214. const messages = { ...configuration.get('advanced.messages') };
  215. messages[suppressionKey] = true;
  216. for (const [key, value] of Object.entries(messages)) {
  217. if (value !== true) {
  218. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  219. delete messages[key as keyof typeof messages];
  220. }
  221. }
  222. return configuration.update('advanced.messages', messages, ConfigurationTarget.Global);
  223. }