Skip to content

Commit

Permalink
Match Projects by exact name (#25) (#26)
Browse files Browse the repository at this point in the history
* Match Projects by exact name (#25)

Co-authored-by: Ben Sheldon [he/him] <bensheldon@gmail.com>
  • Loading branch information
Alex Page and bensheldon authored Mar 8, 2020
1 parent 9a05133 commit 1155dea
Show file tree
Hide file tree
Showing 5 changed files with 2,044 additions and 1,377 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.1.0
- uses: alex-page/github-project-automation-plus@v0.1.3
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.1.0
- uses: alex-page/github-project-automation-plus@v0.1.3
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.1.3 - Exact match for project names
- v0.1.2 - Fix action not running for a card that exists in multiple projects
- v0.1.1 - Document type filter so action runs once
- v0.1.0 - Add support for user projects
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');

const token = core.getInput('repo-token');
const project = core.getInput('project');
const column = core.getInput('column');

const octokit = new github.GitHub(token);

const getData = () => {
const {eventName, payload} = github.context;
if (eventName !== 'pull_request' && eventName !== 'issues') {
Expand All @@ -27,6 +21,10 @@ const getData = () => {

(async () => {
try {
const token = core.getInput('repo-token');
const project = core.getInput('project');
const column = core.getInput('column');

const {eventName, action, nodeId, url} = getData();

// Get the column ID from searching for the project and card Id if it exists
Expand All @@ -45,6 +43,7 @@ const getData = () => {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
name
columns( first: 100 ) {
nodes {
id
Expand All @@ -58,6 +57,7 @@ const getData = () => {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
name
columns( first: 100 ) {
nodes {
id
Expand All @@ -73,31 +73,34 @@ const getData = () => {
}
}`;

const octokit = new github.GitHub(token);
const {resource} = await octokit.graphql(fetchColumnQuery);

// All the matching projects found
// All the projects found
const repoProjects = resource.repository.projects.nodes || [];
const orgProjects = (resource.repository.owner &&
resource.repository.owner.projects &&
resource.repository.owner.projects.nodes) ||
[];

// Search the projects for columns with a name that matches
// Get the column data of projects and columns that match input
const columns = [...repoProjects, ...orgProjects]
.flatMap(projects => {
return projects.columns.nodes ?
projects.columns.nodes.filter(projectColumn => projectColumn.name === column) :
[];
});

const cards = resource.projectCards.nodes ?
resource.projectCards.nodes.filter(card => card.project.name === project) : [];
const cardId = cards.length > 0 ? cards[0].id : null;
.filter(foundProject => foundProject.name === project)
.flatMap(foundProject => foundProject.columns.nodes ?
foundProject.columns.nodes.filter(projectColumn => projectColumn.name === column) :
[]
);

if (columns.length === 0) {
throw new Error(`Could not find ${column} in ${project}`);
throw new Error(`Could not find the column "${column}" in project "${project}"`);
}

// Check if the issue alread has a project associated to it
const cards = resource.projectCards.nodes.length === 0 ?
resource.projectCards.nodes.filter(card => card.project.name === project) :
[];
const cardId = cards.length > 0 ? cards[0].id : null;

// If a card already exists, move it to the column
if (cardId) {
await Promise.all(
Expand All @@ -116,7 +119,6 @@ const getData = () => {

console.log(`✅ ${action === 'opened' ? 'Added' : 'Moved'} card to ${column} in ${project}`);
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
})();
Loading

0 comments on commit 1155dea

Please sign in to comment.