const ISSUES_QUERY = `
query GetIssuesAssignedToCurrentUserByDate($startDate: DateTimeOrDuration!, $endDate: DateTimeOrDuration!) {
viewer {
assignedIssues(filter: {
or: [
{ canceledAt: { gte: $startDate, lt: $endDate } },
{ completedAt: { gte: $startDate, lt: $endDate } },
{ startedAt: { gte: $startDate, lt: $endDate } }
]
}) {
nodes {
id
identifier
title
updatedAt
state {
name
}
team {
id
name
}
}
}
}
}
`;
const ISSUE_HISTORY_QUERY = `
query GetIssueHistory($issueId: String!) {
issue(id: $issueId) {
history {
nodes {
createdAt
fromState {
name
}
toState {
name
}
}
}
}
}
`;
function getYesterdayDateRange(): { startDate: string; endDate: string } {
const now = new Date();
const yesterday = new Date(now.setDate(now.getDate() - 1));
yesterday.setHours(0, 0, 0, 0);
const startDate = yesterday.toISOString();
const endDate = new Date(
yesterday.setDate(yesterday.getDate() + 1),
).toISOString();
return { startDate, endDate };
}
export async function exec(interval: Interval) {
const apiKey = Deno.env.get("LINEAR_API_KEY");
if (!apiKey) {
console.error("LINEAR_API_KEY not found in environment variables");
Deno.exit(1);
}
const { startDate, endDate } = getYesterdayDateRange();
const response = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: apiKey,
},
body: JSON.stringify({
query: ISSUES_QUERY,
variables: { startDate, endDate },
}),
});
const data = await response.json();
if (data.errors) {
console.error("Error fetching data from Linear API:", data.errors);
Deno.exit(1);
}
const issues = data.data.viewer.assignedIssues.nodes;
const issuesByTeam: { [teamName: string]: any[] } = {};
for (const issue of issues) {
const teamName = issue.team ? issue.team.name : "No Team";
if (!issuesByTeam[teamName]) {
issuesByTeam[teamName] = [];
}
const historyResponse = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: apiKey,