11const core = require ( '@actions/core' ) ;
22const github = require ( '@actions/github' ) ;
33
4- const token = core . getInput ( 'repo-token' ) ;
5- const project = core . getInput ( 'project' ) ;
6- const column = core . getInput ( 'column' ) ;
7-
8- const octokit = new github . GitHub ( token ) ;
9-
104const getData = ( ) => {
115 const { eventName, payload} = github . context ;
126 if ( eventName !== 'pull_request' && eventName !== 'issues' ) {
@@ -27,6 +21,10 @@ const getData = () => {
2721
2822( async ( ) => {
2923 try {
24+ const token = core . getInput ( 'repo-token' ) ;
25+ const project = core . getInput ( 'project' ) ;
26+ const column = core . getInput ( 'column' ) ;
27+
3028 const { eventName, action, nodeId, url} = getData ( ) ;
3129
3230 // Get the column ID from searching for the project and card Id if it exists
@@ -45,6 +43,7 @@ const getData = () => {
4543 projects( search: "${ project } ", first: 10, states: [OPEN] ) {
4644 nodes {
4745 id
46+ name
4847 columns( first: 100 ) {
4948 nodes {
5049 id
@@ -58,6 +57,7 @@ const getData = () => {
5857 projects( search: "${ project } ", first: 10, states: [OPEN] ) {
5958 nodes {
6059 id
60+ name
6161 columns( first: 100 ) {
6262 nodes {
6363 id
@@ -73,31 +73,34 @@ const getData = () => {
7373 }
7474 }` ;
7575
76+ const octokit = new github . GitHub ( token ) ;
7677 const { resource} = await octokit . graphql ( fetchColumnQuery ) ;
7778
78- // All the matching projects found
79+ // All the projects found
7980 const repoProjects = resource . repository . projects . nodes || [ ] ;
8081 const orgProjects = ( resource . repository . owner &&
8182 resource . repository . owner . projects &&
8283 resource . repository . owner . projects . nodes ) ||
8384 [ ] ;
8485
85- // Search the projects for columns with a name that matches
86+ // Get the column data of projects and columns that match input
8687 const columns = [ ...repoProjects , ...orgProjects ]
87- . flatMap ( projects => {
88- return projects . columns . nodes ?
89- projects . columns . nodes . filter ( projectColumn => projectColumn . name === column ) :
90- [ ] ;
91- } ) ;
92-
93- const cards = resource . projectCards . nodes ?
94- resource . projectCards . nodes . filter ( card => card . project . name === project ) : [ ] ;
95- const cardId = cards . length > 0 ? cards [ 0 ] . id : null ;
88+ . filter ( foundProject => foundProject . name === project )
89+ . flatMap ( foundProject => foundProject . columns . nodes ?
90+ foundProject . columns . nodes . filter ( projectColumn => projectColumn . name === column ) :
91+ [ ]
92+ ) ;
9693
9794 if ( columns . length === 0 ) {
98- throw new Error ( `Could not find ${ column } in ${ project } ` ) ;
95+ throw new Error ( `Could not find the column " ${ column } " in project " ${ project } " ` ) ;
9996 }
10097
98+ // Check if the issue alread has a project associated to it
99+ const cards = resource . projectCards . nodes . length === 0 ?
100+ resource . projectCards . nodes . filter ( card => card . project . name === project ) :
101+ [ ] ;
102+ const cardId = cards . length > 0 ? cards [ 0 ] . id : null ;
103+
101104 // If a card already exists, move it to the column
102105 if ( cardId ) {
103106 await Promise . all (
@@ -116,7 +119,6 @@ const getData = () => {
116119
117120 console . log ( `✅ ${ action === 'opened' ? 'Added' : 'Moved' } card to ${ column } in ${ project } ` ) ;
118121 } catch ( error ) {
119- core . error ( error ) ;
120122 core . setFailed ( error . message ) ;
121123 }
122124} ) ( ) ;
0 commit comments