Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Add Reviewers #646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
happyPath:
name: "[TEST] Update existing pull request"
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v3
with:
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ inputs:
default: Update from 'Create or Update Request' action
path:
description: Commit selected files only by providing a path. It is used in `git add "<path>"`
path-to-cd-to:
description: Commit changes in another repository by providing a path to cd to (using Node's process.chdir function) before git operations.
required: false
labels:
description: Comma separated list of labels to apply to the pull request
required: false
assignees:
description: Comma separated list of assignees to apply to the pull request
required: false
repository:
description: Repository name in the format owner/repo. Defaults to current repository if not given. Use with path-to-cd-to.
required: false
reviewers:
description: Comma separated list of reviewers to apply to the pull request
required: false
Expand Down
46 changes: 28 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function main() {
if (!process.env.GITHUB_TOKEN) {
core.setFailed(
`GITHUB_TOKEN is not configured. Make sure you made it available to your action

uses: gr2m/create-or-update-pull-request-action@master
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}`
Expand All @@ -40,14 +40,15 @@ async function main() {
auth: process.env.GITHUB_TOKEN,
});

const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");

try {
const inputs = {
title: core.getInput("title"),
body: core.getInput("body"),
branch: core.getInput("branch").replace(/^refs\/heads\//, ""),
path: core.getInput("path"),
pathToCdTo: core.getInput("path-to-cd-to"),
repository: core.getInput("repository"),
commitMessage: core.getInput("commit-message"),
author: core.getInput("author"),
labels: core.getInput("labels"),
Expand All @@ -60,6 +61,11 @@ async function main() {

core.debug(`Inputs: ${inspect(inputs)}`);

let [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
if (inputs.repository) {
[owner, repo] = inputs.repository.split("/");
}

if (
inputs.autoMerge &&
!["merge", "squash", "rebase"].includes(inputs.autoMerge)
Expand All @@ -79,6 +85,11 @@ async function main() {
const DEFAULT_BRANCH = default_branch;
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);

if (inputs.pathToCdTo) {
core.debug(`Changing directory to ${inputs.pathToCdTo}`);
process.chdir(inputs.pathToCdTo);
}

const { hasChanges } = await getLocalChanges(inputs.path);

if (!hasChanges) {
Expand All @@ -87,7 +98,6 @@ async function main() {
} else {
core.info("No local changes");
}

core.setOutput("result", "unchanged");
process.exit(0); // there is currently no neutral exit code
}
Expand Down Expand Up @@ -134,22 +144,22 @@ async function main() {
} else {
core.debug(`rebase all local changes on base branch`);
await runShellCommand(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${owner}/${repo}.git ${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`
);
await runShellCommand(`git stash --include-untracked`);
await runShellCommand(`git rebase -X theirs '${DEFAULT_BRANCH}'`);
}

core.debug(`Try to fetch and checkout remote branch "${inputs.branch}"`);
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch, owner, repo);

core.debug(`Pushing local changes`);
await runShellCommand(
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${owner}/${repo}.git HEAD:refs/heads/${inputs.branch}`
);

if (remoteBranchExists) {
const q = `head:${inputs.branch} type:pr is:open repo:${process.env.GITHUB_REPOSITORY}`;
const q = `head:${inputs.branch} type:pr is:open repo:${owner}/${repo}`;
const { data } = await octokit.request("GET /search/issues", {
q,
});
Expand Down Expand Up @@ -223,7 +233,7 @@ async function main() {
core.info(`Assignees added: ${assignees.join(", ")}`);
core.debug(inspect(data));
}

if (inputs.reviewers || inputs.team_reviewers) {
let params = {
owner,
Expand All @@ -233,8 +243,8 @@ async function main() {
let reviewers = null;
let team_reviewers = null;

if(inputs.reviewers) {
core.debug(`Adding reviewers: ${inputs.reviewers}`)
if (inputs.reviewers) {
core.debug(`Adding reviewers: ${inputs.reviewers}`)
reviewers = (inputs.reviewers || "").trim().split(/\s*,\s*/);

params = {
Expand All @@ -243,26 +253,26 @@ async function main() {
}
};

if(inputs.team_reviewers) {
core.debug(`Adding team reviewers: ${inputs.team_reviewers}`)
if (inputs.team_reviewers) {
core.debug(`Adding team reviewers: ${inputs.team_reviewers}`)
team_reviewers = (inputs.team_reviewers || "").trim().split(/\s*,\s*/);

params = {
...params,
team_reviewers
}
} ;
};

const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers`,
params
);

if(reviewers) {
if (reviewers) {
core.info(`Reviewers added: ${reviewers.join(", ")}`);
}

if(team_reviewers) {
if (team_reviewers) {
core.info(`Team reviewers added: ${team_reviewers.join(", ")}`);
}

Expand Down Expand Up @@ -339,7 +349,7 @@ async function setGitUser({ name, email }) {
await runShellCommand(`git config --global user.email '${email}'`);
}

async function checkOutRemoteBranch(branch) {
async function checkOutRemoteBranch(branch, owner, repo) {
try {
const currentBranch = await runShellCommand(
`git rev-parse --abbrev-ref HEAD`
Expand All @@ -352,7 +362,7 @@ async function checkOutRemoteBranch(branch) {

core.debug(`fetching "${branch}" branch from remote`);
await runShellCommand(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${branch}:${branch}`
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${owner}/${repo}.git ${branch}:${branch}`
);

await runShellCommand(`git branch`);
Expand All @@ -374,7 +384,7 @@ async function checkOutRemoteBranch(branch) {

return true;
} catch (error) {
core.info(`Branch "${branch}" does not yet exist on remote.`);
core.info(`Branch "${branch}" does not yet exist on remote. error: ${error}`);
await runShellCommand(`git checkout -b ${branch}`);
return false;
}
Expand Down
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wed Dec 6 00:26:14 UTC 2023