Skip to content

Commit

Permalink
✨ Add COMMIT_AS_PR_TITLE option (#115)
Browse files Browse the repository at this point in the history
Fixes #103
  • Loading branch information
Nef10 authored Sep 27, 2021
1 parent 2816ce7 commit 8c50c0d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
#### Token
In order for the Action to access your repositories you have to specify a [Personal Access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token) as the value for `GH_PAT` (`GITHUB_TOKEN` will **not** work). The PAT needs the full repo scope ([#31](https://github.com/BetaHuhn/repo-file-sync-action/discussions/31#discussioncomment-674804)).

It is recommneded to set the token as a
It is recommended to set the token as a
[Repository Secret](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository).

Alternatively, you can provide the token of a GitHub App Installation via the `GH_INSTALLATION_TOKEN` input. You can obtain such token for example via [this](https://github.com/marketplace/actions/github-app-token) action. Tokens from apps have the advantage that they provide more granular access control.
Expand Down Expand Up @@ -108,7 +108,8 @@ Here are all the inputs [repo-file-sync-action](https://github.com/BetaHuhn/repo
| `ASSIGNEES` | People to assign to the pull request | **No** | N/A |
| `COMMIT_PREFIX` | Prefix for commit message and pull request title | **No** | 🔄 |
| `COMMIT_BODY` | Commit message body. Will be appended to commit message, separated by two line returns. | **No** | '' |
| `ORIGINAL_MESSAGE` | Use original commit message instead. Only works if the file(s) where changed and the action was triggered by pushing a single commit. | **No** | false |
| `ORIGINAL_MESSAGE` | Use original commit message instead. Only works if the file(s) were changed and the action was triggered by pushing a single commit. | **No** | false |
| `COMMIT_AS_PR_TITLE` | Use first line of the commit message as PR title. Only works if `ORIGINAL_MESSAGE` is `true` and working. | **No** | false |
| `COMMIT_EACH_FILE` | Commit each file seperately | **No** | true |
| `GIT_EMAIL` | The e-mail address used to commit the synced files | **Only when using installation token** | the email of the PAT used |
| `GIT_USERNAME` | The username used to commit the synced files | **Only when using installation token** | the username of the PAT used |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ inputs:
description: |
Re-use the original commit message for commits. Works only if the action is triggered by pushing one commit. Defaults to false
required: false
COMMIT_AS_PR_TITLE:
description: |
Re-use the commit message as PR title. Works only if ORIGINAL_MESSAGE is on and PR has one commit. Defaults to false
required: false
SKIP_PR:
description: |
Skips creating a Pull Request and pushes directly to the default branch. Defaults to false
Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ try {
type: 'boolean',
default: false
}),
COMMIT_AS_PR_TITLE: getInput({
key: 'COMMIT_AS_PR_TITLE',
type: 'boolean',
default: false
}),
BRANCH_PREFIX: getInput({
key: 'BRANCH_PREFIX',
default: 'repo-sync/SOURCE_REPO_NAME'
Expand Down
5 changes: 3 additions & 2 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class Git {
})
}

async createOrUpdatePr(changedFiles) {
async createOrUpdatePr(changedFiles, title) {
const body = dedent(`
Synced local file(s) with [${ GITHUB_REPOSITORY }](https://github.com/${ GITHUB_REPOSITORY }).
Expand All @@ -268,6 +268,7 @@ class Git {
const { data } = await this.github.pulls.update({
owner: this.repo.user,
repo: this.repo.name,
title: `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`,
pull_number: this.existingPr.number,
body: body
})
Expand All @@ -280,7 +281,7 @@ class Git {
const { data } = await this.github.pulls.create({
owner: this.repo.user,
repo: this.repo.name,
title: `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`,
title: title === undefined ? `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }` : title,
body: body,
head: this.prBranch,
base: this.baseBranch
Expand Down
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
SKIP_CLEANUP,
OVERWRITE_EXISTING_PR,
SKIP_PR,
ORIGINAL_MESSAGE
ORIGINAL_MESSAGE,
COMMIT_AS_PR_TITLE
} = require('./config')

const run = async () => {
Expand Down Expand Up @@ -101,7 +102,9 @@ const run = async () => {
modified.push({
dest: file.dest,
source: file.source,
message: message[destExists].pr
message: message[destExists].pr,
useOriginalMessage: useOriginalCommitMessage,
commitMessage: message[destExists].commit
})
}
})
Expand Down Expand Up @@ -137,9 +140,12 @@ const run = async () => {
})
}

await git.commit(useOriginalCommitMessage ? git.originalCommitMessage() : undefined)
const commitMessage = useOriginalCommitMessage ? git.originalCommitMessage() : undefined
await git.commit(commitMessage)
modified.push({
dest: git.workingDir
dest: git.workingDir,
useOriginalMessage: useOriginalCommitMessage,
commitMessage: commitMessage
})
}

Expand All @@ -157,7 +163,8 @@ const run = async () => {
</details>
`)

const pullRequest = await git.createOrUpdatePr(COMMIT_EACH_FILE ? changedFiles : '')
const useCommitAsPRTitle = COMMIT_AS_PR_TITLE && modified.length === 1 && modified[0].useOriginalMessage
const pullRequest = await git.createOrUpdatePr(COMMIT_EACH_FILE ? changedFiles : '', useCommitAsPRTitle ? modified[0].commitMessage.split('\n', 1)[0].trim() : undefined)

core.notice(`Pull Request #${ pullRequest.number } created/updated: ${ pullRequest.html_url }`)

Expand Down

0 comments on commit 8c50c0d

Please sign in to comment.