Skip to content

Commit

Permalink
Feature: Add ability to configure triage-level-access token
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceArikoglu committed Jan 8, 2023
1 parent 25e0948 commit adffed6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,28 @@ jobs:
steps:
- name: Lint Pull Request
uses: reaction-link/actions-lint-pull-request@v0
uses: reaction-link/actions-lint-pull-request@v2
with:
triage-pr-token: ${{secrets.OUR_BOT_REPO_SCOPED_TOKEN}} // GITHUB_TOKEN does not work, is required for deleting labels
config-bot-repotoken: ${{secrets.OUR_BOT_REPO_SCOPED_TOKEN}} // GITHUB_TOKEN also works
config-bot-login: reactionlinkbot // login for GITHUB_TOKEN is github_actions
config-bot-login: mybot // login for GITHUB_TOKEN is github-actions[bot]
github-event: ${{toJson(github.event)}}
```

### Available inputs

```
inputs:
config-bot-repotoken:
description: "Token with repo-scope permissions for a GitHub user (bot account for example)"
access-token: /** v2 */
description: "Token with triage permissions or GITHUB_TOKEN"
required: true
config-bot-login:
token-login: /** v2 */
description: "Username of the GitHub user"
required: true
config-bot-repotoken: /** v1 */
description: "Token with permissions to comment on a Pull Request (Github Token for example)"
required: true
config-bot-login: /** v1 */
description: "Username of the GitHub user (bot account for example)"
required: true
github-event:
Expand Down
10 changes: 5 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: "Lint Pull Request"
description: "Lint the pull request structure and leave a comment suggesting changes if linting fails"
author: 'reaction.link'
author: 'Maurice Arikoglu (reactionlink.de)'

branding:
icon: git-pull-request
color: blue

inputs:
config-bot-repotoken:
description: "Token with repo-scope permissions for a GitHub user (bot account for example)"
access-token:
description: "Token with triage permissions or GITHUB_TOKEN"
required: true
config-bot-login:
description: "Username of the GitHub user (bot account for example)"
token-login:
description: "Username of the GitHub user"
required: true
github-event:
description: "toJSON of github.event (event triggering the action)"
Expand Down
24 changes: 17 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ function data(
greetings: JSON.parse(useGreetingsInput),
approvalLabels: JSON.parse(useApprovalLabelsInput),
titleRegex: new RegExp(useTitleRegexInput, useTitleRegexFlagInput),
descriptionRegex: new RegExp(useDescriptionRegexInput, useDescriptionRegexFlagInput),
descriptionRegex: new RegExp(
useDescriptionRegexInput,
useDescriptionRegexFlagInput
),
useProblemTitle: useProblemTitleInput,
useProblemDescription: useProblemDescriptionInput,
useExplanationTitle: JSON.parse(useExplanationTitleInput),
Expand Down Expand Up @@ -86,8 +89,8 @@ __nccwpck_require__.r(__webpack_exports__);
/* harmony export */ });
const githubEvent = 'github-event';

const configBotRepoToken = 'config-bot-repotoken';
const configBotLogin = 'config-bot-login';
const configBotRepoToken = 'access-token';
const configBotLogin = 'token-login';

