77 * @param {string } contentId - The id of the issue or pull request
88 */
99const generateMutationQuery = ( data , projectName , columnName , contentId ) => {
10- // All the projects found
10+ // All the projects found in organisation and repositories
1111 const repoProjects = data . repository . projects . nodes || [ ] ;
1212 const orgProjects = ( data . repository . owner &&
1313 data . repository . owner . projects &&
@@ -29,53 +29,41 @@ const generateMutationQuery = (data, projectName, columnName, contentId) => {
2929 throw new Error ( `Could not find the column "${ columnName } " or project "${ projectName } "` ) ;
3030 }
3131
32+ const cardLocations = { } ;
33+
3234 // Get the ids of the end card location
33- const endLocationIds = endLocation . map ( project => ( {
34- projectId : project . id ,
35- columnId : project . columns . nodes
36- . filter ( column => column . name === columnName )
37- . map ( column => column . id ) [ 0 ]
38- } ) ) ;
35+ endLocation . forEach ( project => {
36+ cardLocations [ project . id ] = {
37+ columnId : project . columns . nodes
38+ . filter ( column => column . name === columnName )
39+ . map ( column => column . id ) [ 0 ]
40+ } ;
41+ } ) ;
3942
40- // See if the card has a current location
43+ // See if the card exists in the provided project
4144 const currentLocation = data . projectCards . nodes
4245 . filter ( card => card . project . name === projectName ) ;
4346
44- const currentLocationIds = currentLocation . map ( card => ( {
45- projectId : card . project . id ,
46- columnId : card . column . id ,
47- cardId : card . id
48- } ) ) ;
47+ currentLocation . forEach ( card => {
48+ cardLocations [ card . project . id ] . cardId = card . id ;
49+ } ) ;
4950
50- // Get cards to create when they do not have a matching existing project
51- const currentCardProjectIds = currentLocationIds . map ( ids => ids . projectId ) ;
52- const newCards = endLocationIds . filter ( ids => ! currentCardProjectIds . includes ( ids . projectId ) ) ;
51+ // If the card already exists in the project move it otherwise add a new card
52+ const mutations = Object . keys ( cardLocations ) . map ( mutation => cardLocations [ mutation ] . cardId ?
53+ `mutation {
54+ moveProjectCard( input: {
55+ cardId: "${ cardLocations [ mutation ] . cardId } ",
56+ columnId: "${ cardLocations [ mutation ] . columnId } "
57+ }) { clientMutationId } }` :
5358
54- // Create an an array of queries to add the card
55- const addProjectCardQueries = newCards . map ( card =>
5659 `mutation {
5760 addProjectCard( input: {
5861 contentId: "${ contentId } ",
59- projectColumnId: "${ card . columnId } "
62+ projectColumnId: "${ cardLocations [ mutation ] . columnId } "
6063 }) { clientMutationId } }`
6164 ) ;
6265
63- // Get cards to move when they exist in a project
64- const endLocationProjectIds = endLocationIds . map ( ids => ids . projectId ) ;
65- const moveCards = currentLocationIds . filter ( ids => endLocationProjectIds . includes ( ids . projectId ) ) ;
66-
67- // Create an array of queries to move the card
68- const moveProjectCardQueries = moveCards . map ( card => {
69- const endLocation = endLocationIds . filter ( ids => ids . projectId === card . projectId ) [ 0 ] ;
70-
71- return `mutation {
72- moveProjectCard( input: {
73- cardId: "${ card . cardId } ",
74- columnId: "${ endLocation . columnId } "
75- }) { clientMutationId } }` ;
76- } ) ;
77-
78- return [ ...addProjectCardQueries , ...moveProjectCardQueries ] ;
66+ return mutations ;
7967} ;
8068
8169module . exports = generateMutationQuery ;
0 commit comments