From 10a8d95fa9469aaa445d219b9bf608e6b0420589 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Thu, 23 Nov 2023 16:00:37 -0500 Subject: [PATCH] Trigger Workflow dispatch (#104) fix #75 --- CHANGELOG.md | 6 ++- gradle.properties | 4 +- .../dsoftware/ghmanager/actions/ActionKeys.kt | 2 +- .../ghmanager/actions/PostWorkflowActions.kt | 45 ++++++++++++++-- .../actions/WorkflowTypesActionsGroup.kt | 54 +++++++++++++++++++ .../com/dsoftware/ghmanager/api/GithubApi.kt | 6 +-- .../data/WorkflowRunSelectionContext.kt | 2 +- .../com/dsoftware/ghmanager/ui/Icons.kt | 3 -- src/main/resources/META-INF/plugin.xml | 5 ++ 9 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/com/dsoftware/ghmanager/actions/WorkflowTypesActionsGroup.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4804d2f6..4c47f7f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,11 @@ ## [Unreleased] -## [1.14.1] +## [1.15.0] + +### 🚀 Features + +- Trigger workflow dispatch event #75 ### 🐛 Bug Fixes diff --git a/gradle.properties b/gradle.properties index 8c7038a7..c7f69daa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,10 +3,10 @@ pluginGroup=com.dsoftware.ghmanager pluginName=github-actions-manager # SemVer format -> https://semver.org -pluginVersion=1.14.1 +pluginVersion=1.15.0 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html -pluginSinceBuild=232.* +pluginSinceBuild=232 pluginUntilBuild=235.* # IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension diff --git a/src/main/kotlin/com/dsoftware/ghmanager/actions/ActionKeys.kt b/src/main/kotlin/com/dsoftware/ghmanager/actions/ActionKeys.kt index b8b358b6..bb900b5c 100644 --- a/src/main/kotlin/com/dsoftware/ghmanager/actions/ActionKeys.kt +++ b/src/main/kotlin/com/dsoftware/ghmanager/actions/ActionKeys.kt @@ -20,5 +20,5 @@ object ActionKeys { @JvmStatic val ACTION_DATA_CONTEXT = - DataKey.create("com.dsoftware.githubactionstab.workflowrun.action.datacontext") + DataKey.create("com.dsoftware.ghactions.workflowrun.action.datacontext") } \ No newline at end of file diff --git a/src/main/kotlin/com/dsoftware/ghmanager/actions/PostWorkflowActions.kt b/src/main/kotlin/com/dsoftware/ghmanager/actions/PostWorkflowActions.kt index a385fbd1..bd8a2a5e 100644 --- a/src/main/kotlin/com/dsoftware/ghmanager/actions/PostWorkflowActions.kt +++ b/src/main/kotlin/com/dsoftware/ghmanager/actions/PostWorkflowActions.kt @@ -1,17 +1,23 @@ package com.dsoftware.ghmanager.actions import com.dsoftware.ghmanager.api.GithubApi +import com.dsoftware.ghmanager.api.model.WorkflowType +import com.dsoftware.ghmanager.data.RepositoryCoordinates +import com.dsoftware.ghmanager.data.WorkflowRunSelectionContext import com.intellij.icons.AllIcons import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.project.DumbAwareAction +import org.jetbrains.plugins.github.api.GithubApiRequests +import org.jetbrains.plugins.github.util.GithubUrlUtil import javax.swing.Icon abstract class PostUrlAction( - text: String, description: String?, icon: Icon + private val text: String, description: String?, icon: Icon, ) : DumbAwareAction(text, description, icon) { + private var context: WorkflowRunSelectionContext? = null override fun getActionUpdateThread(): ActionUpdateThread { return ActionUpdateThread.BGT } @@ -24,16 +30,20 @@ abstract class PostUrlAction( override fun actionPerformed(e: AnActionEvent) { e.dataContext.getData(CommonDataKeys.PROJECT) ?: return getUrl(e.dataContext)?.let { - val request = GithubApi.postRerunWorkflow(it) + val request = GithubApi.postUrl(text, it, getData(e.dataContext)) val context = e.getRequiredData(ActionKeys.ACTION_DATA_CONTEXT) val future = context.dataLoader.createDataProvider(request).request future.thenApply { - context.resetAllData() + afterPostUrl() } } } abstract fun getUrl(dataContext: DataContext): String? + open fun getData(dataContext: DataContext): Any = Object() + open fun afterPostUrl() { + context.let { it?.resetAllData() } + } } class CancelWorkflowAction : PostUrlAction("Cancel Workflow", null, AllIcons.Actions.Cancel) { @@ -55,4 +65,33 @@ class RerunWorkflowAction : PostUrlAction("Rerun Workflow", null, AllIcons.Actio dataContext.getData(CommonDataKeys.PROJECT) ?: return null return dataContext.getData(ActionKeys.SELECTED_WORKFLOW_RUN)?.rerunUrl } +} + +class WorkflowDispatchAction(private val workflowType: WorkflowType) : + PostUrlAction(workflowType.name, "Select workflow to dispatch", AllIcons.Actions.Execute) { + override fun getUrl(dataContext: DataContext): String? { + val context = dataContext.getData(ActionKeys.ACTION_DATA_CONTEXT) ?: return null + val fullPath = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(context.repositoryMapping.remote.url) + ?: throw IllegalArgumentException( + "Invalid GitHub Repository URL - ${context.repositoryMapping.remote.url} is not a GitHub repository" + ) + val repositoryCoordinates = RepositoryCoordinates(context.account.server, fullPath) + return GithubApiRequests.getUrl( + repositoryCoordinates.serverPath, + GithubApi.urlSuffix, + "/${repositoryCoordinates.repositoryPath}", + "/actions", + "/workflows", + "/${workflowType.id}", + "/dispatches", + ) + } + + override fun getData(dataContext: DataContext): Any { + val context = dataContext.getData(ActionKeys.ACTION_DATA_CONTEXT) ?: return Object() + return mapOf("ref" to context.repositoryMapping.gitRepository.currentBranch?.name) + } + + override fun afterPostUrl() { + } } \ No newline at end of file diff --git a/src/main/kotlin/com/dsoftware/ghmanager/actions/WorkflowTypesActionsGroup.kt b/src/main/kotlin/com/dsoftware/ghmanager/actions/WorkflowTypesActionsGroup.kt new file mode 100644 index 00000000..63379579 --- /dev/null +++ b/src/main/kotlin/com/dsoftware/ghmanager/actions/WorkflowTypesActionsGroup.kt @@ -0,0 +1,54 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.dsoftware.ghmanager.actions + +import com.dsoftware.ghmanager.api.model.WorkflowType +import com.intellij.openapi.actionSystem.ActionGroup +import com.intellij.openapi.actionSystem.ActionPlaces +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.impl.ActionMenu +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.ui.popup.JBPopupFactory + +class WorkflowTypesActionsGroup : ActionGroup("Select Workflow to Execute", true) { + override fun getActionUpdateThread(): ActionUpdateThread { + return ActionUpdateThread.BGT + } + + override fun update(e: AnActionEvent) { + e.presentation.isPerformGroup = true + e.presentation.isDisableGroupIfEmpty = false + e.presentation.putClientProperty(ActionMenu.SUPPRESS_SUBMENU, true) + e.presentation.isEnabledAndVisible = true + } + + override fun actionPerformed(e: AnActionEvent) { + JBPopupFactory.getInstance() + .createActionGroupPopup( + "Select Workflow to Dispatch", + this, + e.dataContext, + JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, + false, null, -1, null, + ActionPlaces.getActionGroupPopupPlace(null) + ).showInBestPositionFor(e.dataContext) + } + + override fun getChildren(e: AnActionEvent?): Array { + if (e == null) return EMPTY_ARRAY + val context = e.getData(ActionKeys.ACTION_DATA_CONTEXT) ?: return EMPTY_ARRAY + val workflowTypeList: List = context.runsListLoader.workflowTypes + LOG.debug("Got ${workflowTypeList.size} workflow types") + + val children: List = workflowTypeList.map { workflowType -> + WorkflowDispatchAction(workflowType) + }.toList() + + return children.toTypedArray() + } + + companion object { + private val LOG = logger() + } +} diff --git a/src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt b/src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt index 9c50b1aa..44ad5bfe 100644 --- a/src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt +++ b/src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt @@ -25,9 +25,9 @@ object GithubApi : GithubApiRequests.Entity("/repos") { fun getDownloadUrlForWorkflowLog(url: String) = GetRunLogRequest(url) .withOperationName("Download Workflow log") - fun postRerunWorkflow(url: String) = - GithubApiRequest.Post.Json(url, Object(), Object::class.java, null) - .withOperationName("Rerun workflow") + fun postUrl(name:String,url: String, data: Any = Object()) = + GithubApiRequest.Post.Json(url, data, Object::class.java, null) + .withOperationName(name) fun getWorkflowTypes( coordinates: RepositoryCoordinates, diff --git a/src/main/kotlin/com/dsoftware/ghmanager/data/WorkflowRunSelectionContext.kt b/src/main/kotlin/com/dsoftware/ghmanager/data/WorkflowRunSelectionContext.kt index ab482c36..c5fdbb3e 100644 --- a/src/main/kotlin/com/dsoftware/ghmanager/data/WorkflowRunSelectionContext.kt +++ b/src/main/kotlin/com/dsoftware/ghmanager/data/WorkflowRunSelectionContext.kt @@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit class WorkflowRunSelectionContext internal constructor( parentDisposable: CheckedDisposable, val project: Project, - private val account: GithubAccount, + val account: GithubAccount, val dataLoader: SingleRunDataLoader, val runsListLoader: WorkflowRunListLoader, val repositoryMapping: GHGitRepositoryMapping, diff --git a/src/main/kotlin/com/dsoftware/ghmanager/ui/Icons.kt b/src/main/kotlin/com/dsoftware/ghmanager/ui/Icons.kt index 0cc03900..ad8cac20 100644 --- a/src/main/kotlin/com/dsoftware/ghmanager/ui/Icons.kt +++ b/src/main/kotlin/com/dsoftware/ghmanager/ui/Icons.kt @@ -17,9 +17,6 @@ object Icons { @JvmField val Workflow = load("/icons/workflow.svg") - @JvmField - val WorkflowAll = load("/icons/workflow-all.svg") - @JvmField val WorkflowAllToolbar = load("/icons/workflow-all-toolbar.svg") diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 0a7ff5b6..72f484b7 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -33,6 +33,11 @@ id="Github.Actions.Manager.Settings.Open" class="com.dsoftware.ghmanager.actions.ShowPluginSettingsAction" icon="AllIcons.General.Settings"/> + +