Skip to content

Commit 8bebf42

Browse files
author
Alex Holmansky
authored
Merge pull request #5 from alexh97/async-fixes
Fixing how async functions are called
2 parents 758c8db + ceaeffc commit 8bebf42

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

project-assigner/dist/index.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20077,25 +20077,29 @@ async function handleLabeled(octokit, projectName, projectColumnId, labelToMatch
2007720077
}
2007820078

2007920079
console.log(`Creating a new card for ${state} ${contentType} #${contentId} in project [${projectName}] column ${projectColumnId} mathing label [${labelToMatch}], labeled by ${github.context.payload.sender.login}`);
20080-
octokit.projects.createCard({
20081-
column_id: projectColumnId,
20082-
content_id: contentId,
20083-
content_type: contentType
20084-
}).then(function (response) {
20080+
try {
20081+
const response = await octokit.projects.createCard({
20082+
column_id: projectColumnId,
20083+
content_id: contentId,
20084+
content_type: contentType
20085+
});
2008520086
console.log(`${contentType} #${contentId} added to project ${projectName} column ${projectColumnId}`);
20086-
}).catch(function(error) {
20087+
} catch (error) {
2008720088
core.setFailed(`Error adding ${contentType} #${contentId} to project ${projectName} column ${projectColumnId}: ${error.message}`);
20088-
});
20089+
};
20090+
} else {
20091+
console.log(`No project assignments are configured for label ${labelToMatch}`);
2008920092
}
2009020093
}
2009120094

