Skip to content

Commit

Permalink
feat: Convert environment variables to action inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-archer committed Nov 7, 2019
1 parent 0c0521d commit 5142ed6
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 88 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'PR from test to master'
description: 'Check if test is behind master, if so create a branch capturing the state of test and open a PR to master from it'
author: 'iFit'
inputs:
pr-reviewers:
description: 'Comma seperated list of github user ids who will review the PR'
default: ''
pr-team-reviewers:
description: 'Comma seperated list of github teams whoe will review the PR'
default: ''
runs:
using: 'node12'
main: 'lib/pr-to-master.js'
with:
repository-name: ${{ github.repository }}
108 changes: 108 additions & 0 deletions lib/pr-to-master.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const request = __importStar(require("request-promise-native"));
const core = __importStar(require("@actions/core"));
// API Docs: https://developer.github.com/v3
const { GITHUB_API_TOKEN } = process.env;
const repositoryName = core.getInput('repository-name');
const prReviewers = core.getInput('pr-reviewers');
const prTeamReviewers = core.getInput('pr-team-reviewers');
const REPO = `https://api.github.com/repos/ifit/${repositoryName}`;
const defaults = {
headers: {
'User-Agent': 'bender-ifit',
Authorization: `Bearer ${GITHUB_API_TOKEN}`
},
json: true,
};
const GET = (uri, opts) => request(Object.assign({ method: 'GET', uri }, opts, defaults));
const POST = (uri, body, opts) => request(Object.assign({ method: 'POST', uri, body }, opts, defaults));
let getBranchHead;
{
let refs;
getBranchHead = (branch) => __awaiter(this, void 0, void 0, function* () {
if (!refs) {
refs = yield GET(`${REPO}/git/refs/heads`);
}
return refs.find(ref => ref.ref === `refs/heads/${branch}`).object.sha;
});
}
function createBranch(name) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
ref: `refs/heads/${name}`,
sha: yield getBranchHead('test')
};
const result = yield POST(`${REPO}/git/refs`, body);
return result;
});
}
function createPR(title, body, head, base) {
return __awaiter(this, void 0, void 0, function* () {
return POST(`${REPO}/pulls`, {
title,
body,
head,
base
});
});
}
function requestReview(pullNumber) {
return __awaiter(this, void 0, void 0, function* () {
return POST(`${REPO}/pulls/${pullNumber}/requested_reviewers`, {
team_reviewers: (prTeamReviewers || "").split(','),
reviewers: (prReviewers || "").split(','),
});
});
}
function noDiff() {
return __awaiter(this, void 0, void 0, function* () {
const test = yield getBranchHead("test");
const master = yield getBranchHead("master");
return test === master;
});
}
function createAutoPR() {
return __awaiter(this, void 0, void 0, function* () {
if (yield noDiff()) {
console.log("test and master are identical so no PR will be created");
return;
}
const d = new Date();
const branchName = "test2master-" + d.toISOString().substr(0, 10);
yield createBranch(branchName);
console.log(`branch created: ${branchName}`);
const prTitle = 'Auto PR ' + branchName.replace('-', ' ');
const prBody = `
Make sure all these commits are ready to be merged into master.
Feel free to request one or more reviews if you aren't sure.
If you _are_ sure then approve and merge.
`;
const pr = yield createPR(prTitle, prBody, branchName, 'master');
console.log(`PR created: ${prTitle}`);
yield requestReview(pr.number);
console.log(`review requested`);
return 'success';
});
}
createAutoPR()
.then(console.log)
.catch((err) => {
console.error(err);
process.exit(1);
});
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "ifit-actions",
"repository": "https://github.com/ifit/ifit-actions",
"private": true,
"dependencies": {
"@actions/core": "^1.2.0",
"request-promise-native": "^1.0.8"
},
"devDependencies": {
"@types/node": "^12.12.6"
}
}
88 changes: 0 additions & 88 deletions pr-to-master.js

This file was deleted.

96 changes: 96 additions & 0 deletions src/pr-to-master.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as request from 'request-promise-native';
import * as core from '@actions/core';

// API Docs: https://developer.github.com/v3
const { GITHUB_API_TOKEN } = process.env;

const repositoryName = core.getInput('repository-name');
const prReviewers = core.getInput('pr-reviewers');
const prTeamReviewers = core.getInput('pr-team-reviewers');

console.log({ GITHUB_API_TOKEN: Boolean(GITHUB_API_TOKEN), repositoryName, prReviewers, prTeamReviewers})

// const REPO = `https://api.github.com/repos/ifit/${repositoryName}`;
// const defaults = {
// headers: {
// 'User-Agent': 'bender-ifit',
// Authorization: `Bearer ${GITHUB_API_TOKEN}`
// },
// json: true,
// };
// const GET = (uri, opts?) => request(Object.assign({ method: 'GET', uri }, opts, defaults));
// const POST = (uri, body, opts?) => request(Object.assign({ method: 'POST', uri, body }, opts, defaults));

// let getBranchHead;
// {
// let refs;
// getBranchHead = async (branch) => {
// if (!refs) {
// refs = await GET(`${REPO}/git/refs/heads`);
// }
// return refs.find(ref => ref.ref === `refs/heads/${branch}`).object.sha;
// }
// }

// async function createBranch(name) {
// const body = {
// ref: `refs/heads/${name}`,
// sha: await getBranchHead('test')
// };
// const result = await POST(`${REPO}/git/refs`, body);
// return result;
// }

// async function createPR(title, body, head, base) {
// return POST(`${REPO}/pulls`, {
// title,
// body,
// head,
// base
// });
// }

// async function requestReview(pullNumber) {
// return POST(
// `${REPO}/pulls/${pullNumber}/requested_reviewers`,
// {
// team_reviewers: (prTeamReviewers || "").split(','),
// reviewers: (prReviewers || "").split(','),
// }
// );
// }

// async function noDiff() {
// const test = await getBranchHead("test");
// const master = await getBranchHead("master");
// return test === master;
// }

// async function createAutoPR() {
// if (await noDiff()) {
// console.log("test and master are identical so no PR will be created");
// return;
// }
// const d = new Date();
// const branchName = "test2master-" + d.toISOString().substr(0,10);
// await createBranch(branchName);
// console.log(`branch created: ${branchName}`)
// const prTitle = 'Auto PR ' + branchName.replace('-', ' ');
// const prBody = `
// Make sure all these commits are ready to be merged into master.
// Feel free to request one or more reviews if you aren't sure.
// If you _are_ sure then approve and merge.
// `;
// const pr = await createPR(prTitle, prBody, branchName, 'master');
// console.log(`PR created: ${prTitle}`)
// await requestReview(pr.number);
// console.log(`review requested`)
// return 'success';
// }

// createAutoPR()
// .then(console.log)
// .catch((err) => {
// console.error(err);
// process.exit(1);
// });
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es6"
],
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": ["node_modules"]
}
Loading

0 comments on commit 5142ed6

Please sign in to comment.