Skip to content

Commit

Permalink
Refactor add and move card logic ✨ (#31)
Browse files Browse the repository at this point in the history
* Refactor add and move card logic ✨
  • Loading branch information
Alex Page authored Mar 26, 2020
1 parent 0597b62 commit e856528
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 52 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
automate-project-columns:
runs-on: ubuntu-latest
steps:
- uses: alex-page/github-project-automation-plus@v0.2.1
- uses: alex-page/github-project-automation-plus@v0.2.2
with:
project: Backlog
column: Triage
Expand All @@ -51,7 +51,7 @@ jobs:
automate-project-columns:
runs-on: ubuntu-latest
steps:
- uses: alex-page/github-project-automation-plus@v0.2.1
- uses: alex-page/github-project-automation-plus@v0.2.2
with:
project: Backlog
column: To do
Expand Down Expand Up @@ -94,6 +94,7 @@ GraphqlError: Resource protected by organization SAML enforcement. You must gran
## Release History
- v0.2.2 - Refactor add and move card logic ✨
- v0.2.1 - Fix bug with move logic when card is already in project
- v0.2.0 - Restructure project, add tests, change add and move logic
- v0.1.3 - Exact match for project names
Expand Down
3 changes: 0 additions & 3 deletions __tests__/generate-mutation-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const moveData = {
nodes: [
{
id: 'MDExOlByb2plY3RDYXJkMzUxNzI2MjM=',
column: {
id: 'MDEzOlByb2plY3RDb2x1bW44NDU0MzQ6'
},
project: {
name: project,
id: 'MDc6UHJvamVjdDQwNzU5MDI='
Expand Down
6 changes: 0 additions & 6 deletions __tests__/generate-project-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const issueQuery = `query {
projectCards {
nodes {
id
column {
id
}
project {
name
id
Expand Down Expand Up @@ -57,9 +54,6 @@ const pullrequestQuery = `query {
projectCards {
nodes {
id
column {
id
}
project {
name
id
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-project-automation-plus",
"version": "0.2.1",
"version": "0.2.2",
"description": "🤖 Automate GitHub Project cards with any webhook event",
"private": true,
"main": "dist/index.js",
Expand Down
58 changes: 23 additions & 35 deletions src/generate-mutation-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {string} contentId - The id of the issue or pull request
*/
const generateMutationQuery = (data, projectName, columnName, contentId) => {
// All the projects found
// All the projects found in organisation and repositories
const repoProjects = data.repository.projects.nodes || [];
const orgProjects = (data.repository.owner &&
data.repository.owner.projects &&
Expand All @@ -29,53 +29,41 @@ const generateMutationQuery = (data, projectName, columnName, contentId) => {
throw new Error(`Could not find the column "${columnName}" or project "${projectName}"`);
}

const cardLocations = {};

// Get the ids of the end card location
const endLocationIds = endLocation.map(project => ({
projectId: project.id,
columnId: project.columns.nodes
.filter(column => column.name === columnName)
.map(column => column.id)[0]
}));
endLocation.forEach(project => {
cardLocations[project.id] = {
columnId: project.columns.nodes
.filter(column => column.name === columnName)
.map(column => column.id)[0]
};
});

// See if the card has a current location
// See if the card exists in the provided project
const currentLocation = data.projectCards.nodes
.filter(card => card.project.name === projectName);

const currentLocationIds = currentLocation.map(card => ({
projectId: card.project.id,
columnId: card.column.id,
cardId: card.id
}));
currentLocation.forEach(card => {
cardLocations[card.project.id].cardId = card.id;
});

// Get cards to create when they do not have a matching existing project
const currentCardProjectIds = currentLocationIds.map(ids => ids.projectId);
const newCards = endLocationIds.filter(ids => !currentCardProjectIds.includes(ids.projectId));
// If the card already exists in the project move it otherwise add a new card
const mutations = Object.keys(cardLocations).map(mutation => cardLocations[mutation].cardId ?
`mutation {
moveProjectCard( input: {
cardId: "${cardLocations[mutation].cardId}",
columnId: "${cardLocations[mutation].columnId}"
}) { clientMutationId } }` :

// Create an an array of queries to add the card
const addProjectCardQueries = newCards.map(card =>
`mutation {
addProjectCard( input: {
contentId: "${contentId}",
projectColumnId: "${card.columnId}"
projectColumnId: "${cardLocations[mutation].columnId}"
}) { clientMutationId } }`
);

// Get cards to move when they exist in a project
const endLocationProjectIds = endLocationIds.map(ids => ids.projectId);
const moveCards = currentLocationIds.filter(ids => endLocationProjectIds.includes(ids.projectId));

// Create an array of queries to move the card
const moveProjectCardQueries = moveCards.map(card => {
const endLocation = endLocationIds.filter(ids => ids.projectId === card.projectId)[0];

return `mutation {
moveProjectCard( input: {
cardId: "${card.cardId}",
columnId: "${endLocation.columnId}"
}) { clientMutationId } }`;
});

return [...addProjectCardQueries, ...moveProjectCardQueries];
return mutations;
};

module.exports = generateMutationQuery;
3 changes: 0 additions & 3 deletions src/generate-project-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ const projectQuery = (url, eventName, project) => (
projectCards {
nodes {
id
column {
id
}
project {
name
id
Expand Down

0 comments on commit e856528

Please sign in to comment.