2009220095
async function handleUnlabeled(octokit, projectName, labelToMatch) {
2009320096
if (github.context.payload.label.name == labelToMatch) {
2009420097
const owner = github.context.payload.repository.owner.login;
2009520098
const repo = github.context.payload.repository.name;
20096-
var query, projectCardsPath;
20099+
var query, projectCardsPath, contentType;
2009720100

2009820101
if (github.context.eventName == "issues") {
20102+
contentType = 'Issue';
2009920103
query = `{
2010020104
repository(owner: "${owner}", name: "${repo}") {
2010120105
issue(number: ${github.context.payload.issue.number}) {
@@ -20116,6 +20120,7 @@ async function handleUnlabeled(octokit, projectName, labelToMatch) {
2011620120
projectCardsPath = 'repository.issue.projectCards.edges';
2011720121

2011820122
} else if (github.context.eventName == "pull_request") {
20123+
contentType = 'Pull request';
2011920124
query = `{
2012020125
repository(owner: "${owner}", name: "${repo}") {
2012120126
pullRequest(number: ${github.context.payload.pull_request.number}) {
@@ -20147,14 +20152,20 @@ async function handleUnlabeled(octokit, projectName, labelToMatch) {
2014720152

2014820153
if (cardToRemove) {
2014920154
const cardId = _.get(cardToRemove, 'node.databaseId');
20150-
octokit.projects.deleteCard({ card_id: cardId }).then(function(response) {
20151-
console.log(`Issue removed from project ${projectName}`);
20152-
}).catch(function(error) {
20153-
core.setFailed(`Error removing issue from project: ${error.message}`);
20154-
});
20155+
20156+
try {
20157+
const response = await octokit.projects.deleteCard({ card_id: cardId });
20158+
console.log(`${contentType} removed from project ${projectName}`);
20159+
} catch (error) {
20160+
core.setFailed(`Error removing ${contentType} from project: ${error.message}`);
20161+
};
20162+
} else {
20163+
console.log(`No card found in project ${projectName} for a given ${contentType}`);
2015520164
}
2015620165
}
20157-
}
20166+
} else {
20167+
console.log(`No project assignments are configured for label ${labelToMatch}`);
20168+
}
2015820169
}
2015920170

2016020171
async function run() {
@@ -20167,14 +20178,14 @@ async function run() {
2016720178
// console.log(`Event context: ${JSON.stringify(github.context, undefined, 2)}`);
2016820179

2016920180
if (github.context.payload.action == "labeled") {
20170-
issueMappings.forEach(mapping => {
20171-
handleLabeled(octokit, mapping.projectName, mapping.columnId, mapping.label);
20172-
});
20181+
for (const mapping of issueMappings) {
20182+
await handleLabeled(octokit, mapping.projectName, mapping.columnId, mapping.label);
20183+
};
2017320184

2017420185
} else if (github.context.payload.action == "unlabeled") {
20175-
issueMappings.forEach(mapping => {
20176-
handleUnlabeled(octokit, mapping.projectName, mapping.label);
20177-
});
20186+
for (const mapping of issueMappings) {
20187+
await handleUnlabeled(octokit, mapping.projectName, mapping.label);
20188+
};
2017820189
}
2017920190
} catch (error) {
2018020191
context = JSON.stringify(github.context, undefined, 2);

project-assigner/index.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,29 @@ async function handleLabeled(octokit, projectName, projectColumnId, labelToMatch
1818
}
1919

2020
console.log(`Creating a new card for ${state} ${contentType} #${contentId} in project [${projectName}] column ${projectColumnId} mathing label [${labelToMatch}], labeled by ${github.context.payload.sender.login}`);
21-
octokit.projects.createCard({
22-
column_id: projectColumnId,
23-
content_id: contentId,
24-
content_type: contentType
25-
}).then(function (response) {
21+
try {
22+
const response = await octokit.projects.createCard({
23+
column_id: projectColumnId,
24+
content_id: contentId,
25+
content_type: contentType
26+
});
2627
console.log(`${contentType} #${contentId} added to project ${projectName} column ${projectColumnId}`);
27-
}).catch(function(error) {
28+
} catch (error) {
2829
core.setFailed(`Error adding ${contentType} #${contentId} to project ${projectName} column ${projectColumnId}: ${error.message}`);
29-
});
30+
};
31+
} else {
32+
console.log(`No project assignments are configured for label ${labelToMatch}`);
3033
}
3134
}
3235

3336
async function handleUnlabeled(octokit, projectName, labelToMatch) {
3437
if (github.context.payload.label.name == labelToMatch) {
3538
const owner = github.context.payload.repository.owner.login;
3639
const repo = github.context.payload.repository.name;
37-
var query, projectCardsPath;
40+
var query, projectCardsPath, contentType;
3841

3942
if (github.context.eventName == "issues") {
43+
contentType = 'Issue';
4044
query = `{
4145
repository(owner: "${owner}", name: "${repo}") {
4246
issue(number: ${github.context.payload.issue.number}) {
@@ -57,6 +61,7 @@ async function handleUnlabeled(octokit, projectName, labelToMatch) {
5761
projectCardsPath = 'repository.issue.projectCards.edges';
5862

5963
} else if (github.context.eventName == "pull_request") {
64+
contentType = 'Pull request';
6065
query = `{
6166
repository(owner: "${owner}", name: "${repo}") {
6267
pullRequest(number: ${github.context.payload.pull_request.number}) {
@@ -88,14 +93,20 @@ async function handleUnlabeled(octokit, projectName, labelToMatch) {
8893

8994
if (cardToRemove) {
9095
const cardId = _.get(cardToRemove, 'node.databaseId');
91-
octokit.projects.deleteCard({ card_id: cardId }).then(function(response) {
92-
console.log(`Issue removed from project ${projectName}`);
93-
}).catch(function(error) {
94-
core.setFailed(`Error removing issue from project: ${error.message}`);
95-
});
96+
97+
try {
98+
const response = await octokit.projects.deleteCard({ card_id: cardId });
99+
console.log(`${contentType} removed from project ${projectName}`);
100+
} catch (error) {
101+
core.setFailed(`Error removing ${contentType} from project: ${error.message}`);
102+
};
103+
} else {
104+
console.log(`No card found in project ${projectName} for a given ${contentType}`);
96105
}
97106
}
98-
}
107+
} else {
108+
console.log(`No project assignments are configured for label ${labelToMatch}`);
109+
}
99110
}
100111

101112
async function run() {
@@ -108,14 +119,14 @@ async function run() {
108119
// console.log(`Event context: ${JSON.stringify(github.context, undefined, 2)}`);
109120

110121
if (github.context.payload.action == "labeled") {
111-
issueMappings.forEach(mapping => {
112-
handleLabeled(octokit, mapping.projectName, mapping.columnId, mapping.label);
113-
});
122+
for (const mapping of issueMappings) {
123+
await handleLabeled(octokit, mapping.projectName, mapping.columnId, mapping.label);
124+
};
114125

115126
} else if (github.context.payload.action == "unlabeled") {
116-
issueMappings.forEach(mapping => {
117-
handleUnlabeled(octokit, mapping.projectName, mapping.label);
118-
});
127+
for (const mapping of issueMappings) {
128+
await handleUnlabeled(octokit, mapping.projectName, mapping.label);
129+
};
119130
}
120131
} catch (error) {
121132
context = JSON.stringify(github.context, undefined, 2);

0 commit comments

Comments
 (0)