Skip to content

Commit

Permalink
Update required fields and add branch push logging
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
  • Loading branch information
fjogeleit committed Dec 7, 2022
1 parent 7600af2 commit afa4ad4
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 47 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ jobs:
|Argument | Description | Default |
|------------|---------------------------------------------------------------------------------|---------------------|
|valueFile | relative path from the Workspace Directory | _required_ Field |
|propertyPath| PropertyPath for the new value, JSONPath supported | _required_ Field |
|value | New value for the related PropertyPath | _required_ Field |
|valueFile | relative path from the Workspace Directory | _required_ Field if `changes` is not used |
|propertyPath| PropertyPath for the new value, JSONPath supported | _required_ Field if `changes` is not used |
|value | New value for the related PropertyPath | _required_ Field if `changes` is not used |
|changes | Configure changes on multiple values and/or multiple files. Expects all changes as JSON, supported formats are `{"filepath":{"propertyPath":"value"}}` and `{"propertyPath":"value"}`. If you use the second format, it uses the filepath provided from the `valueFile` intput. ||
|labels | Comma separated list of labels, e.g. "feature, yaml-updates" | 'yaml-updates' |
|updateFile | **(deprected)** the updated content will be written into the actual file by default | `false` |
|workDir | Relative location of the configured `repository` | . | |
|format | Specify the used format parser of your file. WIll be guessed by file extension if not provided and uses YAML as fallback. Supports `YAML` and `JSON` ||
Expand All @@ -111,6 +110,7 @@ Determine the behavior for none existing properties or array elements.
|----------------|-------------------------------------------------------------------------------------------------------------|------------------------|
|commitChange | Commit the change to __branch__ with the given __message__ | `true` |
|message | Commit message for the changed YAML file | '' |
|labels | Comma separated list of labels, e.g. "feature, yaml-updates" | 'yaml-updates' |
|createPR | Create a PR from __branch__ to __targetBranch__. Use 'true' to enable it | `true` |
|title | Custom title for the created Pull Request | 'Merge: {{message}}' |
|description | Custom description for the created Pull Request | '' |
Expand Down
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ description: 'Update the property of an existing YAML File'
author: 'Frank Jogeleit <frank.jogeleit@web.de>'
inputs:
valueFile:
required: true
required: false
description: 'YAML file which should be updated'
propertyPath:
required: true
required: false
description: 'Property Path - valid jsonPath expression'
value:
required: true
required: false
description: 'New property value'
noCompatMode:
required: false
Expand Down
55 changes: 34 additions & 21 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "typescript-action",
"version": "0.8.0",
"name": "yaml-update-action",
"version": "0.12.2",
"private": true,
"description": "TypeScript template action",
"description": "Update YAML property with dynamic values",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
Expand Down
2 changes: 1 addition & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function gitProcessing(
actions.debug(JSON.stringify({createdCommit: newCommitSha}))
actions.setOutput('commit', newCommitSha)

await updateBranch(octokit, owner, repo, branch, newCommitSha)
await updateBranch(octokit, owner, repo, branch, newCommitSha, actions)

actions.debug(`Complete`)
}
Expand Down
28 changes: 20 additions & 8 deletions src/git-commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Octokit} from '@octokit/rest'
import {Actions} from './github-actions'
import {Committer, ChangedFile} from './types'

export type GitCreateTreeParamsTree = {
Expand Down Expand Up @@ -116,21 +117,32 @@ export const createNewCommit = async (
return data.sha
}

export const updateBranch = async (octo: Octokit, owner: string, repo: string, branch: string, commitSha: string): Promise<void> => {
export const updateBranch = async (
octo: Octokit,
owner: string,
repo: string,
branch: string,
commitSha: string,
actions: Actions
): Promise<void> => {
try {
await octo.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: commitSha
})
} catch (e) {
await octo.git.createRef({
owner,
repo,
ref: `refs/heads/${branch}`,
sha: commitSha
})
} catch (error) {
actions.info(`update branch ${branch} failed (${error}), fallback to create branch`)

await octo.git
.createRef({
owner,
repo,
ref: `refs/heads/${branch}`,
sha: commitSha
})
.catch(e => actions.setFailed(`failed to create branch: ${e}`))
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class GitHubOptions implements Options {
}

get changes(): Changes {
const changes: Changes = {}
let changes: Changes = {}
if (this.valueFile && this.propertyPath) {
let value: string | number | boolean = this.value

Expand All @@ -163,7 +163,12 @@ export class GitHubOptions implements Options {
}
}

return parseChanges(changes, this.valueFile, core.getInput('changes'))
changes = parseChanges(changes, this.valueFile, core.getInput('changes'))
if (Object.keys(changes).length === 0) {
core.setFailed('No changes to update detected')
}

return changes
}

get method(): Method {
Expand Down

0 comments on commit afa4ad4

Please sign in to comment.