generated from Bullrich/parity-action-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created base action with the ability to update PRs and print a summary of the results. Resolves #2
- Loading branch information
Showing
7 changed files
with
168 additions
and
28 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Up to date | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
updatePullRequests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Update all the PRs | ||
uses: paritytech/up-to-date-action@main | ||
with: | ||
GITHUB_TOKEN: ${{ github.token }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,76 @@ | ||
# Parity GitHub Action template | ||
# Up to date action | ||
|
||
Template used to generate GitHub Actions. | ||
Keep your pull request up to date when the target branch changes. | ||
|
||
## To start | ||
## Why? | ||
|
||
- Remember to modify the `action.yml` file to have your required attributes and details. | ||
- You can use [GitHub Action Brandings cheatsheet](https://github.com/haya14busa/github-action-brandings) to set the style of the action. | ||
- Remember to modify the name in the `package.json`. | ||
This action was created on the need to keep all the PRs up to date, so they would always be tested on latest. | ||
|
||
When a repo has too many PRs, it can became a bit troublesome to keep all the PRs up to date. | ||
|
||
It basically does the same thing as pressing the following button: | ||
|
||
![update-button](./.github/update-branch.png) | ||
|
||
## Configuration | ||
|
||
```yml | ||
name: Up to date | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
updatePullRequests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Update all the PRs | ||
uses: paritytech/up-to-date-action@main | ||
with: | ||
GITHUB_TOKEN: ${{ secret.PAT }} | ||
``` | ||
### Inputs | ||
#### `GITHUB_TOKEN` | ||
- Required | ||
- Has to be a [Personal Access Token](https://github.com/settings/tokens/) with `repo` permissions. | ||
- It can not be GitHub's action token because a push made by an action secret does not trigger new actions (and the new tests would not be triggered) | ||
- [Related reading](https://github.com/orgs/community/discussions/25702#discussioncomment-3248819) | ||
|
||
##### Using a GitHub app instead of a PAT | ||
In some cases, specially in big organizations, it is more organized to use a GitHub app to authenticate, as it allows us to give it permissions per repository and we can fine-grain them even better. If you wish to do that, you need to create a GitHub app with the following permissions: | ||
- Repository permissions: | ||
- Pull Requests | ||
- [x] Write | ||
- Contents | ||
- [x] Write | ||
|
||
Because this project is intended to be used with a token we need to do an extra step to generate one from the GitHub app: | ||
- After you create the app, copy the *App ID* and the *private key* and set them as secrets. | ||
- Then you need to modify the workflow file to have an extra step: | ||
```yml | ||
steps: | ||
- name: Generate token | ||
id: generate_token | ||
uses: tibdex/github-app-token@v1 | ||
with: | ||
app_id: ${{ secrets.APP_ID }} | ||
private_key: ${{ secrets.PRIVATE_KEY }} | ||
- name: Update all the PRs | ||
uses: paritytech/up-to-date-action@main | ||
with: | ||
# The previous step generates a token which is used as the input for this action | ||
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} | ||
``` | ||
|
||
## Development | ||
To work on this app, you require | ||
- `Node 18.x` | ||
- `yarn` | ||
|
||
Use `yarn install` to set up the project. | ||
|
||
`yarn build` compiles the TypeScript code to JavaScript. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
import { PullRequest } from "@octokit/webhooks-types"; | ||
|
||
import { ActionLogger, GitHubClient } from "./types"; | ||
import { GitHubClient } from "./types"; | ||
|
||
/** API class that uses the default token to access the data from the pull request and the repository */ | ||
export class PullRequestApi { | ||
constructor( | ||
private readonly api: GitHubClient, | ||
private readonly logger: ActionLogger, | ||
private readonly repo: { owner: string; repo: string }, | ||
) {} | ||
|
||
getPrAuthor(pr: PullRequest): string { | ||
return pr.user.login; | ||
async listPRs( | ||
onlyAutoMerge: boolean, | ||
): Promise<{ number: number; title: string }[]> { | ||
const openPRs = await this.api.paginate(this.api.rest.pulls.list, { | ||
...this.repo, | ||
state: "open", | ||
}); | ||
|
||
if (onlyAutoMerge) { | ||
return openPRs | ||
.filter((pr) => pr.auto_merge) | ||
.map(({ number, title }) => ({ number, title }) as const); | ||
} else { | ||
return openPRs.map(({ number, title }) => ({ number, title }) as const); | ||
} | ||
} | ||
|
||
async update(number: number): Promise<string | undefined> { | ||
const { data } = await this.api.rest.pulls.updateBranch({ | ||
...this.repo, | ||
pull_number: number, | ||
}); | ||
|
||
return data.message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters