forked from rematocorp/open-pull-request-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (53 loc) · 1.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()