diff --git a/README.md b/README.md index d2152ab..a036f28 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,30 @@ This also isn't limited to Github Action yaml files - another use case could be | `AUTO_CREATE_NEW_BRANCH` | ***false*** | Auto create new brach in a repository if the branch dose not exists | | `COMMIT_EACH_FILE` | ***false*** | if you need to keep track of each file's commit history separate then set it to true | | `PULL_REQUEST` | **false** | Set to `true` if you want the changes to be pushed via pull request. | +| `PULL_REQUEST_LABELS` | - | Labels to apply to the created pull request. Separate multiple values using a comma. | | `SKIP_CI` | **false** | Set to `true` if you want skip all automations inside target repository. | | `COMMIT_MESSAGE` | **false** | You can provide your custom commit message. | | `RETRY_MODE` | **true** | Enable retry and throttling octokit plugins to avoid secondary rate limits on github content creation. | ### Personal Access Token Scope -#### [Github Personal Token](https://github.com/settings/tokens/new?description=gh-workflow-sync) Is required with the below scope + +A personal access token is required to use this action. Create either a fine-grained or classic token with the following scopes: + +#### [Github Personal Token (fine-grained)](https://github.com/settings/personal-access-tokens/new) + +For public repositories, choose `Public repositories` for the repository access. + +For private repositories, choose either `All repositories` or `Only select repositories` with the following permissions: + +| Permission | Access | Notes | +|---------------|----------------|--------------------------------------------| +| Content | Read and write | | +| Issues | Read and write | Only required when adding labels to the PR | +| Metadata | Read-only | | +| Pull requests | Read and write | Only required when creating a PR | +| Workflows | Read and write | | + +#### [Github Personal Token (classic)](https://github.com/settings/tokens/new?description=gh-workflow-sync) ![https://cdn.svarun.dev/gh/varunsridharan/action-github-workflow-sync/scope.jpg](https://cdn.svarun.dev/gh/varunsridharan/action-github-workflow-sync/scope.jpg) diff --git a/action.yml b/action.yml index 9f4119d..70686b8 100644 --- a/action.yml +++ b/action.yml @@ -40,6 +40,9 @@ inputs: description: "Whether or not you want to do a pull request. Only works when branch name is provided. Default false" required: false default: 'false' + PULL_REQUEST_LABELS: + description: "Comma separated list of labels to add to the pull request. Default empty" + required: false SKIP_CI: description: "Adds [skip ci] to commit message which will avoid running github actions in targer repository" required: false diff --git a/src/index.js b/src/index.js index 9ab75ee..2b88567 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,7 @@ async function run() { let REPOSITORIES = require( './variables' ).REPOSITORIES; let WORKFLOW_FILES = require( './variables' ).WORKFLOW_FILES; let PULL_REQUEST = require( './variables' ).PULL_REQUEST; + let PULL_REQUEST_LABELS = require( './variables' ).PULL_REQUEST_LABELS; let SKIP_CI = require( './variables' ).SKIP_CI; let COMMIT_MESSAGE = require( './variables' ).COMMIT_MESSAGE; let RETRY_MODE = require( './variables' ).RETRY_MODE; @@ -28,6 +29,7 @@ async function run() { toolkit.log( ` * AUTO_CREATE_NEW_BRANCH : ${AUTO_CREATE_NEW_BRANCH}` ); toolkit.log( ` * COMMIT_EACH_FILE : ${COMMIT_EACH_FILE}` ); toolkit.log( ` * PULL_REQUEST : ${PULL_REQUEST}` ); + toolkit.log( ` * PULL_REQUEST_LABELS : ${PULL_REQUEST_LABELS}` ); toolkit.log( ` * DRY_RUN : ${DRY_RUN}` ); toolkit.log( ` * WORKFLOW_FILES_DIR : ${WORKFLOW_FILES_DIR}` ); toolkit.log( ` * WORKSPACE : ${WORKSPACE}` ); @@ -202,6 +204,14 @@ async function run() { if (pull_request_resp) { toolkit.log.green( `Pull Request Created : #${pull_request_resp.data.number}` ); toolkit.log( `${pull_request_resp.data.html_url}` ); + if (PULL_REQUEST_LABELS) { + toolkit.log(`Adding labels [${PULL_REQUEST_LABELS}] to pull request`); + await finalOctokit.request(`POST /repos/${owner}/${repository}/issues/${pull_request_resp.data.number}/labels`, { + labels: PULL_REQUEST_LABELS.split(',').map(label => label.trim()), + }).catch((error) => { + toolkit.log.error(`Error on adding labels to pull request: ${error.status}: ${JSON.stringify(error.response.data)}`); + }); + } } } diff --git a/src/variables.js b/src/variables.js index 5ba2f34..3dfc2d5 100644 --- a/src/variables.js +++ b/src/variables.js @@ -5,6 +5,7 @@ const AUTO_CREATE_NEW_BRANCH = toolkit.input.tobool( core.getInput( 'AUTO_CREATE const COMMIT_EACH_FILE = toolkit.input.tobool( core.getInput( 'COMMIT_EACH_FILE' ) ); const DRY_RUN = toolkit.input.tobool( core.getInput( 'DRY_RUN' ) ); const PULL_REQUEST = toolkit.input.tobool( core.getInput( 'PULL_REQUEST' ) ); +const PULL_REQUEST_LABELS = core.getInput( 'PULL_REQUEST_LABELS' ); const SKIP_CI = toolkit.input.tobool( core.getInput( 'SKIP_CI' ) ); const GITHUB_TOKEN = core.getInput( 'GITHUB_TOKEN' ); const GIT_URL = core.getInput( 'GIT_URL' ); @@ -28,6 +29,7 @@ module.exports = { GIT_URL, RAW_REPOSITORIES, PULL_REQUEST, + PULL_REQUEST_LABELS, RAW_WORKFLOW_FILES, WORKFLOW_FILES_DIR, REPOSITORIES,