diff --git a/.github/workflows/src/app/airtable.js b/.github/workflows/src/app/airtable.js new file mode 100644 index 00000000..dc61e0c2 --- /dev/null +++ b/.github/workflows/src/app/airtable.js @@ -0,0 +1,41 @@ +const Airtable = require('airtable') + +Airtable.configure({ + endpointUrl: 'https://api.airtable.com', + apiKey: "key4mXwIMvKyFZQzB" +}); + +const GRADUATION_TABLE = "appnpTfSaHWAf964L" + +class ATable { + fetchGraduates() { + const airtable = Airtable.base(GRADUATION_TABLE); + return new Promise((resolve, reject)=>{ + const data = [] + airtable('Graduates Data').select({ + // Selecting the first 3 records in Pending Reviews: + maxRecords: 3, + view: "Pending Reviews", + // filterByFormula: `{GitHub Username} = '${}'` + }).eachPage(function page(records, fetchNextPage) { + // This function (`page`) will get called for each page of records. + records.forEach(function(record) { + let login = record.get('GitHub Username') + data.push(login) + }); + + // To fetch the next page of records, call `fetchNextPage`. + // If there are more records, `page` will get called again. + // If there are no more records, `done` will get called. + // fetchNextPage(); + + }, function done(err) { + if (err) { console.error(err); return; } + }); + + resolve(data) + }) + } +} + +module.exports = new ATable() diff --git a/.github/workflows/src/app/octokit.js b/.github/workflows/src/app/octokit.js new file mode 100644 index 00000000..6a2806ed --- /dev/null +++ b/.github/workflows/src/app/octokit.js @@ -0,0 +1,53 @@ +const { Octokit, App, Action } = require("octokit") + +class Octo { + async fetchPr(pr) { + const octokit = new Octokit({ auth: process.env.GH_SECRET }); + + const { repository: { pullRequest } } = await octokit.graphql( + ` + query myQuery($name: String!, $owner: String!, $pr: Int!){ + repository(name: $name, owner: $owner) { + pullRequest(number: $pr) { + author { + login + } + bodyText + closed + comments(first: 100) { + edges { + node { + author { + login + } + body + url + } + } + } + + files(first: 100) { + edges { + node { + path + additions + deletions + } + } + } + } + } + } + + `, + { + name: "GitHubGraduation-2021", + owner: "campus-experts", + pr: pr + } + ) + return pullRequest + } +} + +module.exports = new Octo() diff --git a/.github/workflows/src/app/stepper.js b/.github/workflows/src/app/stepper.js deleted file mode 100644 index 44a77655..00000000 --- a/.github/workflows/src/app/stepper.js +++ /dev/null @@ -1 +0,0 @@ -import { Octokit, App, Action } from "octokit"; diff --git a/.github/workflows/src/index.js b/.github/workflows/src/index.js index 087841fe..fb6638f6 100644 --- a/.github/workflows/src/index.js +++ b/.github/workflows/src/index.js @@ -1,135 +1,4 @@ -if(!process.env.GITHUB_ACTIONS) { - const result = require('dotenv').config(); - - if (result.error) { - throw result.error - } -} - -const Airtable = require('airtable'); -const { Octokit, App, Action } = require("octokit"); - -Airtable.configure({ - endpointUrl: 'https://api.airtable.com', - apiKey: process.env.AIRTABLE_SECRET -}); - -(async ()=>{ - -// "GITHUB_EVENT_NAME": "pull_request", -// "GITHUB_REF": "refs/pull/2/merge", -// "GITHUB_API_URL": "https://api.github.com", -// "GITHUB_SHA": "b69202fcb31aeca2d94b021bfe108df1251a53ca", -// "GITHUB_GRAPHQL_URL": "https://api.github.com/graphql", -// "GITHUB_ACTOR": "renz45", -// "GITHUB_JOB": "build", -// "GITHUB_BASE_REF": "main", -// "GITHUB_WORKFLOW": "Graduation Helper", -// "GITHUB_REPOSITORY": "campus-experts/GitHubGraduation-2021", - -const GRADUATION_TABLE = "appnpTfSaHWAf964L" -const airtable = Airtable.base(GRADUATION_TABLE); -const [ owner, repoName ] = process.env.GITHUB_REPOSITORY.split('/') - -const fs = require('fs') -const ev = JSON.parse( - fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8') -) - -console.log(ev) -const pr = ev.pull_request.number - -// let pr -// console.log("EVENT NAMEL: " + process.env.GITHUB_EVENT_NAME) -// console.log(process.env.GITHUB_REF) -// if (process.env.GITHUB_EVENT_NAME === "pull_request_target") { -// pr = process.env.GITHUB_REF.split("/")[2] -// } - -// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo -const octokit = new Octokit({ auth: process.env.GH_SECRET }); - -// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user -// const { -// data: { login }, -// } = await octokit.rest.users.getAuthenticated(); -// console.log("Hello, %s", login); - -console.log("Actor: " + process.env.GITHUB_ACTOR) -airtable('Graduates Data').select({ - // Selecting the first 3 records in Pending Reviews: - maxRecords: 3, - view: "Pending Reviews", - // filterByFormula: `{GitHub Username} = '${}'` -}).eachPage(function page(records, fetchNextPage) { - // This function (`page`) will get called for each page of records. - records.forEach(function(record) { - console.log('Retrieved', record.get('GitHub Username')); - }); - - // To fetch the next page of records, call `fetchNextPage`. - // If there are more records, `page` will get called again. - // If there are no more records, `done` will get called. - // fetchNextPage(); - -}, function done(err) { - if (err) { console.error(err); return; } -}); - -const { repository: { pullRequest } } = await octokit.graphql( - ` - query myQuery($name: String!, $owner: String!, $pr: Int!){ - repository(name: $name, owner: $owner) { - pullRequest(number: $pr) { - author { - login - } - bodyText - closed - comments(first: 100) { - edges { - node { - author { - login - } - body - url - } - } - } - - files(first: 100) { - edges { - node { - path - additions - deletions - } - } - } - } - } - } - - `, - { - name: "GitHubGraduation-2021", - owner: "campus-experts", - pr: pr - } -); - -console.log(pullRequest) - - -// const { -// data: { login }, -// } = await octokit.rest.repos.pulls() -// console.log("Hello, %s", login); - - - // stepper // reverse parse markdown from bot comment, determine which step the PR is on: @@ -149,7 +18,25 @@ console.log(pullRequest) // * congrats! // * merge PR +const fs = require('fs') +const airtable = require('./app/airtable.js'); +const octokit = require('./app/octokit.js'); +if(!process.env.GITHUB_ACTIONS) { + const result = require('dotenv').config() + if (result.error) { + throw result.error + } +} else { + // const [ owner, repoName ] = process.env.GITHUB_REPOSITORY.split('/') + const eventData = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')) + const pr = eventData.pull_request.number +} +(async ()=>{ + const grads = await airtable.fetchGraduates() + const pull = await octokit.fetchPr(3) + console.log(grads) + console.log(pull) })()