Skip to content

Commit 1155dea

Browse files
Alex Pagebensheldon
andauthored
Match Projects by exact name (#25) (#26)
* Match Projects by exact name (#25) Co-authored-by: Ben Sheldon [he/him] <[email protected]>
1 parent 9a05133 commit 1155dea

File tree

5 files changed

+2044
-1377
lines changed

5 files changed

+2044
-1377
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].0
32+
- uses: alex-page/[email protected].3
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].0
54+
- uses: alex-page/[email protected].3
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.1.3 - Exact match for project names
9798
- v0.1.2 - Fix action not running for a card that exists in multiple projects
9899
- v0.1.1 - Document type filter so action runs once
99100
- v0.1.0 - Add support for user projects

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.

index.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const core = require('@actions/core');
22
const 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-
104
const 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

Comments
 (0)