Skip to content

Commit e856528

Browse files
author
Alex Page
authored
Refactor add and move card logic ✨ (#31)
* Refactor add and move card logic ✨
1 parent 0597b62 commit e856528

File tree

8 files changed

+29
-52
lines changed

8 files changed

+29
-52
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
automate-project-columns:
3030
runs-on: ubuntu-latest
3131
steps:
32-
- uses: alex-page/[email protected].1
32+
- uses: alex-page/[email protected].2
3333
with:
3434
project: Backlog
3535
column: Triage
@@ -51,7 +51,7 @@ jobs:
5151
automate-project-columns:
5252
runs-on: ubuntu-latest
5353
steps:
54-
- uses: alex-page/[email protected].1
54+
- uses: alex-page/[email protected].2
5555
with:
5656
project: Backlog
5757
column: To do
@@ -94,6 +94,7 @@ GraphqlError: Resource protected by organization SAML enforcement. You must gran
9494
9595
## Release History
9696
97+
- v0.2.2 - Refactor add and move card logic ✨
9798
- v0.2.1 - Fix bug with move logic when card is already in project
9899
- v0.2.0 - Restructure project, add tests, change add and move logic
99100
- v0.1.3 - Exact match for project names

__tests__/generate-mutation-query.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const moveData = {
1111
nodes: [
1212
{
1313
id: 'MDExOlByb2plY3RDYXJkMzUxNzI2MjM=',
14-
column: {
15-
id: 'MDEzOlByb2plY3RDb2x1bW44NDU0MzQ6'
16-
},
1714
project: {
1815
name: project,
1916
id: 'MDc6UHJvamVjdDQwNzU5MDI='

__tests__/generate-project-query.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ const issueQuery = `query {
88
projectCards {
99
nodes {
1010
id
11-
column {
12-
id
13-
}
1411
project {
1512
name
1613
id
@@ -57,9 +54,6 @@ const pullrequestQuery = `query {
5754
projectCards {
5855
nodes {
5956
id
60-
column {
61-
id
62-
}
6357
project {
6458
name
6559
id

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-project-automation-plus",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "🤖 Automate GitHub Project cards with any webhook event",
55
"private": true,
66
"main": "dist/index.js",

src/generate-mutation-query.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @param {string} contentId - The id of the issue or pull request
88
*/
99
const 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

8169
module.exports = generateMutationQuery;

src/generate-project-query.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ const projectQuery = (url, eventName, project) => (
1212
projectCards {
1313
nodes {
1414
id
15-
column {
16-
id
17-
}
1815
project {
1916
name
2017
id

0 commit comments

Comments
 (0)