diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d44d5bb7..27720b00 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,6 +42,7 @@ jobs: branch: test-create-new-pull-request commit-message: "Just testing [skip ci]" author: "J. Doe " + 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 }} diff --git a/README.md b/README.md index 6ffb5783..16748329 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ with: path: "lib/" commit-message: "My commit message for uncommitted changes in lib/ folder" author: "Lorem J. Ipsum " + labels: label1, label2 ``` To create multiple commits for different paths, use the action multiple times diff --git a/action.yml b/action.yml index 51585451..ff4055b4 100644 --- a/action.yml +++ b/action.yml @@ -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 ""` + labels: + description: Comma separated list of labels to apply to the pull request runs: using: "node12" main: "dist/index.js" diff --git a/index.js b/index.js index 059b68d4..ef472670 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,8 @@ async function main() { return; } + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); + try { const inputs = { title: core.getInput("title"), @@ -43,17 +45,20 @@ async function main() { 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}`); @@ -86,7 +91,7 @@ async function main() { await setGitUser({ name, - email + email, }); } @@ -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) { @@ -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); } } @@ -173,7 +200,7 @@ async function getLocalChanges(path) { return { hasUncommitedChanges, - hasChanges: hasUncommitedChanges + hasChanges: hasUncommitedChanges, }; } @@ -184,7 +211,7 @@ async function getGitUser() { return { name, - email + email, }; } catch (error) { return;