Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Page committed Aug 31, 2019
0 parents commit 89716e3
Show file tree
Hide file tree
Showing 9 changed files with 4,909 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
76 changes: 76 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at alex@alexpage.com.au. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Alex Page

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# GitHub Project Automation+

> 🤖 Automate GitHub Project cards with any webhook event
This action allows you to use any of the `pull_request` and `issue` [webhook events](https://help.github.com/en/articles/events-that-trigger-workflows#webhook-events) to automate your project cards. For example when an `issue` is `opened` create a card in the Backlog project, Triage column.

If the `pull_request` or `issue` has a card it will be moved to the column provided. Otherwise the card will be created in the column.


## Usage

1. Create a new workflow by adding `.github/workflows/backlog-automation.yml` to your project.
2. Create a [project](https://help.github.com/en/articles/about-project-boards) with Columns set up in your repository or organisation.
3. In the `backlog-automation.yml` you have to decide what events and actions are going move an issue or pull request to a column.


_For example:_

To move opened issues into the Triage column and assigned pull requests into the To Do column of the Backlog project. You would add the following to the `backlog-automation.yml` file:

```yml
name: Automate project columns

on: [issues, pull_request]

jobs:
automate-project-columns:
runs-on: ubuntu-latest
steps:
- name: Move new issues into Triage
if: github.event_name == 'issues' && github.event.action == 'opened'
uses: alex-page/automate-project-columns@master
with:
project: Backlog
column: Triage
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Move assinged pull requests into To do
if: github.event_name == 'pull_request' && github.event.action == 'assigned'
uses: alex-page/automate-project-columns@master
with:
project: Backlog
column: To do
repo-token: ${{ secrets.GITHUB_TOKEN }}
```
## Workflow options
These are the options recommended to be changed. For more detailed explanation of the workflow file, check out the [GitHub documentation](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file).
| Setting | Description | Values |
| --- | --- | --- |
| `on` | When the automation is ran | `[issues, pull_request]` |
| `github.event.name` | The event type | `issues` or `pull_request` |
| `github.event.action` | The [webhook event](https://help.github.com/en/articles/events-that-trigger-workflows#webhook-events) that triggers the automation | `opened`, `assigned`, [...more](https://help.github.com/en/articles/events-that-trigger-workflows#webhook-events) |
| `project` | The name of the project | `Backlog` |
| `column` | The column to create or move the card to | `Triage` |


## Release History

- v0.0.0 - Initial release
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'GitHub Project Automation+'
description: '🤖 Automate GitHub Project cards with any webhook event'
author: 'Alex Page <alex@alexpage.com.au>'
inputs:
repo-token:
description: 'The GITHUB_TOKEN secret can be passed in using {{ secrets.GITHUB_TOKEN }}'
required: true
project:
description: 'The name of the GitHub Project'
required: true
column:
description: 'The name of the column to move the issue or pull request to'
required: true
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'plus'
color: 'yellow'
1 change: 1 addition & 0 deletions dist/index.js

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const core = require('@actions/core');
const github = require('@actions/github');

const token = core.getInput('repo-token');
const project = core.getInput('project');
const column = core.getInput('column');

const octokit = new github.GitHub(token);

const getData = () => {
const {eventName, payload} = github.context;
if (eventName !== 'pull_request' && eventName !== 'issues') {
throw new Error(`Only pull requests or issues allowed, received:\n${eventName}`);
}

const githubData = eventName === 'issues' ?
payload.issue :
payload.pull_request;

return {
eventName,
action: payload.action,
nodeId: githubData.node_id,
url: githubData.html_url
};
};

(async () => {
try {
const {eventName, action, nodeId, url} = getData();

// Get the column ID from searching for the project and card Id if it exists
const fetchColumnQuery = `query {
resource( url: "${url}" ) {
... on ${eventName === 'issues' ? 'Issue' : 'PullRequest'} {
projectCards {
nodes {
id
}
}
repository {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
columns( first: 100 ) {
nodes {
id
name
}
}
}
}
owner {
... on Organization {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
columns( first: 100 ) {
nodes {
id
name
}
}
}
}
}
}
}
}
}
}`;

const {resource} = await octokit.graphql(fetchColumnQuery);

// All the matching projects found
const repoProjects = resource.repository.projects.nodes || [];
const orgProjects = (resource.repository.owner &&
resource.repository.owner.projects &&
resource.repository.owner.projects.nodes) ||
[];

// Search the projects for columns with a name that matches
const columns = [...repoProjects, ...orgProjects]
.flatMap(projects => {
return projects.columns.nodes ?
projects.columns.nodes.filter(projectColumn => projectColumn.name === column) :
[];
});

const cardId = (resource.projectCards.nodes &&
resource.projectCards.nodes[0] &&
resource.projectCards.nodes[0].id) ||
null;

if (columns.length === 0) {
throw new Error(`Could not find ${column} in ${project}`);
}

// If a card already exists, move it to the column
if (cardId) {
await Promise.all(
columns.map(column => octokit.graphql(`mutation {
moveProjectCard( input: { cardId: "${cardId}", columnId: "${column.id}"
}) { clientMutationId } }`))
);
// If the card does not exist, add it to the column
} else {
await Promise.all(
columns.map(column => octokit.graphql(`mutation {
addProjectCard( input: { contentId: "${nodeId}", projectColumnId: "${column.id}"
}) { clientMutationId } }`))
);
}

console.log(`✅ ${action === 'opened' ? 'Added' : 'Moved'} card to ${column} in ${project}`);
} catch (error) {
console.error(error);
}
})();
Loading

0 comments on commit 89716e3

Please sign in to comment.