Skip to content
This repository was archived by the owner on Sep 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from alex-page/develop
Browse files Browse the repository at this point in the history
Use backlog name, iterate over projects and columns
  • Loading branch information
Alex Page authored Apr 14, 2019
2 parents e975655 + 2da65df commit 96f72f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

## How to use

To use this action we need the project number and the name of the column for the new issues to go into.
- Get the project number from the project URL `projects/1` the number would be `1`.
- Get the column name from the project board for example "To do".
To use this action we need the project name and the name of the column for the new issues will go into. The project and column names will be used to get a column ID for automation.

In your project create a new workflow file `.github/main.workflow`:
```
Expand All @@ -18,12 +16,12 @@ workflow "✨Add new issues to projects" {
action "alex-page/add-new-issue-project" {
uses = "alex-page/add-new-issue-project@master"
args = [ "1", "To do"]
args = [ "🎒 Backlog", "To do"]
secrets = ["GITHUB_TOKEN"]
}
```

> Note: Replace `1` with your project number and `To do` with your project column.
> Note: Replace `🎒 Backlog` with your project name and `To do` with your project column.

## Release history
Expand Down
84 changes: 46 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ const { Toolkit } = require( 'actions-toolkit' );
Toolkit.run( async ( tools ) => {
try {
// Get the arguments
const projectNumber = tools.arguments._[ 0 ];
const columnName = tools.arguments._[ 1 ];
const projectName = tools.arguments._[ 0 ];
const columnName = tools.arguments._[ 1 ];

// Get the data from the event
const issueUrl = tools.context.payload.issue.html_url;
const issueTitle = tools.context.payload.issue.title;
const issueId = tools.context.payload.issue.node_id;
const issue = tools.context.payload.issue;

// Get the issue id, project name and number, column ID and name
// Get the project ID from the name
const { resource } = await tools.github.graphql(`query {
resource( url: "${ issueUrl }" ) {
resource( url: "${ issue.html_url }" ) {
... on Issue {
id
repository {
projects( first: 10, states: [OPEN] ) {
projects( search: "${ projectName }", first: 10, states: [OPEN] ) {
nodes {
name
number
columns( first: 10 ) {
columns( first: 100 ) {
nodes {
id
name
Expand All @@ -31,13 +26,10 @@ Toolkit.run( async ( tools ) => {
}
}
owner {
url
... on Organization {
projects( first: 10, states: [OPEN] ) {
projects( search: "${ projectName }", first: 10, states: [OPEN] ) {
nodes {
name
number
columns( first: 10 ) {
columns( first: 100 ) {
nodes {
id
name
Expand All @@ -52,34 +44,50 @@ Toolkit.run( async ( tools ) => {
}
}`);

// Get the project from the matching provided number
const project = resource.repository.projects.nodes
.filter( node => node.number === projectNumber )[ 0 ];

// Get the column from the matching provided column name
const column = project.columns.nodes.filter( node => node.name === columnName )[ 0 ];
const columnId = column.id;
// Get an array of all matching projects
const repoProjects = resource.repository.projects.nodes || [];
const orgProjects = resource.repository.owner
&& resource.repository.owner.projects
&& resource.repository.owner.projects.nodes
|| [];

// Get the columns with matching names
const columns = [ ...repoProjects, ...orgProjects ]
.flatMap( projects => {
return projects.columns.nodes
? projects.columns.nodes.filter( column => column.name === columnName )
: [];
});

// Check we have a valid column ID
if( !columnId || !project ) {
tools.exit.failure(
`Could not find project number "${ projectNumber }" or column "${ columnName }"`
);
if( !columns.length ) {
tools.exit.failure( `Could not find "${ projectName }" with "${ columnName }" column` );
}

// Add the card to the project
await tools.github.graphql(
`mutation {
addProjectCard( input: { contentId: "${ issueId }", projectColumnId: "${ columnId }" }) {
clientMutationId
// Add the cards to the columns
const createCards = columns.map( column => {
return new Promise( async( resolve, reject ) => {
try {
await tools.github.graphql(`mutation {
addProjectCard( input: { contentId: "${ issue.node_id }", projectColumnId: "${ column.id }" }) {
clientMutationId
}
}`);

resolve();
}
catch( error ){
reject( error );
}
}`
);
})
})

// Wait for completion
await Promise.all( createCards )
.catch( error => tools.exit.failure( error ) );

// Log success message
tools.log.success(
`Added issue "${ issueTitle }" to "${ project.name }" in "${ column.name }".`
);
tools.log.success( `Added "${ issue.title }" to "${ projectName }" in "${ columnName }".` );
}
catch( error ){
tools.exit.failure( error );
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

0 comments on commit 96f72f5

Please sign in to comment.