Skip to content

Commit

Permalink
trying email
Browse files Browse the repository at this point in the history
  • Loading branch information
bretg committed Dec 18, 2024
1 parent 16e49a4 commit 9b76554
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/code-path-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ on:
- '**'

env:
EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME }}
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
OAUTH2_CLIENT_ID: : ${{ secrets.OAUTH2_CLIENT_ID }}
OAUTH2_CLIENT_SECRET: : ${{ secrets.OAUTH2_CLIENT_SECRET }}
OAUTH2_REFRESH_TOKEN: : ${{ secrets.OAUTH2_REFRESH_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -26,7 +27,7 @@ jobs:
node-version: '16'

- name: Install dependencies
run: npm install axios nodemailer
run: npm install axios nodemailer googleapis

- name: Run Notification Script
run: |
Expand Down
36 changes: 32 additions & 4 deletions test/send-notification-on-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
},
});

Expand All @@ -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,
Expand All @@ -93,3 +120,4 @@ const nodemailer = require('nodemailer');
process.exit(1);
}
})();

0 comments on commit 9b76554

Please sign in to comment.