Skip to content

Commit

Permalink
Introduce two new members related flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ukupat committed Jul 16, 2024
1 parent 6b943c7 commit 9ab053d
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 130 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/trello.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Trello integration
on:
pull_request:
types:
[
opened,
edited,
closed,
reopened,
ready_for_review,
converted_to_draft,
review_requested,
review_request_removed,
]
pull_request_review:
issue_comment:
types: [created, edited]
jobs:
trello:
runs-on: ubuntu-latest
steps:
- uses: rematocorp/trello-integration-action@114-disabling-member-assigning-completely
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
trello-api-key: ${{ secrets.TRELLO_API_KEY }}
trello-auth-token: ${{ secrets.TRELLO_AUTH_TOKEN }}
trello-list-id-pr-open: 66963abac807711874026524
trello-list-id-pr-closed: 66963ac72b1222986bf432a2
trello-switch-members-in-review: true
121 changes: 84 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,22 @@ jobs:

## All options

### 1. `trello-api-key` & `trello-auth-token`
<details>
<summary>Tokens</summary>

Required: Visit https://trello.com/app-key
#### 1. `github-token`

**Required**: Needed for Octokit API.

Example:

```yaml
github-token: ${{ secrets.GITHUB_TOKEN }}
```
#### 2. `trello-api-key` & `trello-auth-token`

**Required**: Needed for Trello API, visit https://trello.com/app-key.

Example:

