Last active
August 1, 2023 06:53
-
-
Save zhongfly/d9b6b851b39a49404909da7ccf8c5f36 to your computer and use it in GitHub Desktop.
Pipedream Source/Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[Action] Github:Create a workflow dispatch event | |
import { Octokit } from "@octokit/core"; | |
import { paginateRest } from "@octokit/plugin-paginate-rest"; | |
import { default as github } from "@pipedream/github"; | |
import { ConfigurationError } from "@pipedream/platform"; | |
const CustomOctokit = Octokit.plugin(paginateRest); | |
export default defineComponent({ | |
name: "Create a workflow dispatch event", | |
description: "Create a workflow dispatch event", | |
key: "workflow", | |
version: "0.0.9", | |
type: "action", | |
props: { | |
github, | |
repoFullName: { | |
type: "string", | |
propDefinition: [ | |
github, | |
"repoFullname", | |
], | |
reloadProps: true, | |
}, | |
}, | |
async additionalProps() { | |
const repoFullName = this.repoFullName?.value || this.repoFullName; | |
const props = {}; | |
if (repoFullName != undefined){ | |
const client = new CustomOctokit({ | |
auth: this.github.$auth.oauth_access_token | |
}); | |
let workflows = await client.paginate(`GET /repos/${repoFullName}/actions/workflows`, {}); | |
workflows = workflows.map((workflow) => ({ label: workflow.name, value: `${workflow.id}`, })); | |
props.workflow = { | |
label: "Workflow", | |
type: "string", | |
description: "Input the ID of the workflow. You can also pass the workflow file name as a string.", | |
options: workflows, | |
}; | |
let refs = await client.paginate(`GET /repos/${repoFullName}/branches`, {}); | |
refs = refs.map((b)=>b.name) | |
props.ref = { | |
label: "Branch", | |
type: "string", | |
description: "Use workflow from which branch", | |
options: refs, | |
}; | |
props.inputs = { | |
type: "object", | |
description: "Input keys and values configured in the workflow file.", | |
optional: true | |
}; | |
} else { | |
throw new ConfigurationError("The input of 'Repo' is undefined!"); | |
} | |
return props; | |
}, | |
async run({ steps, $ }) { | |
function isEmptyObject(obj){ | |
if(obj && Object.keys(obj).length === 0 && obj.constructor === Object) return true | |
return false | |
} | |
const client = new CustomOctokit({ | |
auth: this.github.$auth.oauth_access_token | |
}); | |
const repoFullName = this.repoFullName?.value || this.repoFullName; | |
const workflow = this.workflow?.value || this.workflow; | |
const ref = this.ref?.value || this.ref; | |
const inputs = this.inputs?.value || this.inputs; | |
let data = { | |
ref: ref, | |
} | |
if (!isEmptyObject(inputs)) { | |
data["inputs"] = inputs; | |
} | |
let res = await client.request(`POST /repos/${repoFullName}/actions/workflows/${workflow}/dispatches`, data); | |
return res; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it:
1.mp4