Blame · Line-by-line history
MergePrAction.kt
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 0377886 | 1 | package com.gluecron.actions |
| 2 | ||
| 3 | import com.gluecron.GluecronUtil | |
| 4 | import com.gluecron.settings.GluecronSettingsState | |
| 5 | import com.intellij.notification.NotificationType | |
| 6 | import com.intellij.openapi.actionSystem.AnAction | |
| 7 | import com.intellij.openapi.actionSystem.AnActionEvent | |
| 8 | import com.intellij.openapi.application.ApplicationManager | |
| 9 | import com.intellij.openapi.progress.ProgressIndicator | |
| 10 | import com.intellij.openapi.progress.ProgressManager | |
| 11 | import com.intellij.openapi.progress.Task | |
| 12 | import com.intellij.openapi.project.DumbAware | |
| 13 | import com.intellij.openapi.ui.Messages | |
| 14 | ||
| 15 | /** | |
| 16 | * Merges the open pull request for the currently checked-out branch. | |
| 17 | * | |
| 18 | * Flow: | |
| 19 | * 1. Detect owner/repo from the git remote URL. | |
| 20 | * 2. Detect the current branch name. | |
| 21 | * 3. Ask the user to confirm (the merge is irreversible). | |
| 22 | * 4. POST to {host}/api/repos/{owner}/{repo}/pulls/merge | |
| 23 | * with body: { "head": "<branch>", "merge_method": "merge" } | |
| 24 | * 5. Show a success or error balloon notification. | |
| 25 | * | |
| 26 | * The API call runs on a background thread via ProgressManager so it | |
| 27 | * doesn't block the EDT. | |
| 28 | * | |
| 29 | * This is the JetBrains equivalent of the VS Code "gluecron.mergePr" command. | |
| 30 | */ | |
| 31 | class MergePrAction : AnAction(), DumbAware { | |
| 32 | ||
| 33 | override fun actionPerformed(e: AnActionEvent) { | |
| 34 | val project = e.project ?: return | |
| 35 | val settings = GluecronSettingsState.getInstance() | |
| 36 | ||
| 37 | if (settings.token.isBlank()) { | |
| 38 | GluecronUtil.notify( | |
| 39 | project, | |
| 40 | "Gluecron access token is not configured. " + | |
| 41 | "Go to Settings → Tools → Gluecron to add your token.", | |
| 42 | NotificationType.ERROR | |
| 43 | ) | |
| 44 | return | |
| 45 | } | |
| 46 | ||
| 47 | val (owner, repo) = GluecronUtil.detectOwnerRepo(project) | |
| 48 | ?: run { | |
| 49 | GluecronUtil.notify( | |
| 50 | project, | |
| 51 | "No Gluecron remote detected. Ensure the project has a git remote " + | |
| 52 | "pointing to ${settings.host}.", | |
| 53 | NotificationType.WARNING | |
| 54 | ) | |
| 55 | return | |
| 56 | } | |
| 57 | ||
| 58 | val branch = GluecronUtil.currentBranch(project) | |
| 59 | ?: run { | |
| 60 | GluecronUtil.notify( | |
| 61 | project, | |
| 62 | "Could not determine the current branch.", | |
| 63 | NotificationType.WARNING | |
| 64 | ) | |
| 65 | return | |
| 66 | } | |
| 67 | ||
| 68 | // Confirm before merging — this is a destructive action | |
| 69 | val confirmed = Messages.showYesNoDialog( | |
| 70 | project, | |
| 71 | "Merge the open pull request for branch \"$branch\" into $owner/$repo?\n\n" + | |
| 72 | "This action cannot be undone.", | |
| 73 | "Merge PR — Gluecron", | |
| 74 | "Merge", | |
| 75 | "Cancel", | |
| 76 | Messages.getQuestionIcon() | |
| 77 | ) | |
| 78 | if (confirmed != Messages.YES) return | |
| 79 | ||
| 80 | // Run the API call on a background thread | |
| 81 | ProgressManager.getInstance().run(object : Task.Backgroundable( | |
| 82 | project, | |
| 83 | "Gluecron: Merging PR for branch \"$branch\"…", | |
| 84 | false | |
| 85 | ) { | |
| 86 | override fun run(indicator: ProgressIndicator) { | |
| 87 | indicator.isIndeterminate = true | |
| 88 | try { | |
| 89 | val path = "/api/repos/$owner/$repo/pulls/merge" | |
| 90 | val body = """{"head":"$branch","merge_method":"merge"}""" | |
| 91 | GluecronUtil.apiPost(path, body) | |
| 92 | ||
| 93 | ApplicationManager.getApplication().invokeLater { | |
| 94 | GluecronUtil.notify( | |
| 95 | project, | |
| 96 | "PR for branch \"$branch\" merged successfully into $owner/$repo.", | |
| 97 | NotificationType.INFORMATION | |
| 98 | ) | |
| 99 | } | |
| 100 | } catch (ex: Exception) { | |
| 101 | ApplicationManager.getApplication().invokeLater { | |
| 102 | GluecronUtil.notify( | |
| 103 | project, | |
| 104 | "Failed to merge PR: ${ex.message}", | |
| 105 | NotificationType.ERROR | |
| 106 | ) | |
| 107 | } | |
| 108 | } | |
| 109 | } | |
| 110 | }) | |
| 111 | } | |
| 112 | ||
| 113 | override fun update(e: AnActionEvent) { | |
| 114 | e.presentation.isEnabledAndVisible = e.project != null | |
| 115 | } | |
| 116 | } |