Skip to content

feat: add the ability to set labels on the created pull request #34

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

Open
wants to merge 1 commit into
base: main
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) <small> Is required with the below scope </small>

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)

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let PULL_REQUEST_LABELS = require( './variables' ).PULL_REQUEST_LABELS;
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;
Expand All @@ -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}` );
Expand Down Expand Up @@ -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)}`);
});
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -28,6 +29,7 @@ module.exports = {
GIT_URL,
RAW_REPOSITORIES,
PULL_REQUEST,
PULL_REQUEST_LABELS,
RAW_WORKFLOW_FILES,
WORKFLOW_FILES_DIR,
REPOSITORIES,
Expand Down