Skip to content

Commit 76fe29d

Browse files
feat: add trigger.js
1 parent e7b1329 commit 76fe29d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const sync = context.getService("workSharing/v2");
2+
const apiV2 = context.getService("targetprocess/api/v2");
3+
const utils = require("utils");
4+
const commands = [];
5+
const profiles = await sync.getProfiles();
6+
const PROFILE_NAME = "0.0 Prod JIRA to ATP";
7+
const teamIterationIds = [702507]; /*Input specific Team Iteration IDs if needed*/
8+
const currentProfile = profiles.find(
9+
(p) => (p.name || "").toUpperCase() === PROFILE_NAME.toUpperCase()
10+
);
11+
12+
if (!currentProfile) {
13+
console.log(`Failed to find profile by name: "${PROFILE_NAME}"`);
14+
return;
15+
}
16+
17+
const mappingId = currentProfile.mappings[0].id;
18+
const jiraTool = currentProfile.targetTool;
19+
const jiraApi = sync.getProxy(jiraTool);
20+
21+
const getMappingTeamBoardIds = (profiles) => {
22+
const teamBoardMapping = [];
23+
profiles
24+
.filter((profile) => profile.targetTool.id === jiraTool.id)
25+
.forEach((profile) => {
26+
profile.mappings.forEach((mapping) => {
27+
mapping.typeMappings.forEach((typeMapping) => {
28+
if (
29+
typeMapping.settings &&
30+
typeMapping.settings &&
31+
typeMapping.settings.kind ===
32+
"TargetprocessTeamIterationToJiraSprint"
33+
) {
34+
teamBoardMapping.push(...typeMapping.settings.mappings);
35+
}
36+
});
37+
});
38+
});
39+
40+
return teamBoardMapping;
41+
};
42+
43+
const teamBoardIdsMapping = getMappingTeamBoardIds(profiles);
44+
45+
if (!teamBoardIdsMapping.length) {
46+
console.log(`Faield to find mapped teams.`);
47+
return;
48+
}
49+
50+
/* and StartDate>=Today */
51+
const teamIterations = await apiV2.queryAsync("teamiteration", {
52+
where: `team.id in ${JSON.stringify(
53+
teamBoardIdsMapping.map((mapping) => Number(mapping.team.id))
54+
)}
55+
and isCurrent=false
56+
${teamIterationIds.length > 0 ? `and id in [${teamIterationIds}]` : ""}`,
57+
select: `{id, name, team:team.id}`,
58+
orderBy: `endDate desc`,
59+
});
60+
61+
const CHUNK_SIZE = 4;
62+
63+
const chunks = teamIterations.reduce((acc, ti, i) => {
64+
const chunkIndex = Math.floor(i / CHUNK_SIZE);
65+
if (!acc[chunkIndex]) {
66+
acc[chunkIndex] = [];
67+
}
68+
acc[chunkIndex].push(ti);
69+
return acc;
70+
}, []);
71+
72+
console.log(chunks);
73+
74+
return chunks.map((chunk) => {
75+
return utils.activateNamedTrigger("process-ti", {
76+
tis: chunk,
77+
mapping: teamBoardIdsMapping,
78+
tool: jiraTool,
79+
mappingId,
80+
});
81+
});

0 commit comments

Comments
 (0)