Skip to content

Commit 850c6ae

Browse files
authored
feat: labels option (#211)
1 parent 6f066da commit 850c6ae

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
branch: test-create-new-pull-request
4343
commit-message: "Just testing [skip ci]"
4444
author: "J. Doe <j@example.com>"
45+
labels: test1, test2
4546
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request"
4647
env:
4748
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ with:
3737
path: "lib/"
3838
commit-message: "My commit message for uncommitted changes in lib/ folder"
3939
author: "Lorem J. Ipsum <lorem@example.com>"
40+
labels: label1, label2
4041
```
4142
4243
To create multiple commits for different paths, use the action multiple times

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ inputs:
2929
default: Update from 'Create or Update Request' action
3030
path:
3131
description: Commit selected files only by providing a path. It is used in `git add "<path>"`
32+
labels:
33+
description: Comma separated list of labels to apply to the pull request
3234
runs:
3335
using: "node12"
3436
main: "dist/index.js"

index.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,29 @@ async function main() {
3636
return;
3737
}
3838

39+
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
40+
3941
try {
4042
const inputs = {
4143
title: core.getInput("title"),
4244
body: core.getInput("body"),
4345
branch: core.getInput("branch").replace(/^refs\/heads\//, ""),
4446
path: core.getInput("path"),
4547
commitMessage: core.getInput("commit-message"),
46-
author: core.getInput("author")
48+
author: core.getInput("author"),
49+
labels: core.getInput("labels"),
4750
};
4851

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

5154
const {
52-
data: { default_branch }
53-
} = await request(`GET /repos/${process.env.GITHUB_REPOSITORY}`, {
55+
data: { default_branch },
56+
} = await request(`GET /repos/{owner}/{repo}`, {
5457
headers: {
55-
authorization: `token ${process.env.GITHUB_TOKEN}`
56-
}
58+
authorization: `token ${process.env.GITHUB_TOKEN}`,
59+
},
60+
owner,
61+
repo,
5762
});
5863
const DEFAULT_BRANCH = default_branch;
5964
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);
@@ -86,7 +91,7 @@ async function main() {
8691

8792
await setGitUser({
8893
name,
89-
email
94+
email,
9095
});
9196
}
9297

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

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

142147
core.debug(`Creating pull request`);
143148
const {
144-
data: { html_url }
145-
} = await request(`POST /repos/${process.env.GITHUB_REPOSITORY}/pulls`, {
149+
data: { html_url, number },
150+
} = await request(`POST /repos/{owner}/{repo}/pulls`, {
146151
headers: {
147-
authorization: `token ${process.env.GITHUB_TOKEN}`
152+
authorization: `token ${process.env.GITHUB_TOKEN}`,
148153
},
154+
owner,
155+
repo,
149156
title: inputs.title,
150157
body: inputs.body,
151158
head: inputs.branch,
152-
base: DEFAULT_BRANCH
159+
base: DEFAULT_BRANCH,
153160
});
154161

155-
core.info(`Pull request created: ${html_url}`);
162+
core.info(`Pull request created: ${html_url} (#${number})`);
163+
164+
if (inputs.labels) {
165+
core.debug(`Adding labels: ${inputs.labels}`);
166+
const labels = inputs.labels.trim().split(/\s*,\s*/);
167+
const { data } = await request(
168+
`POST /repos/{owner}/{repo}/issues/{issue_number}/labels`,
169+
{
170+
headers: {
171+
authorization: `token ${process.env.GITHUB_TOKEN}`,
172+
},
173+
owner,
174+
repo,
175+
issue_number: number,
176+
labels,
177+
}
178+
);
179+
core.info(`Labels added: ${labels.join(", ")}`);
180+
core.debug(inspect(data));
181+
}
182+
156183
await runShellCommand(`git stash pop || true`);
157184
} catch (error) {
158-
core.debug(inspect(error));
185+
core.info(inspect(error));
159186
core.setFailed(error.message);
160187
}
161188
}
@@ -173,7 +200,7 @@ async function getLocalChanges(path) {
173200

174201
return {
175202
hasUncommitedChanges,
176-
hasChanges: hasUncommitedChanges
203+
hasChanges: hasUncommitedChanges,
177204
};
178205
}
179206

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

185212
return {
186213
name,
187-
email
214+
email,
188215
};
189216
} catch (error) {
190217
return;

0 commit comments

Comments
 (0)