Skip to content

Commit

Permalink
Add mergePullRequest method (#23)
Browse files Browse the repository at this point in the history
* Add mergePullRequest method

* Add retryPullRequestCheck/retryPullRequestChecks method

* Update github.js to add debug methods via CURL

Try and bypass some weird 500 issues with undici

* Update github.js curl methods
  • Loading branch information
extremeheat committed Apr 24, 2024
1 parent 680a78d commit 2d00e80
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
78 changes: 76 additions & 2 deletions src/github.js
Expand Up @@ -3,6 +3,12 @@ const fs = require('fs')
const github = require('@actions/github')
const core = require('@actions/core')
const { DefaultArtifactClient } = require('@actions/artifact')
const cp = require('child_process')
function exec (cmd) {
console.log('$ ', cmd)
// inherit stderr, capture stdout
return cp.execSync(cmd, { stdio: ['inherit', 'pipe'] }).toString()
}

// const runningOverActions = !!process.env.GITHUB_ACTIONS

Expand Down Expand Up @@ -42,6 +48,15 @@ function mod (githubContext, githubToken) {
return currentUserData
}

function _sendRequestWithCURL (url, jsonPayload) {
jsonPayload = typeof jsonPayload === 'string' ? jsonPayload : JSON.stringify(jsonPayload)
const escapedPayload = jsonPayload.replace(/'/g, "\\'")
const cmd = `curl -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${token}" -d '${escapedPayload}' ${url}`
const result = exec(cmd)
console.log('> ', result)
return JSON.parse(result)
}

// Artifacts
async function uploadArtifact (name, files, filesRoot, options) {
const { id, size } = await artifact.uploadArtifact(name, files, filesRoot, {
Expand Down Expand Up @@ -375,7 +390,11 @@ function mod (githubContext, githubToken) {
name: check.name,
status: check.status,
conclusion: check.conclusion,
url: check.html_url
url: check.html_url,
startedAt: check.started_at,
completedAt: check.completed_at,
output: check.output,
id: check.id
}))
}

Expand All @@ -395,6 +414,22 @@ function mod (githubContext, githubToken) {
return checks
}

async function retryPullRequestCheck (id) {
await octokit.rest.checks.rerequestRun({
...context.repo,
check_run_id: id
})
}

async function retryPullRequestChecks (number) {
// Rerun failed checks
const checks = await getPullRequestChecks(number)
const failed = checks.filter(check => check.conclusion === 'failure')
for (const check of failed) {
await retryPullRequestCheck(number, check.id)
}
}

async function createPullRequest (title, body, fromBranch, intoBranch) {
if (!intoBranch) {
intoBranch = await getDefaultBranch()
Expand All @@ -412,6 +447,19 @@ function mod (githubContext, githubToken) {
}
}

function _createPullRequestCURL (title, body, fromBranch, intoBranch) {
const result = _sendRequestWithCURL(`https://api.github.com/repos/${context.repo.owner}/${context.repo.repo}/pulls`, JSON.stringify({
title,
body,
head: fromBranch,
base: intoBranch
}))
return {
number: result.number,
url: result.html_url
}
}

async function createPullRequestReview (id, payload) {
debug('createPullRequestReview', id, payload)
await octokit.rest.pulls.createReview({
Expand All @@ -421,6 +469,17 @@ function mod (githubContext, githubToken) {
})
}

async function mergePullRequest (id, { method, title, message }) {
const pr = await octokit.rest.pulls.merge({
...context.repo,
pull_number: id,
merge_method: method,
commit_title: title,
commit_message: message
})
return pr.data
}

async function addCommentReaction (commentId, reaction) {
await octokit.rest.reactions.createForIssueComment({
...context.repo,
Expand Down Expand Up @@ -481,6 +540,14 @@ function mod (githubContext, githubToken) {
})
}

// For debugging purposes, we can also send a workflow dispatch event using CURL
function _sendWorkflowDispatchCURL ({ owner, repo, branch, workflow, inputs }) {
return _sendRequestWithCURL(`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow}/dispatches`, JSON.stringify({
ref: branch || 'main',
inputs
}))
}

function onRepoComment (fn) {
const payload = context.payload
if (payload.comment && payload.issue) {
Expand Down Expand Up @@ -612,6 +679,8 @@ function mod (githubContext, githubToken) {
getPullRequest,
getPullRequestChecks,
waitForPullRequestChecks,
retryPullRequestCheck,
retryPullRequestChecks,

getDiffForPR,
getDiffForCommit,
Expand All @@ -623,6 +692,8 @@ function mod (githubContext, githubToken) {
createPullRequest,
createPullRequestReview,

mergePullRequest,

close,
comment,
updateComment,
Expand All @@ -637,7 +708,10 @@ function mod (githubContext, githubToken) {
repoURL,

checkRepoExists,
using
using,

_createPullRequestCURL,
_sendWorkflowDispatchCURL
}
}
module.exports = mod
20 changes: 15 additions & 5 deletions src/index.d.ts
Expand Up @@ -17,13 +17,13 @@ type FullPRData = {
headBranch: string;
headRepo: string;
headCloneURL: string;
title?: string;
body?: string;
state?: string;
number?: number;
title: string;
body: string;
state: string;
number: number;
author: string;
created: string;
url?: string;
url: string;
comments?: Comment[]
};

Expand Down Expand Up @@ -54,6 +54,10 @@ type PRCheck = {
| "action_required"
| null,
url: string
startedAt?: string
completedAt?: string
output?: string
id: number
}

interface GithubHelper {
Expand Down Expand Up @@ -104,6 +108,10 @@ interface GithubHelper {
getPullRequestChecks(id: number): Promise<PRCheck[]>
// Waits for all of the PR checks to be complete (upto specified timeout), then return all the checks
waitForPullRequestChecks(id: number, timeout: number): Promise<PRCheck[]>
// Re-run check on a PR
retryPullRequestChecks(checkId: number): Promise<void>
// Re-run failed checks on a PR
retryPullRequestChecks(prNumber: number): Promise<void>

updatePull(id: number, payload: { title?: string; body?: string }): Promise<void>;
createPullRequest(title: string, body: string, fromBranch: string, intoBranch?: string): Promise<{ number: number, url: string }>;
Expand All @@ -114,6 +122,8 @@ interface GithubHelper {
comments?: object[]
}): Promise<void>;

mergePullRequest(id: number, payload: { method?: "merge" | "squash" | "rebase"; title?: string; message?: string }): Promise<void>;

close(id: number, reason?: string): Promise<void>;

// Comment on an issue or PR
Expand Down
4 changes: 4 additions & 0 deletions src/mock.js
Expand Up @@ -92,11 +92,15 @@ const mock = {
getPullRequest,
getPullRequestChecks: () => [],
waitForPullRequestChecks: noop,
retryPullRequestCheck: noop,
retryPullRequestChecks: noop,

updatePull: noop,
createPullRequest: () => ({ number: 420, url: 'http://example.url/420' }),
createPullRequestReview: noop,

mergePullRequest: noop,

close: console.log,
comment: console.log,
updateComment: console.log,
Expand Down

0 comments on commit 2d00e80

Please sign in to comment.