-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (97 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { Client, Intents, MessageEmbed } = require('discord.js');
const dotenv = require('dotenv');
const { GraphQLClient, gql } = require('graphql-request');
const { addDays, differenceInDays, format } = require('date-fns');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
dotenv.config();
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const guild = client.guilds.cache.get(process.env.GUILD_ID);
const channel = guild.channels.cache.get(process.env.CHANNEL_ID);
await fetchGitlab(channel);
});
const isPastThreshold = (mergeRequest) => {
const threshold = process.env.THRESHOLD_DAYS;
return (
differenceInDays(new Date(), new Date(mergeRequest.updatedAt)) > threshold
);
};
const processInformation = (channel, data) => {
const projects = data.projects.nodes;
const mergeRequestsPastThreshold = projects
.map((project) => {
return project.mergeRequests.nodes
.filter((mergeRequest) => mergeRequest.draft === false)
.flatMap((mergeRequest) => {
return isPastThreshold(mergeRequest) ? mergeRequest : [];
})
.filter(Boolean);
})
.flat();
mergeRequestsPastThreshold.forEach((mergeRequest) =>
sendEmbed(channel, mergeRequest)
);
};
const sendEmbed = async (channel, mergeRequest) => {
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(mergeRequest.title)
.setURL(mergeRequest.webUrl)
.setDescription(mergeRequest.description)
.setThumbnail(`https://gitlab.com/${mergeRequest.author.avatarUrl}`)
.addFields(
{ name: 'Author: ', value: mergeRequest.author.name },
{
name: 'Last update: ',
value: format(new Date(mergeRequest.updatedAt), 'dd/LL/yyyy'),
}
);
channel.send({ embeds: [embed] });
};
const fetchGitlab = async (channel) => {
const projectIds = process.env.PROJECT_IDS.split(',').map(
(id) => `gid://gitlab.com/Project/${id}`
);
const query = gql`
query ($ids: [ID!]) {
projects(membership: true, ids: $ids) {
nodes {
description
fullPath
id
mergeRequests(state: opened) {
nodes {
id
createdAt
updatedAt
webUrl
title
draft
description
author {
name
avatarUrl
}
}
}
}
}
}
`;
const variables = {
ids: projectIds,
};
const gitlabClient = new GraphQLClient('https://gitlab.com/api/graphql');
gitlabClient.setHeader(
'authorization',
`Bearer ${process.env.GITLAB_ACCESS_TOKEN}`
);
const data = await gitlabClient.request(query, variables);
processInformation(channel, data);
};
client.on('error', (err) => {
console.warn(err);
});
client.login(process.env.BOT_TOKEN);