|
1 | | -const async = require('async') |
2 | | -const config = require('../config') |
3 | | -const { graphqlClient } = require('../lib/github') |
4 | | -const { query, mutation } = require('../graphql') |
| 1 | +const { query } = require('../graphql') |
| 2 | +const { |
| 3 | + graphqlClient, |
| 4 | + addProjectCard, |
| 5 | + moveProjectCard, |
| 6 | + baseVariables |
| 7 | +} = require('../lib/github') |
5 | 8 |
|
6 | | -const { owner, repo } = config.github |
7 | | -const baseVariables = { owner, name: repo } |
8 | | - |
9 | | -const addToProject = async (project, issue, variables) => { |
10 | | - if (!project || project.repository.projects.edges.length === 0) return |
11 | | - if (!issue) return |
12 | | - if (!variables) return |
13 | | - |
14 | | - const { repository: { issue: { id: contentId } } } = issue |
15 | | - const projectColumnId = project.repository.projects.edges[0].node.columns.edges[0].node.id || null |
16 | | - if (contentId && projectColumnId) { |
17 | | - const projectCardMutationVariables = Object.assign({}, variables, { |
18 | | - "issue": { contentId, projectColumnId } |
19 | | - }) |
20 | | - await graphqlClient.request(mutation.AddProjectCard, projectCardMutationVariables) |
21 | | - } |
22 | | -} |
23 | | - |
24 | | -const moveProjectCard = async (destinationColumnName, issue, variables) => { |
25 | | - if (!destinationColumnName) return |
26 | | - if (!issue) return |
27 | | - if (!variables) return |
28 | | - const { repository: { issue: { projectCards } } } = issue |
29 | | - async.each(projectCards.edges, async ({ node: { |
30 | | - id: cardId, |
31 | | - project: { name: projectName }, |
32 | | - column: { id: currentColumnId, name: currentColumnName } } }) => { |
33 | | - const project = await graphqlClient.request(query.FindProjectColumns, |
34 | | - Object.assign({}, variables, { projectName }) |
35 | | - ) |
36 | | - const { repository: { projects: { edges } } } = project |
37 | | - async.each(edges, async ({ node: { columns: { edges } } }) => { |
38 | | - async.each(edges, async ({ node: { id: columnId, name: columnName } }) => { |
39 | | - if (columnId === currentColumnId) return |
40 | | - if (columnName.toLowerCase() === currentColumnName.toLowerCase()) return |
41 | | - if (columnName.toLowerCase() !== destinationColumnName.toLowerCase()) return |
42 | | - const projectCardMutationVariables = Object.assign({}, variables, { |
43 | | - "card": { cardId, columnId } |
44 | | - }) |
45 | | - await graphqlClient.request(mutation.MoveProjectCard, projectCardMutationVariables) |
46 | | - }) |
47 | | - }) |
48 | | - }) |
49 | | -} |
50 | | - |
51 | | -async function labeled (payload) { |
52 | | - const { issue: { number, state, labels }, label: { name } } = payload |
| 9 | +module.exports = async (payload) => { |
| 10 | + const { issue: { number }, label: { name } } = payload |
53 | 11 | const variables = Object.assign({}, baseVariables, { |
54 | 12 | number, projectName: name |
55 | 13 | }) |
| 14 | + |
56 | 15 | const [issue, project] = await Promise.all([ |
57 | | - graphqlClient.request(query.FindIssue, variables), |
58 | | - graphqlClient.request(query.FindProject, variables) |
| 16 | + graphqlClient.request(query.findIssue, variables), |
| 17 | + graphqlClient.request(query.findProject, variables) |
59 | 18 | ]) |
60 | | - const issueLabels = issue.repository.issue.labels.edges |
61 | | - .filter((label) => label.node.name === name) |
62 | 19 |
|
63 | | - async.each(issueLabels, async (label) => { |
64 | | - const { node: { name, description } } = label |
65 | | - switch(description) { |
66 | | - case 'project': |
67 | | - await addToProject(project, issue, variables) |
68 | | - break |
69 | | - case 'status': |
70 | | - await moveProjectCard(name, issue, variables) |
71 | | - break |
72 | | - default: |
73 | | - } |
74 | | - }) |
75 | | -} |
| 20 | + const label = issue.repository.issue.labels.edges |
| 21 | + .find(label => label.node.name === name) |
76 | 22 |
|
77 | | -module.exports = labeled |
| 23 | + const { description } = label.node |
| 24 | + |
| 25 | + switch(description) { |
| 26 | + case 'project': |
| 27 | + await addProjectCard({ label, project, issue, variables }) |
| 28 | + break |
| 29 | + case 'status': |
| 30 | + await moveProjectCard({ label, issue, variables }) |
| 31 | + break |
| 32 | + default: |
| 33 | + } |
| 34 | +} |
0 commit comments