Skip to content

Commit

Permalink
Changelog and Labels (#13)
Browse files Browse the repository at this point in the history
- Enable option to Manage Changelog
- Enable setting Labels on PRs created
- Updated README.md with documentation for these new features

Signed-off-by: Jason Field <jason@avon-lea.co.uk>
  • Loading branch information
xorima committed Aug 17, 2021
1 parent 50ef4b4 commit cc8423b
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 218 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Enable option to Manage Changelog
- Enable setting Labels on PRs created

## 2.0.0 - *2021-08-11*

- Breaking Change: Defaults to looking for main branches, introduces new cli arg for setting default branch name
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Below are a list of variables, what they mean and example values
| GFM_PULL_REQUEST_LABELS | `String` | No | The labels to apply to the Pull Request, Takes a csv, eg: `tech-debt,automated` |
| GFM_GIT_NAME | `String` | No | The Name to use when creating the git commits |
| GFM_GIT_EMAIL | `String` | No | The E-mail address to use when creating the git commits |
| GFM_CHANGELOG_LOCATION | `String` | No | The location of the change log to update relative to the root of the repo |
| GFM_CHANGELOG_MARKER | `String` | No | The string to use as the update point in the changelog, if not found it will be added before the next subtitle of `##` |
| GFM_MANAGE_CHANGELOG | `String` | Yes | Should we be managing the changelog, set to `0` for no, `1` for yes |

| GFM_DEFAULT_GIT_BRANCH | `String` | No | The name of the default branch, if not set this will default to `main` |

## Git Authentication
Expand Down
30 changes: 28 additions & 2 deletions app/entrypoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ param (
[String]
$GitEmail = $ENV:GFM_GIT_EMAIL,
[String]
$ChangeLogLocation = $ENV:GFM_CHANGELOG_LOCATION,
[String]
$ChangeLogMarker = $ENV:GFM_CHANGELOG_MARKER,
[bool]
$ChangeLogIsManaged = [Int]$ENV:GFM_MANAGE_CHANGELOG,
[String]
$DefaultBranchName = $ENV:GFM_DEFAULT_GIT_BRANCH
)

Expand All @@ -58,6 +64,7 @@ try {
import-module ./app/modules/github
import-module ./app/modules/git
import-module ./app/modules/logging
import-module ./app/modules/changelog

}
catch {
Expand All @@ -77,6 +84,12 @@ if (!($DefaultBranchName)){
$DefaultBranchName = 'main'

}

if ($PullRequestLabels) {
$labelsArray = $PullRequestLabels.Split(',')
$manageLabels = $true
}

# Setup the git config first, if env vars are not supplied this will do nothing.
Set-GitConfig -gitName $GitName -gitEmail $GitEmail

Expand Down Expand Up @@ -193,18 +206,31 @@ catch {
# Commit the files that have changed
try {
Write-Log -Level INFO -Source 'entrypoint' -Message "Commiting standardised files and pushing to remote if changed"
New-CommitAndPushIfChanged -CommitMessage "Standardise files with files in $SourceRepoOwner/$SourceRepoName" -push
New-CommitAndPushIfChanged -CommitMessage "Standardise files with files in $SourceRepoOwner/$SourceRepoName" -push `
-ChangeLogIsManaged $ChangeLogIsManaged -ChangeLogMarker $ChangeLogMarker -ChangeLogLocation $ChangeLogLocation
}
catch {
Write-Log -Level ERROR -Source 'entrypoint' -Message "Unable to commit standardised files and push to remote if changed"
}
try {
Write-Log -Level INFO -Source 'entrypoint' -Message "Opening Pull Request $PullRequestTitle with body of $PullRequestBody"
New-GithubPullRequest -owner $DestinationRepoOwner -Repo $repository.name -Head "$($DestinationRepoOwner):$($BranchName)" -base $DefaultBranchName -title $PullRequestTitle -body $PullRequestBody
$pr = New-GithubPullRequest -owner $DestinationRepoOwner -Repo $repository.name -Head "$($DestinationRepoOwner):$($BranchName)" -base $DefaultBranchName -title $PullRequestTitle -body $PullRequestBody
}
catch {
Write-Log -Level ERROR -Source 'entrypoint' -Message "Unable to open Pull Request $PullRequestTitle with body of $PullRequestBody"
}
if ($manageLabels) {
try {
$prJson = $pr | ConvertTo-Json
Write-Log -level INFO -Source 'TEST******' -Message $prJson
Write-Log -level INFO -Source 'TEST******' -Message $pr.number
Write-Log -Level INFO -Source 'entrypoint' -Message "Setting Labels on Pull Request, $($labelsArray.ToString()), PR ID: $($pr.number)"
Add-GithubIssueLabels -owner $DestinationRepoOwner -repo $repository.Name -issueId $pr.number -labels $labelsArray
}
catch {
Write-Log -Level ERROR -Source 'entrypoint' -Message "Unable to set labels $labelsArray on Pull Request"
}
}
}
else {
Write-Log -Level INFO -Source 'entrypoint' -Message "No file changes to process"
Expand Down
45 changes: 45 additions & 0 deletions app/modules/changelog/changelog.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

function Set-ChangeLog {
param(
[String]
$ChangelogPath,
[String]
$ChangeLogMarker,
[String]
$ChangeLogEntry
)
if (-not (Test-Path $ChangeLogPath)) {
Write-Log -Level WARN -Source 'entrypoint' -Message "Unable to find $ChangeLogPath"
return $null
}

$changelog = get-content $ChangeLogPath
# Work around case sensitivity
if ($changelog | Where-Object { $_ -like "*$ChangeLogMarker*" }) {
$ChangeLogMarker = $changelog | Where-Object { $_ -like "*$ChangeLogMarker*" }
}
$changeIndex = $changelog.IndexOf($changelogMarker)

if ($changeIndex -ge 0) {
$changeIndex += 2
$changelog[$changeIndex] = "$changeLogEntry$($changelog[$changeIndex])"
}
else {
# Find the next title:
$NextSubTitle = ($changelog | Where-Object { $_ -like "## *" })[0]
if ($NextSubTitle) {
# Get the index of that subtitle
$NextSubTitleIndex = $changelog.IndexOf($NextSubTitle)

$changelog[$NextSubTitleIndex] = "$changelogMarker`n`n$changeLogEntry`n$($changelog[$NextSubTitleIndex])"
}
# Unable to find any subtitle
else {
$changelog[2] = "$changelogMarker`n`n$changeLogEntry`n$($changelog[2])"
}
}

Set-Content -path $changelogPath -Value $changelog
}

Export-ModuleMember *
14 changes: 13 additions & 1 deletion app/modules/git/git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ function New-CommitAndPushIfChanged {
[String]
$CommitMessage,
[Switch]
$Push
$Push,
[Switch]
$ChangeLogIsManaged,
[String]
$ChangeLogLocation,
[String]
$ChangeLogMarker
)
try {
$ChangeCount = Get-GitChangeCount
if ($ChangeCount -gt 0) {
Write-Log -level INFO -Source 'entrypoint' -Message "************ ChangeLogIsManaged: $ChangeLogIsManaged , ChangeLogLocation: $ChangeLogLocation , ChangeLogMarker = $ChangeLogMarker"
if ($ChangeLogIsManaged) {
Write-Log -Level INFO -Source 'entrypoint' -Message "Managing the changelog in $ChangeLogLocation with Marker of $ChangeLogMarker"
Set-ChangeLog -ChangelogPath $ChangeLogLocation -ChangeLogMarker $ChangeLogMarker -ChangeLogEntry "$CommitMessage`n`n"
}

Write-Log -Level INFO -Source 'git' -Message 'Files have changed adding all files'
git add -A
Write-Log -Level INFO -Source 'git' -Message "Committing to current branch $(git branch)"
Expand Down
19 changes: 19 additions & 0 deletions app/modules/github/github.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,23 @@ function New-GithubPullRequest {
}


function Add-GithubIssueLabels {
param(
[String]
$owner,
[String]
$repo,
[Int]
$issueId,
[Array]
$labels
)

Write-Log -Level INFO -Source 'github' -Message "Setting Labels $Labels on $owner/$repo : $issueID"
$labelsJson = @{'labels' = $labels} | ConvertTo-Json

return Invoke-GithubApi -Endpoint "repos/$owner/$repo/issues/$issueID/labels" -Method POST -Body $labelsJson -ErrorAction Stop
}


Export-ModuleMember *
43 changes: 0 additions & 43 deletions infrastructure/chef-cookbooks-cookbook.yml

This file was deleted.

43 changes: 0 additions & 43 deletions infrastructure/sous-chefs-cookbook.yml

This file was deleted.

43 changes: 0 additions & 43 deletions infrastructure/sous-chefs-ide.yml

This file was deleted.

43 changes: 0 additions & 43 deletions infrastructure/sous-chefs-orb.yml

This file was deleted.

Loading

0 comments on commit cc8423b

Please sign in to comment.