Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'payload' field to allow for Repository Dispatch #341

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,43 @@ jobs:
label-operator: AND
```

#### Example Usage: using Repository Dispatch and `payload`

Source workflow step:

```
- uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: <orgName>/<reponame>
event-type: UNIQUE-EVENT-NAME
client-payload: '{
"github":{
"repository":{
"owner":{
"login": ${{ toJson(github.repository_owner) }}
}
},
"issue": ${{ toJson(github.event.issue) }}
}
}'
```

Target workflow trigger:
```
on:
repository_dispatch:
types: [UNIQUE-EVENT-NAME]
```

Target workflow step:

```
- uses: actions/add-to-project@RELEASE_VERSION
with:
payload: ${{ toJson(github.event.client_payload.github) }}
```

### Further reading and additional resources

- [actions/add-to-project](#actionsadd-to-project)
Expand All @@ -117,7 +154,8 @@ jobs:
token](https://github.com/settings/tokens/new) with `repo` and `project` scopes.
_See [Creating a PAT and adding it to your repository](#creating-a-pat-and-adding-it-to-your-repository) for more details_
- <a name="labeled">`labeled`</a> **(optional)** is a comma-separated list of labels used to filter applicable issues. When this key is provided, an issue must have _one_ of the labels in the list to be added to the project. Omitting this key means that any issue will be added.
- <a name="labeled">`label-operator`</a> **(optional)** is the behavior of the labels filter, either `AND`, `OR` or `NOT` that controls if the issue should be matched with `all` `labeled` input or any of them, default is `OR`.
- <a name="label-operator">`label-operator`</a> **(optional)** is the behavior of the labels filter, either `AND`, `OR` or `NOT` that controls if the issue should be matched with `all` `labeled` input or any of them, default is `OR`.
- <a name="payload">`payload`</a> **(optional)** replace the default github.context.payload with a different context, usually ${{ github }} from a different workflow, passed in as the client payload via [Repository Dispatch](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#repository_dispatch) (see [usage example](#example-usage-using-repository-dispatch-and-payload) above)

## Supported Events

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
label-operator:
required: false
description: The behavior of the labels filter, AND to match all labels, OR to match any label, NOT to exclude any listed label (default is OR)
payload:
required: false
description: Replacement context to use instead of github.context.payload (usually {{ github }} from another workflow)
runs:
using: 'node16'
main: 'dist/index.js'
5 changes: 3 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/add-to-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface ProjectV2AddDraftIssueResponse {
}

export async function addToProject(): Promise<void> {
const payload = core.getInput('payload') ? JSON.parse(core.getInput('payload')) : github.context.payload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const payload = core.getInput('payload') ? JSON.parse(core.getInput('payload')) : github.context.payload
const inputPayload = core.getInput('payload')
cont payload = inputPayload ? JSON.parse(inputPayload) : github.context.payload

const projectUrl = core.getInput('project-url', {required: true})
const ghToken = core.getInput('github-token', {required: true})
const labeled =
Expand All @@ -49,9 +50,9 @@ export async function addToProject(): Promise<void> {

const octokit = github.getOctokit(ghToken)

const issue = github.context.payload.issue ?? github.context.payload.pull_request
const issue = payload.issue ?? payload.pull_request
const issueLabels: string[] = (issue?.labels ?? []).map((l: {name: string}) => l.name.toLowerCase())
const issueOwnerName = github.context.payload.repository?.owner.login
const issueOwnerName = payload.repository?.owner.login

core.debug(`Issue/PR owner: ${issueOwnerName}`)
core.debug(`Issue/PR labels: ${issueLabels.join(', ')}`)
Expand Down