Expand All @@ -54,123 +67,150 @@ trello-api-key: ${{ secrets.TRELLO_API_KEY }}
trello-auth-token: ${{ secrets.TRELLO_AUTH_TOKEN }}
```

### 2. `github-include-pr-comments`
</details>

<details>
<summary>Finding Trello cards</summary>

#### 1. `github-include-pr-comments`

Scans PR comments to find Trello card URLs.

Default: `true`

### 3. `github-include-pr-commit-messages`
#### 2. `github-include-pr-commit-messages`

Scans PR commit messages to find Trello card URLs and comments card URL to the PR if found.

Default: `false`

### 4. `github-include-pr-branch-name`
#### 3. `github-include-pr-branch-name`

Uses the branch name to find card id (e.g. feature/38-card-title) and comments card URL to the PR if found.

NB! Make sure you set `pull-requests: write` permission for the job. [Learn more.](https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs)

Default: `false`

### 5. `github-allow-multiple-cards-in-pr-branch-name`
#### 4. `github-allow-multiple-cards-in-pr-branch-name`

Allows to reference multiple card short IDs in the branch name (e.g. feature/38-39-40-foo-bar).

NB! Option `github-include-pr-branch-name` needs to be true and it is recommended to set `trello-board-id` to avoid moving wrong cards.

Default: `false`

### 6. `github-include-new-card-command`
#### 5. `github-include-new-card-command`

Creates a new Trello card from PR details if "/new-trello-card" is written in the PR description. Replaces "/new-trello-card" with the card link.

NB! Make sure you set `pull-requests: write` permission for the job. [Learn more.](https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs)

Default: `false`

### 7. `github-require-keyword-prefix`
#### 6. `github-require-keyword-prefix`

Only matches Trello URLs prefixed with "Closes" etc. Just like [GitHub Issues + PR work.](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword)

Default: `false`

### 8. `github-enable-related-keyword-prefix`
#### 7. `github-enable-related-keyword-prefix`

Ignores Trello URLs prefixed with "Related". Alternative approach when you don't want to use `github-require-keyword-prefix` but still want to link related cards for extra context.

Default: `false`

### 9. `github-require-trello-card`
#### 8. `github-require-trello-card`

Throws an error if no Trello card can be found in the PR.

Default: `false`

### 10. `github-users-to-trello-users`
</details>

Newline-separated list of mapping between GitHub username and Trello username. Use it for people who have different usernames in GitHub and Trello. If the current username is not in the list, we still try to find a Trello user with GitHub username.
<details>
<summary>Moving Trello cards</summary>

Example:

```yaml
github-users-to-trello-users: |-
GithubUser1:TrelloUser1
GithubUser2:TrelloUser2
```

### 11. `trello-remove-unrelated-members`

Removes card members who are not authors, contributors or assignees of the PR.

Default: `true`

### 12. `trello-list-id-pr-draft`
#### 1. `trello-list-id-pr-draft`

Trello list ID for draft pull request. [How to find list ID.](https://stackoverflow.com/a/50908600/2311110) Useful when you want to move the card back to "In progress" when ready PR is converted to draft.

### 13. `trello-list-id-pr-open`
#### 2. `trello-list-id-pr-open`

Trello list ID for open pull request. [How to find list ID.](https://stackoverflow.com/a/50908600/2311110)

### 14. `trello-list-id-pr-changes-requested`
#### 3. `trello-list-id-pr-changes-requested`

Trello list ID for a pull request which has at least one review requesting for changes. [How to find list ID.](https://stackoverflow.com/a/50908600/2311110)

NB! Add `pull_request_review` trigger and modify `pull_request` trigger with `review_requested` and `review_request_removed`. [Learn more.](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)

### 15. `trello-list-id-pr-approved`
#### 4. `trello-list-id-pr-approved`

Trello list ID for a pull request which has at least one approve and no reviews requesting for changes. [How to find list ID.](https://stackoverflow.com/a/50908600/2311110)

NB! Add `pull_request_review` trigger and modify `pull_request` trigger with `review_requested` and `review_request_removed`. [Learn more.](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)

### 16. `trello-list-id-pr-closed`
#### 5. `trello-list-id-pr-closed`

Trello list ID for closed pull request. [How to find list ID.](https://stackoverflow.com/a/50908600/2311110)

### 17. `trello-board-id`
#### 6. `trello-board-id`

Trello board ID where to move the cards. [How to find board ID.](https://stackoverflow.com/a/50908600/2311110) Useful when you want the action to move the card out from a backlog board.

Separate board IDs with a semicolon to support multiple boards. [Learn more.](https://github.com/rematocorp/trello-integration-action/issues/68)

### 18. `trello-archive-on-merge`
#### 7. `trello-archive-on-merge`

Archives Trello cards when PR is merged.

Default: `false`

### 19. `trello-card-position`
#### 8. `trello-card-position`

Position of the card after being moved to a list.

Options: `'top' | 'bottom'`

Default: `'top'`

### 20. `trello-organization-name`
</details>

<details>
<summary>Assigning members to Trello cards</summary>

#### 1. `github-users-to-trello-users`

Newline-separated list of mapping between GitHub username and Trello username. Use it for people who have different usernames in GitHub and Trello. If the current username is not in the list, we still try to find a Trello user with GitHub username.

Example:

```yaml
github-users-to-trello-users: |-
GithubUser1:TrelloUser1
GithubUser2:TrelloUser2
```

#### 2. `trello-add-members-to-cards`

Adds PR author, contributors and assignees to Trello card members list. This turns off also `trello-switch-members-in-review` and `trello-remove-unrelated-members`.

Default: `true`

#### 3. `trello-switch-members-in-review`

Replaces Trello card members with PR reviewers when PR is opened. It reassigns the PR author, contributors and assignees when the card is moved away from trello-list-id-pr-open.

Default: `false`

#### 4. `trello-remove-unrelated-members`

Removes card members who are not authors, contributors or assignees of the PR.

Default: `true`

#### 5. `trello-organization-name`

Your organization name to avoid assigning cards to outside members. Edit your workspace details and look for the short name.

Expand All @@ -180,13 +220,18 @@ Example:
trello-organization-name: remato
```

### 21. `trello-add-labels-to-cards`
</details>

<details>
<summary>Assigning labels to Trello cards</summary>

#### 1. `trello-add-labels-to-cards`

Assigns branch category (e.g. feature/foo) label to Trello card.

Default: `true`

### 22. `trello-conflicting-labels`
#### 2. `trello-conflicting-labels`

When a card already has one of these labels then branch category label is not assigned.

Expand All @@ -195,3 +240,5 @@ Example:
```yaml
trello-conflicting-labels: 'feature;bug;chore'
```

