Skip to content

Instantly share code, notes, and snippets.

@andreasciamanna
Last active August 16, 2022 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasciamanna/0ef3598eeccadda40e6bb31546f08e1f to your computer and use it in GitHub Desktop.
Save andreasciamanna/0ef3598eeccadda40e6bb31546f08e1f to your computer and use it in GitHub Desktop.
Fix start date in Gantt chart
/*jshint esversion: 6 */
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const http = require('@jetbrains/youtrack-scripting-api/http');
const knownGanttIssues = {};
const connection = new http.Connection('https://<URL_TO_API>');
connection.addHeader('Content-Type', 'application/json;charset=UTF-8');
connection.addHeader('Authorization', 'Bearer <aUTH_TOKEN>');
const updateGanttIssuesStore = (ganttId) => {
if (knownGanttIssues.hasOwnProperty(ganttId) && !!knownGanttIssues[ganttId]) {
return;
}
if (knownGanttIssues.hasOwnProperty(ganttId) && knownGanttIssues[ganttId] === undefined) {
delete knownGanttIssues[ganttId]
};
const response = connection.getSync(`/api/gantts/${ganttId}`, [{
name: 'fields',
value: 'members(id,idReadable,startDate)'
}]);
if (response && response.code === 200) {
const data = JSON.parse(response.response);
knownGanttIssues[ganttId] = data.members;
} else if(response) {
console.error(response);
}
}
const updateStartDate = (ganttId, memberId, startDate) => {
const payLoad = {
"id": memberId,
"startDate": startDate,
};
const response = connection.postSync(`/api/gantts/${ganttId}/members/${memberId}`, [], payLoad);
if (response && response.code === 200) {
if (knownGanttIssues.hasOwnProperty(ganttId)) {
delete knownGanttIssues[ganttId];
}
} else if(response) {
console.error(response);
}
}
exports.rule = entities.Issue.onSchedule({
title: 'Fix start date in Gantt chart',
search: 'project: <SOME_PROJECT> Start Date: -Undefined #Unresolved Gantt: <SOME_GANTT_CHART>',
cron: '0 0 * ? * * *',
muteUpdateNotifications: true,
guard: function() {
return true;
},
action: function(ctx) {
const issue = ctx.issue;
const ganttIds = ['280-11', '280-26', '280-21']; // <<< I want to get all the Gantt charts belonging to the issue
for (const ganttId of ganttIds) {
updateGanttIssuesStore(ganttId);
if (knownGanttIssues.hasOwnProperty(ganttId) && knownGanttIssues[ganttId]) {
const member = knownGanttIssues[ganttId].find((item) => item.idReadable === issue.id);
if(!!member && member.startDate !== issue.fields.StartDate) {
updateStartDate(ganttId, member.id, issue.fields.StartDate);
}
}
}
},
requirements: {
StartDate: {
type: entities.Field.dateType,
name: 'Start Date',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment