I wrote my own mini module, which I use on 1 project for my own purposes. But in the developer account, I sometimes receive errors randomly. Help me to deal with it.
Code:
import api, { fetch, route, properties } from '@forge/api';
import { billing_url, test_billing_url, events } from './config.js';
// Отримати всіх користувачів з групи
const fetchAllUsersFromGroup = async (groupId) => {
const response = await api.asApp().requestJira(route`/rest/api/3/group/member?groupId=${groupId}&maxResults=1000`);
const data = await response.json();
return data;
};
// Отримати всіх користувачів з ролі
const fetchAllUsersFromRole = async (projectKey, roleId) => {
const response = await api.asApp().requestJira(route`/rest/api/3/project/${projectKey}/role/${roleId}`);
const data = await response.json();
return data;
};
// Отримати дані користувача
const fetchUser = async (accountId) => {
const response = await api.asApp().requestJira(route`/rest/api/3/user/search?accountId=${accountId}`);
const data = await response.json();
return data;
};
// Отримування даних
async function getData(projectKey, key) {
const response = await properties.onJiraProject(projectKey).get(key);
return response;
}
// Очищення налаштувань
async function clearData(projectKey) {
var json = {};
Object.keys(events).map((key) => {
json = {
...json,
[key]: {
"groups": [],
"roles": [],
"users": [],
"assignee": ["false"],
}
};
});
return json;
}
// Запуск
exports.run = async (event, context) => {
const projectKey = event['issue']['fields']['project']['key'];
const eventType = event['eventType'];
var settings = await getData(projectKey, 'settings');
if (settings == undefined) {
settings = await clearData(projectKey);
} else if (Object.keys(settings).length === 0) {
settings = await clearData(projectKey);
} else if (!settings[eventType].hasOwnProperty('assignee')) {
settings[eventType]['assignee'] = ["false"];
}
var users = [];
try {
await Promise.all(settings[eventType]['groups'].map(async (groupId) => {
const allUsersFromGroup = await fetchAllUsersFromGroup(groupId);
try {
allUsersFromGroup['values'].map(async (user) => {
if (user['accountType'] == 'atlassian' && user['active'] == true) {
if (!users.includes(user['accountId'])) {
users.push(user['accountId']);
}
};
});
} catch (error) {}
}));
} catch (error) {}
try {
await Promise.all(settings[eventType]['roles'].map(async (roleId) => {
const allUsersFromRole = await fetchAllUsersFromRole(event['issue']['fields']['project']['key'], roleId);
try {
allUsersFromRole['actors'].map(async (user) => {
const userInformation = await fetchUser(user['actorUser']['accountId']);
try {
userInformation.map(async (user) => {
if (user['accountType'] == 'atlassian' && user['active'] == true) {
if (!users.includes(user['accountId'])) {
users.push(user['accountId']);
}
};
});
} catch (error) {}
});
} catch (error) {}
}));
} catch (error) {}
try {
settings[eventType]['users'].forEach(async (userId) => {
if (!users.includes(userId)) {
users.push(userId);
}
});
} catch (error) {}
await fetch(billing_url + "/cgi-bin/jira_push_notifications.pl", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
activity: 'send_push_notification',
data: {
event: event,
context: context,
},
users: users,
assignee: settings[eventType]['assignee'][0] == 'true' ? 'true' : 'false',
}),
});
await fetch(test_billing_url + "/cgi-bin/jira_push_notifications.pl", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
activity: 'send_push_notification',
data: {
event: event,
context: context,
},
users: users,
assignee: settings[eventType]['assignee'][0] == 'true' ? 'true' : 'false',
}),
});
return 1;
};