Skip to content

Instantly share code, notes, and snippets.

@nflaig
Last active March 5, 2023 21:23
Show Gist options
  • Save nflaig/9ba419e3498e52e313f2db37fd5df614 to your computer and use it in GitHub Desktop.
Save nflaig/9ba419e3498e52e313f2db37fd5df614 to your computer and use it in GitHub Desktop.
const { Octokit } = require("@octokit/rest");
const { parse } = require('issue-parser');
// Replace with your GitHub access token
const accessToken = 'YOUR_ACCESS_TOKEN';
// Replace with your GitHub repository owner and name
const owner = 'REPOSITORY_OWNER';
const repo = 'REPOSITORY_NAME';
const octokit = new Octokit({ auth: accessToken });
// Get the latest release
const { data: release } = await octokit.repos.getLatestRelease({ owner, repo });
// Parse the release notes to extract the pull request numbers
const issues = parse(release.body).issues;
// Post a comment on each pull request
for (const issue of issues) {
if (issue.prefix === 'pull') {
const prNumber = parseInt(issue.issue);
const { data: pullRequest } = await octokit.pulls.get({ owner, repo, pull_number: prNumber });
await octokit.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: `:tada: This pull request was included in [${release.name}](${release.html_url}) :tada:`
});
}
}
const https = require('https');
// Replace with your GitHub access token
const accessToken = 'YOUR_ACCESS_TOKEN';
// Replace with your GitHub repository owner and name
const owner = 'REPOSITORY_OWNER';
const repo = 'REPOSITORY_NAME';
// Define the API endpoint
const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
// Define the headers for the API requests
const headers = {
'Authorization': `token ${accessToken}`,
'User-Agent': 'node.js'
};
// Get the latest release
https.get(`${apiUrl}/releases/latest`, { headers }, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', async () => {
const release = JSON.parse(data);
// Parse the release notes to extract the pull request numbers
const issues = release.body.match(/#(\d+)/g);
// Post a comment on each pull request
for (const issue of issues) {
const prNumber = issue.replace('#', '');
const { statusCode, headers, data } = await sendRequest(`${apiUrl}/pulls/${prNumber}`);
if (statusCode === 200) {
const pullRequest = JSON.parse(data);
await sendRequest(`${apiUrl}/issues/${prNumber}/comments`, 'POST', {
body: `:tada: This pull request was included in [${release.name}](${release.html_url}) :tada:`
});
}
}
});
});
// Helper function to send HTTP requests to the GitHub API
function sendRequest(url, method = 'GET', body = null) {
return new Promise((resolve, reject) => {
const req = https.request(url, { method, headers }, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
resolve({ statusCode: res.statusCode, headers: res.headers, data });
});
});
req.on('error', error => {
reject(error);
});
if (body) {
req.write(JSON.stringify(body));
}
req.end();
});
}
const axios = require('axios');
// Replace with your GitHub access token
const accessToken = 'YOUR_ACCESS_TOKEN';
// Replace with your GitHub repository owner and name
const owner = 'REPOSITORY_OWNER';
const repo = 'REPOSITORY_NAME';
// Define the API endpoint
const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
// Define the headers for the API requests
const headers = {
'Authorization': `token ${accessToken}`,
'User-Agent': 'axios'
};
// Get the latest release
axios.get(`${apiUrl}/releases/latest`, { headers })
.then(async res => {
const release = res.data;
// Parse the release notes to extract the pull request numbers
const issues = release.body.match(/#(\d+)/g);
// Post a comment on each pull request
for (const issue of issues) {
const prNumber = issue.replace('#', '');
const { status, data } = await sendRequest(`${apiUrl}/pulls/${prNumber}`);
if (status === 200) {
const pullRequest = data;
await sendRequest(`${apiUrl}/issues/${prNumber}/comments`, 'POST', {
body: `:tada: This pull request was included in [${release.name}](${release.html_url}) :tada:`
});
}
}
})
.catch(error => {
console.error(error);
});
// Helper function to send HTTP requests to the GitHub API using axios
function sendRequest(url, method = 'GET', data = null) {
return axios({
url,
method,
headers,
data
})
.then(res => {
return { status: res.status, data: res.data };
})
.catch(error => {
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment