Skip to content

Instantly share code, notes, and snippets.

@nwtgck
Created March 6, 2020 10:48
Show Gist options
  • Save nwtgck/c8accac2bcd9939a6604a3500247012d to your computer and use it in GitHub Desktop.
Save nwtgck/c8accac2bcd9939a6604a3500247012d to your computer and use it in GitHub Desktop.
(async () => {
// Get pull-req URL like "https://api.github.com/repos/nwtgck/actions-merge-preview/pulls/4"
const pullReqUrl = context.payload.issue.pull_request.url;
const githubUser = context.payload.repository.owner.login;
console.log({ githubUser });
const res = await fetch(pullReqUrl, {
headers: [
['Authorization', `Basic ${Buffer.from(`${githubUser}:${githubToken}`).toString('base64')}`]
]
});
const resJson = await res.json();
const prUserName = resJson.head.user.login;
const baseBranchName = resJson.base.ref;
const branchName = resJson.head.ref;
const fullRepoName = resJson.head.repo.full_name;
const previewBranchName = `actions-merge-preview/${prUserName}-${branchName}`;
execSync(`git config --global user.email "[email protected]"`);
execSync(`git config --global user.name "Bee Bot"`);
// (from: https://stackoverflow.com/a/23987039/2885946)
execSync(`git fetch --all`);
console.log(execSync(`git checkout ${baseBranchName}`).toString());
console.log(execSync(`git checkout -b ${previewBranchName} ${baseBranchName}`).toString());
console.log(execSync(`git pull https://github.com/${fullRepoName}.git ${branchName}`).toString());
// Push preview branch
// NOTE: Force push (should be safe because preview branch always start with "actions-merge-preview/")
execSync(`git push -fu origin ${previewBranchName}`);
const baseRepoFullName = context.payload.repository.full_name;
// Create GitHub client
const githubClient = new GitHub(githubToken);
// Comment body
const commentBody = `🚀 Preview branch: \n<https://github.com/${baseRepoFullName}/tree/${previewBranchName}>`;
// Comment the deploy URL
await githubClient.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment