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

feat: labels option #211

Merged
merged 9 commits into from
Sep 18, 2020
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
branch: test-create-new-pull-request
commit-message: "Just testing [skip ci]"
author: "J. Doe <[email protected]>"
labels: test1, test2
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ with:
path: "lib/"
commit-message: "My commit message for uncommitted changes in lib/ folder"
author: "Lorem J. Ipsum <[email protected]>"
labels: label1, label2
```

To create multiple commits for different paths, use the action multiple times
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ 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>"`
labels:
description: Comma separated list of labels to apply to the pull request
runs:
using: "node12"
main: "dist/index.js"
57 changes: 42 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@ async function main() {
return;
}

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"),
commitMessage: core.getInput("commit-message"),
author: core.getInput("author")
author: core.getInput("author"),
labels: core.getInput("labels"),
};

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

const {
data: { default_branch }
} = await request(`GET /repos/${process.env.GITHUB_REPOSITORY}`, {
data: { default_branch },
} = await request(`GET /repos/{owner}/{repo}`, {
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
}
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
owner,
repo,
});
const DEFAULT_BRANCH = default_branch;
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);
Expand Down Expand Up @@ -86,7 +91,7 @@ async function main() {

await setGitUser({
name,
email
email,
});
}

Expand Down Expand Up @@ -128,7 +133,7 @@ async function main() {
if (remoteBranchExists) {
const q = `head:${inputs.branch} type:pr is:open repo:${process.env.GITHUB_REPOSITORY}`;
const { data } = await request("GET /search/issues", {
q
q,
});

if (data.total_count > 0) {
Expand All @@ -141,21 +146,43 @@ async function main() {

core.debug(`Creating pull request`);
const {
data: { html_url }
} = await request(`POST /repos/${process.env.GITHUB_REPOSITORY}/pulls`, {
data: { html_url, number },
} = await request(`POST /repos/{owner}/{repo}/pulls`, {
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
owner,
repo,
title: inputs.title,
body: inputs.body,
head: inputs.branch,
base: DEFAULT_BRANCH
base: DEFAULT_BRANCH,
});

core.info(`Pull request created: ${html_url}`);
core.info(`Pull request created: ${html_url} (#${number})`);

if (inputs.labels) {
core.debug(`Adding labels: ${inputs.labels}`);
const labels = inputs.labels.trim().split(/\s*,\s*/);
const { data } = await request(
`POST /repos/{owner}/{repo}/issues/{issue_number}/labels`,
{
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
owner,
repo,
issue_number: number,
labels,
}
);
core.info(`Labels added: ${labels.join(", ")}`);
core.debug(inspect(data));
}

await runShellCommand(`git stash pop || true`);
} catch (error) {
core.debug(inspect(error));
core.info(inspect(error));
core.setFailed(error.message);
}
}
Expand All @@ -173,7 +200,7 @@ async function getLocalChanges(path) {

return {
hasUncommitedChanges,
hasChanges: hasUncommitedChanges
hasChanges: hasUncommitedChanges,
};
}

Expand All @@ -184,7 +211,7 @@ async function getGitUser() {

return {
name,
email
email,
};
} catch (error) {
return;
Expand Down