Skip to content

Commit

Permalink
Trigger Workflow dispatch (#104)
Browse files Browse the repository at this point in the history
fix #75
  • Loading branch information
cunla authored Nov 23, 2023
1 parent e35b330 commit 10a8d95
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 14 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

## [Unreleased]

## [1.14.1]
## [1.15.0]

### 🚀 Features

- Trigger workflow dispatch event #75

### 🐛 Bug Fixes

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ object ActionKeys {

@JvmStatic
val ACTION_DATA_CONTEXT =
DataKey.create<WorkflowRunSelectionContext>("com.dsoftware.githubactionstab.workflowrun.action.datacontext")
DataKey.create<WorkflowRunSelectionContext>("com.dsoftware.ghactions.workflowrun.action.datacontext")
}
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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) {
Expand All @@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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<AnAction> {
if (e == null) return EMPTY_ARRAY
val context = e.getData(ActionKeys.ACTION_DATA_CONTEXT) ?: return EMPTY_ARRAY
val workflowTypeList: List<WorkflowType> = context.runsListLoader.workflowTypes
LOG.debug("Got ${workflowTypeList.size} workflow types")

val children: List<AnAction> = workflowTypeList.map { workflowType ->
WorkflowDispatchAction(workflowType)
}.toList()

return children.toTypedArray()
}

companion object {
private val LOG = logger<WorkflowTypesActionsGroup>()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/com/dsoftware/ghmanager/ui/Icons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
id="Github.Actions.Manager.Settings.Open"
class="com.dsoftware.ghmanager.actions.ShowPluginSettingsAction"
icon="AllIcons.General.Settings"/>
<group id="WorkflowTypesActionsGroup"
class="com.dsoftware.ghmanager.actions.WorkflowTypesActionsGroup"
icon="com.dsoftware.ghmanager.ui.Icons.WorkflowAllToolbar"
popup="true"/>

</group>

<group id="Github.Workflow.ToolWindow.List.Popup">
Expand Down

0 comments on commit 10a8d95

Please sign in to comment.