</details>
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ inputs:
trello-card-position:
description: Position of the card after being moved to a list. Can be "top" or "bottom".
default: 'top'
trello-add-members-to-cards:
description: Adds PR author, contributors and assignees to the Trello card.
default: true
trello-switch-members-in-review:
description: Replaces Trello card members with PR reviewers when PR is opened. It reassigns the PR author, contributors and assignees when the card is moved away from trello-list-id-pr-open.
default: false
trello-remove-unrelated-members:
description: Removes card members who are not authors or assignees of the PR.
default: true
Expand Down
4 changes: 2 additions & 2 deletions src/actions/getCardIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Conf, PR, PRHead } from '../types'
import { getBranchName, getCommits, getPullRequest, getPullRequestComments, updatePullRequestBody } from './api/github'
import { createCard, searchTrelloCards } from './api/trello'
import matchCardIds from './utils/matchCardIds'
import isDraftPullRequest from './utils/isDraftPullRequest'
import isPullRequestInDraft from './utils/isPullRequestInDraft'

export default async function getCardIds(conf: Conf, pr: PR) {
console.log('Searching for card IDs')
Expand Down Expand Up @@ -109,7 +109,7 @@ async function getTrelloCardByShortId(shortId: string, boardId?: string) {
}

async function createNewCard(conf: Conf, pr: PR) {
const isDraft = isDraftPullRequest(pr)
const isDraft = isPullRequestInDraft(pr)
const listId = pr.state === 'open' && isDraft ? conf.trelloListIdPrDraft : conf.trelloListIdPrOpen
const commandRegex = /(^|\s)\/new-trello-card(\s|$)/ // Avoids matching URLs

Expand Down
34 changes: 7 additions & 27 deletions src/actions/moveOrArchiveCards.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Conf, PR } from '../types'
import { isPullRequestMerged, getPullRequestReviews, getPullRequestRequestedReviewers } from './api/github'
import { isPullRequestMerged } from './api/github'
import { archiveCard, getBoardLists, getCardInfo, moveCardToList } from './api/trello'
import isDraftPullRequest from './utils/isDraftPullRequest'
import isChangesRequestedInReview from './utils/isChangesRequestedInReview'
import isPullRequestInDraft from './utils/isPullRequestInDraft'
import isPullRequestApproved from './utils/isPullRequestApproved'

export default async function moveOrArchiveCards(conf: Conf, cardIds: string[], pr: PR) {
const reviews = await getActivePullRequestReviews()
const isChangesRequested = reviews?.some((review) => review.state === 'CHANGES_REQUESTED')
const isApproved = reviews?.some((review) => review.state === 'APPROVED')
const isDraft = isDraftPullRequest(pr)
const isDraft = isPullRequestInDraft(pr)
const isChangesRequested = await isChangesRequestedInReview()
const isApproved = await isPullRequestApproved()
const isMerged = await isPullRequestMerged()

if (pr.state === 'open' && isDraft && conf.trelloListIdPrDraft) {
Expand Down Expand Up @@ -54,27 +55,6 @@ export default async function moveOrArchiveCards(conf: Conf, cardIds: string[],
console.log('Skipping moving and archiving the cards', { state: pr.state, isDraft, isMerged })
}

/**
* Returns all pull request reviews that are still relevant
*
* @returns https://docs.github.com/en/graphql/reference/objects#pullrequestreview
*/
async function getActivePullRequestReviews(): Promise<{ state: string }[]> {
const reviews = await getPullRequestReviews()
const requestedReviewers = await getPullRequestRequestedReviewers()

// Filters out pending reviews
const submittedReviews = reviews?.filter((review) => review.state !== 'PENDING')

// Filters in only the latest review per person
const latestReviews = Array.from(
submittedReviews?.reduce((map, review) => map.set(review.user?.id, review), new Map()).values() || [],
)

// Filters out reviews by people who have been re-requested for review
return latestReviews.filter((r) => !requestedReviewers?.users.some((u) => u.id === r.user?.id))
}

async function moveCardsToList(cardIds: string[], listId: string, boardId?: string) {
const listIds = listId.split(';')

Expand Down
Loading

0 comments on commit 9ab053d

Please sign in to comment.