package com.gluecron.actions
import com.gluecron.GluecronUtil
import com.gluecron.settings.GluecronSettingsState
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.ui.Messages
class MergePrAction : AnAction(), DumbAware {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val settings = GluecronSettingsState.getInstance()
if (settings.token.isBlank()) {
GluecronUtil.notify(
project,
"Gluecron access token is not configured. " +
"Go to Settings → Tools → Gluecron to add your token.",
NotificationType.ERROR
)
return
}
val (owner, repo) = GluecronUtil.detectOwnerRepo(project)
?: run {
GluecronUtil.notify(
project,
"No Gluecron remote detected. Ensure the project has a git remote " +
"pointing to ${settings.host}.",
NotificationType.WARNING
)
return
}
val branch = GluecronUtil.currentBranch(project)
?: run {
GluecronUtil.notify(
project,
"Could not determine the current branch.",
NotificationType.WARNING
)
return
}
val confirmed = Messages.showYesNoDialog(
project,
"Merge the open pull request for branch \"$branch\" into $owner/$repo?\n\n" +
"This action cannot be undone.",
"Merge PR — Gluecron",
"Merge",
"Cancel",
Messages.getQuestionIcon()
)
if (confirmed != Messages.YES) return
ProgressManager.getInstance().run(object : Task.Backgroundable(
project,
"Gluecron: Merging PR for branch \"$branch\"…",
false
) {
override fun run(indicator: ProgressIndicator) {
indicator.isIndeterminate = true
try {
val path = "/api/repos/$owner/$repo/pulls/merge"
val body = """{"head":"$branch","merge_method":"merge"}"""
GluecronUtil.apiPost(path, body)
ApplicationManager.getApplication().invokeLater {
GluecronUtil.notify(
project,
"PR for branch \"$branch\" merged successfully into $owner/$repo.",
NotificationType.INFORMATION
)
}
} catch (ex: Exception) {
ApplicationManager.getApplication().invokeLater {
GluecronUtil.notify(
project,
"Failed to merge PR: ${ex.message}",
NotificationType.ERROR
)
}
}
}
})
}
override fun update(e: AnActionEvent) {
e.presentation.isEnabledAndVisible = e.project != null
}
}
|