const useGreetings = 'use-greetings';
const useApprovalLabels = 'use-approval-labels';
Expand Down Expand Up @@ -167,7 +170,7 @@ __nccwpck_require__.r(__webpack_exports__);
/* harmony export */ "updateIssueComment": () => (/* binding */ updateIssueComment),
/* harmony export */ "deleteIssueComment": () => (/* binding */ deleteIssueComment)
/* harmony export */ });
async function getCommentId(octokit, eventData, actionData) {
async function getCommentId(octokit, eventData, actionData, commentPrefix) {
const response = await octokit.request(
'GET /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
Expand All @@ -179,7 +182,11 @@ async function getCommentId(octokit, eventData, actionData) {

// Looking for previous comment
const comment = response['data'].find((comment) => {
return comment['user']['login'] === actionData.botLogin;
console.log(comment);
return (
comment['user']['login'] === actionData.botLogin &&
comment['body'].startsWith(commentPrefix)
);
});

return comment && comment['id'];
Expand Down Expand Up @@ -8796,6 +8803,7 @@ const {
deleteIssueComment,
} = __nccwpck_require__(2754);
const { greetings, explainProblems } = __nccwpck_require__(3171);
const commentPrefix = '**Automated Response: [Lint Pull Request](https://github.com/reaction-link/actions-lint-pull-request)**';

function lintPullRequestEvent(actionData, eventData) {
const problemsFound = [];
Expand Down Expand Up @@ -8826,6 +8834,8 @@ function lintPullRequestEvent(actionData, eventData) {

function getBody(actionData, eventData, problemsFound) {
return [
commentPrefix,
'---',
greetings(actionData.greetings, eventData.pullRequestUserLogin),
explainProblems(problemsFound.length),
'\n**Problems**:\n',
Expand Down Expand Up @@ -8859,7 +8869,7 @@ async function run() {

const commentId =
event.pullRequestCommentCount > 0
? await getCommentId(octokit, event, action)
? await getCommentId(octokit, event, action, commentPrefix)
: null;

if (success) {
Expand Down Expand Up @@ -8897,7 +8907,7 @@ async function run() {
// Remove Labels displaying successful lint
for (const label of action.approvalLabels) {
console.log('Removing:', label);
if (event.pullRequestLabels.includes(label)) {
if (event.pullRequestLabels.map((l) => l.name).includes(label)) {
await deletePRLabel(octokit, event, label);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const githubEvent = 'github-event';

const configBotRepoToken = 'config-bot-repotoken';
const configBotLogin = 'config-bot-login';
const configBotRepoToken = 'access-token';
const configBotLogin = 'token-login';

const useGreetings = 'use-greetings';
const useApprovalLabels = 'use-approval-labels';
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/octokit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function getCommentId(octokit, eventData, actionData) {
async function getCommentId(octokit, eventData, actionData, commentPrefix) {
const response = await octokit.request(
'GET /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
Expand All @@ -10,7 +10,11 @@ async function getCommentId(octokit, eventData, actionData) {

// Looking for previous comment
const comment = response['data'].find((comment) => {
return comment['user']['login'] === actionData.botLogin;
console.log(comment);
return (
comment['user']['login'] === actionData.botLogin &&
comment['body'].startsWith(commentPrefix)
);
});

return comment && comment['id'];
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
deleteIssueComment,
} = require('./helpers/octokit');
const { greetings, explainProblems } = require('./helpers/rules');
const commentPrefix =
'**Automated Response: [Lint Pull Request](https://github.com/reaction-link/actions-lint-pull-request)**';

function lintPullRequestEvent(actionData, eventData) {
const problemsFound = [];
Expand Down Expand Up @@ -41,6 +43,8 @@ function lintPullRequestEvent(actionData, eventData) {

function getBody(actionData, eventData, problemsFound) {
return [
commentPrefix,
'---',
greetings(actionData.greetings, eventData.pullRequestUserLogin),
explainProblems(problemsFound.length),
'\n**Problems**:\n',
Expand Down Expand Up @@ -74,7 +78,7 @@ async function run() {

const commentId =
event.pullRequestCommentCount > 0
? await getCommentId(octokit, event, action)
? await getCommentId(octokit, event, action, commentPrefix)
: null;

if (success) {
Expand Down Expand Up @@ -112,7 +116,7 @@ async function run() {
// Remove Labels displaying successful lint
for (const label of action.approvalLabels) {
console.log('Removing:', label);
if (event.pullRequestLabels.includes(label)) {
if (event.pullRequestLabels.map((l) => l.name).includes(label)) {
await deletePRLabel(octokit, event, label);
}
}
Expand Down

0 comments on commit adffed6

Please sign in to comment.