Skip to content

Commit

Permalink
v 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfattony committed Dec 15, 2021
1 parent b692116 commit 65f5854
Show file tree
Hide file tree
Showing 369 changed files with 192,158 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
yarn-error.log
32 changes: 16 additions & 16 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2021 Remato
Copyright (c) 2021 Remato

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Open pull request action


The action tries to open PR and then checks if the PR is mergeable. If mergeable then action returns PR id, if not then process exits with error code 1 to prevent later execution.
Now maximum time to wait for PR state is 3 sec.

```yaml
name: Open pull request and echo its number
on:
push:
branches: ["change_this"]
jobs:
example:
runs-on: ubuntu-latest
steps:
- name: Open PR
id: open_pr
uses: rematocorp/open-pull-request-action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
from-branch: change_this
to-branch: change_this
repository-owner: change_this
repository: change_this

- name: Echo mergeable PR number
run: echo "${{ join(steps.open_pr.outputs.*, '') }}"

```
24 changes: 24 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
author: Remato
name: open-pull-request
description: GitHub Action to open pull request and return it's id if request is mergeable, otherwise throw error to stop the flow
branding:
color: yellow
icon: anchor
inputs:
github-token:
description: Github token, https://docs.github.com/en/actions/reference/authentication-in-a-workflow
required: true
from-branch:
description: head branch
required: true
to-branch:
description: base branch
required: true
repository-owner:
required: true
repository:
required: true

runs:
using: node12
main: 'dist/index.js'
113 changes: 113 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
var core = require("@actions/core");
var Octokit = require('@octokit/core').Octokit;
var octokit;
var retryCount = 0;
var maxAttempts = 3; // https://youtu.be/-IOMNUayJjI
function run() {
return __awaiter(this, void 0, void 0, function () {
var githubToken, head, base, owner, repo, response, pull_number;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
githubToken = core.getInput('github-token', { required: true });
head = core.getInput('from-branch', { required: true });
base = core.getInput('to-branch', { required: true });
owner = core.getInput('repository-owner', { required: true });
repo = core.getInput('repository', { required: true });
octokit = new Octokit({ auth: githubToken });
return [4 /*yield*/, octokit.request('POST /repos/{owner}/{repo}/pulls', {
owner: owner,
repo: repo,
head: head,
base: base,
title: "Auto merge ".concat(head, " to ").concat(base)
})];
case 1:
response = _a.sent();
pull_number = response.data.number;
core.info('PR created');
return [4 /*yield*/, returnPullRequestNumber({
repo: repo,
owner: owner,
pull_number: pull_number
})];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
}
function returnPullRequestNumber(requestData) {
return __awaiter(this, void 0, void 0, function () {
var pull_request;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, octokit.request("GET /repos/{owner}/{repo}/pulls/{pull_number}", requestData)];
case 1:
pull_request = _a.sent();
if (!(pull_request.data.mergeable_state == 'unknown')) return [3 /*break*/, 3];
if (retryCount >= maxAttempts) {
core.error("Get pr status max attempts limit exceeded, payload: ".concat(JSON.stringify(requestData)));
process.exit(1);
}
retryCount++;
return [4 /*yield*/, delay(1000)];
case 2:
_a.sent();
return [2 /*return*/, returnPullRequestNumber(requestData)];
case 3:
if (pull_request.data.mergeable) {
core.setOutput('pull_number', requestData.pull_number);
}
else {
core.error("Pull request #".concat(requestData.pull_number, " is not mergeable"));
process.exit(1);
}
return [2 /*return*/];
}
});
});
}
function delay(ms) {
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
}
run().then();
67 changes: 67 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as core from '@actions/core'
const { Octokit } = require('@octokit/core')

let octokit
let retryCount = 0
const maxAttempts = 3 // https://youtu.be/-IOMNUayJjI

async function run(): Promise<void> {
const githubToken = core.getInput('github-token', { required: true })
const head = core.getInput('from-branch', { required: true })
const base = core.getInput('to-branch', { required: true })
const owner = core.getInput('repository-owner', { required: true })
const repo = core.getInput('repository', { required: true })

octokit = new Octokit({ auth: githubToken })

const response = await octokit.request('POST /repos/{owner}/{repo}/pulls', {
owner,
repo,
head,
base,
title: `Auto merge ${head} to ${base}`,
})

const pull_number = response.data.number

core.info('PR created')

await returnPullRequestNumber({
repo,
owner,
pull_number,
})
}

async function returnPullRequestNumber(requestData: {
owner: string
repo: string
pull_number: string
}): Promise<void> {
const pull_request = await octokit.request(`GET /repos/{owner}/{repo}/pulls/{pull_number}`, requestData)

if (pull_request.data.mergeable_state == 'unknown') {
if (retryCount >= maxAttempts) {
core.error(`Get pr status max attempts limit exceeded, payload: ${JSON.stringify(requestData)}`)

process.exit(1)
}
retryCount++
await delay(1000)

return returnPullRequestNumber(requestData)
}

if (pull_request.data.mergeable) {
core.setOutput('pull_number', requestData.pull_number)
} else {
core.error(`Pull request #${requestData.pull_number} is not mergeable`)
process.exit(1)
}
}

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

run().then()
1 change: 1 addition & 0 deletions node_modules/.bin/prettier

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 65f5854

Please sign in to comment.