generated from sourcecred/template-instance
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,22 @@ const path = require('path'); | |
const axios = require('axios'); | ||
const nodemailer = require('nodemailer'); | ||
|
||
async function getAccessToken(clientId, clientSecret, refreshToken) { | ||
try { | ||
const response = await axios.post('https://oauth2.googleapis.com/token', { | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
refresh_token: refreshToken, | ||
grant_type: 'refresh_token', | ||
}); | ||
|
||
return response.data.access_token; | ||
} catch (error) { | ||
console.error('Failed to fetch access token:', error.response?.data || error.message); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
(async () => { | ||
const configFilePath = path.join(__dirname, 'codepath-notification'); | ||
const repo = process.env.GITHUB_REPOSITORY; | ||
|
@@ -56,12 +72,23 @@ const nodemailer = require('nodemailer'); | |
|
||
console.log('Grouped matches by email:', matchesByEmail); | ||
|
||
// Configure Nodemailer | ||
// Generate OAuth2 access token | ||
const CLIENT_ID = process.env.OAUTH2_CLIENT_ID; | ||
const CLIENT_SECRET = process.env.OAUTH2_CLIENT_SECRET; | ||
const REFRESH_TOKEN = process.env.OAUTH2_REFRESH_TOKEN; | ||
|
||
const accessToken = await getAccessToken(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); | ||
|
||
// Configure Nodemailer with OAuth2 | ||
const transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
auth: { | ||
user: process.env.EMAIL_USERNAME, | ||
pass: process.env.EMAIL_PASSWORD, | ||
type: 'OAuth2', | ||
user: '[email protected]', | ||
clientId: CLIENT_ID, | ||
clientSecret: CLIENT_SECRET, | ||
refreshToken: REFRESH_TOKEN, | ||
accessToken, | ||
}, | ||
}); | ||
|
||
|
@@ -76,7 +103,7 @@ const nodemailer = require('nodemailer'); | |
|
||
try { | ||
await transporter.sendMail({ | ||
from: `"GitHub Bot" <${process.env.EMAIL_USERNAME}>`, | ||
from: `"GitHub Bot" <[email protected]>`, | ||
to: email, | ||
subject: `Files Changed in PR #${prNumber}`, | ||
html: emailBody, | ||
|
@@ -93,3 +120,4 @@ const nodemailer = require('nodemailer'); | |
process.exit(1); | ||
} | ||
})(); | ||
|