Skip to content

Commit

Permalink
Adding complete Pipeline support to the entire module (#242)
Browse files Browse the repository at this point in the history
# Description

Comprehensively adds pipeline support throughout the entire module

This change required examining every method, and in some cases a few breaking changes (noted below) had to be introduced in order to make support work consistently end-to-end.  UT's were added for every function, which resuled in a few bugs that were identified and fixed (also noted below).

## Breaking changes
### Unavoidable Breaks
 * `Get-GitHubRepositoryBranch`: `Name` parameter is now `BranchName`.
  > A `GitHub.Repository` object has a `name` property which is the name of the repo.  Piping in the repo object to this method would confuse it, making it search for a branch with the name of the repo, as opposed to the desired effect of just listing out all branches in the repo.

 * `*-GitHub*Label`: `Name` parameter is now `Label`.
  > Similar to above.
  > `Name` was too generic and was causing unintended conflicts when passing in certain objects over the pipeline (like a `GitHub.Repository`) which also has a `name` property, making it impossible to pipe in a repository to list all labels (since it was trying to query for a label in that repository named the same as the repo.

 * `*-GitHubLabel`: `Milestone` parameter is now `MilestoneNumber`.
  > A `GitHub.Issue` contains a `milestone` object property, and PowerShell was complaining that there was no way to convert that to an `[int64]` when an Issue was passed in via the pipeline.  The only way to avoid this was to use `MilestoneNumber` which is the name of the property we add to `GitHub.Milestone` objects, and it's what we alias to in all other methods that interact with milestones.

 * `*-GitHubIssue`: `Milestone` parameter is now `MilestoneNumber`.
  > A `GitHub.Issue` contains a `milestone` object property, and PowerShell was complaining that there was no way to convert that to an `[int64]` when an Issue was passed in via the pipeline.  The only way to avoid this was to use `MilestoneNumber` which is the name of the property we add to `GitHub.Milestone` objects, and it's what we alias to in all other methods that interact with milestones.

 * `Get-GitHubLicense`: `Name` parameter is now `Key`.
  > The `key` is what you can query against, but a License object has _both_ a `name` and a `key` property, which caused piped object queries to fail with the existing parameter name.
 
 * `Get-GitHubCodeOfConduct`: `Name` parameter is now `Key`.
  > Similar to above.
  > The `key` is what you can query against, but a Code of Conduct object has _both_ a `name` and a `key` property, which caused piped object queries to fail with the existing parameter name.
 
 * `New-ProjectCard`: Signature has changed.  Instead of specifying `ContentId` and `ContentType`, you now just directly specify either `IssueId` or `PullRequestId`.
  > Pipeline input would not have worked without this change. Additionally, this simply provides a cleaner interface for users in general, requiring one less input.
 
 * `Get-GitHubUserContextualInformation`: Signature has changed.  Instead of specifying `SubjectId` and `SubjectType`, you now just directly specify either `OrganizationId`, `RepositoryId`, `IssueId` or `PullRequestId`.
  > Similar to above.
  > Pipeline input would not have worked without this change. Additionally, this simply provides a cleaner interface for users in general, requiring one less input.
 

### Breaks With Workarounds
 * A number of changes to the Comments functions
    * `GitHubComments.ps1` was renamed to `GitHubIssueComments.ps1` given that there are 5 different types of comments, these functions focus on `Issue` comments, and there doesn't currently appear to be a path forward for creating a single unified API set for all comment types.

    * All of these methods were renamed from `*-GitHubComment` to `*-GitHubIssueComment`, however they were also aliased back to their previous names (for now).  You should really migrate to these new names however.

    * The main parameter was renamed from `CommentId` to `Comment`, however it was aliased back to `CommentId`.

 * `*-GitHubProject`: `ArchivedState` parameter is now `State` (but aliased back to `ArchivedState` to help with migration).

 * `*-GitHubProject`: `Name` parameter is now `ColumnName` (but aliased back to `Name`)

 * `*-GitHubProjectColumn`: `Name` parameter is now `ColumnName` (but aliased back to `Name`)

 * `Move-GitHubProjectCard`: `ColumnId` parameter is now `Column` (but aliased back to `ColumnId` to support pipeline input).

 * `Get-GitHubRelease`: `ReleaseId` parameter is now `Release` (but aliased back to `ReleaseId` to support pipeline input).
 
 * `Rename-GitHubRepository` has been effectively deprectaed.  It was built on top of the same endpoint that `Set-GitHubRepository` uses, but was only modifying a single property (the name).  For now, the function remains, but it now just internally calls into `Set-GitHubRepository` to perform the same action.
 
 * `Set-GitHubRepositoryTopic`: `Name` parameter is now `Topic` (but aliased back to `Name`).
 
 * `Get-GitHubUser`: `User` parameter is now `UserName` (but aliased back to `User` and `Name`).

 * `Get-GitHubUserContextualInformation`: `User` parameter is now `UserName` (but aliased back to `User` and `Name`).

## Bug Fixes:
 * Events had been using a typo'd version of the `sailor-v` AcceptHeader.  This was fixed as part of the migration to using the AcceptHeader constants.
 * `Lock-GitHubIssue` was not correctly providing the `lock_reason` value to the API, so that metadata was getting lost.  This was caught by new UT's and then fixed.
 * `Update-GitHubLabel` was modified to accept colors both with and without a leading # sign, just like `New-GitHubLabel` already supported.
 
## New Features:
 * `Get-GitHubGitIgnore` has a new `RawContent` switch which allows you to directly get back the content of the actual .gitignore file.
 * `Set-GitHubRepository` has been updated to allow users to change the name (rename) the repository at the same time as making any of their other changes.  If a new name has been specified, users will be required to confirm the change, or to specify `-Confirm:$false` and/or `-Force` to avoid the confirmation.
 * `Get-GitHubRepositoryCollaborator` has gained a new parameter `Affiliation` allowing you to filter which collaborators you're interested in querying for.

## Minor updates:
 * Fixed the casing of the "microsoft" OwnerName in any examples
 * Fixed the casing of `GitHub` in the `Assignee` method names (was `Github` intead of `GitHub`
 * Added new configuration property (`DisablePipelineSupport`) to assist with pipeline development
 * All `AcceptHeader` usage has migrated to using script-wide constants
 * `Split-GitHubUri` can now return both components in a single function call if no switch is specified.
 * Added `Join-GitHubUri` which can reverse what `Split-GitHubUri` does, based on the configured `ApiHostName`.
 * Adds type information (via `.OUTPUTS` and `[OutputType()]` for every function.
 * Documentation updates reflecting the new pipeline support.

## Test changes:
 * `Set-StrictMode -Version 1.0` has been specified for all tests to catch access to undeclared variables (which should help catch common typo errors)
 * The workarounds for PSScriptAnalyzer's false complaint for rule `PSUseDeclaredVarsMoreThanAssignments` have been removed, and all test files now suppress that rule.
 * A test file now exists for every module file, and some tests were moved around to the appropriate file (away from GitHubAnalytics.tests.ps1 which had previously been a dumping ground for tests).
 * A _substantial_ number of new tests have been added.  Every function should now be tested with limited exceptions (like for Organizations and Teams where we don't yet have the support in the module to get the account in the appropriate state to be able to query with those existing functions).
  
> Note: These tests fully pass with Pester 4.10.1.  Some additional work may be done prior to merging to get them passing with Pester 5.0, but the priority is to get this merged into master soon in order to unblock the pipeline of existing pull requests that have been waiting on this change.

#### Issues Fixed
- Fixes #63

#### References
[The whole API set](https://developer.github.com/v3/)
  • Loading branch information
HowardWolosky committed Jun 18, 2020
1 parent 1f63817 commit 17f6122
Show file tree
Hide file tree
Showing 54 changed files with 9,752 additions and 2,112 deletions.
6 changes: 3 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
-->
- [ ] You actually ran the code that you just wrote, especially if you did just "one last quick change".
- [ ] Comment-based help added/updated, including examples.
- [ ] [Static analysis](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#static-analysis)
is reporting back clean.
- [ ] [Static analysis](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#static-analysis) is reporting back clean.
- [ ] New/changed code adheres to our [coding guidelines](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#coding-guidelines).
- [ ] New/changed code continues to [support the pipeline](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#pipeline-support).
- [ ] Changes to the manifest file follow the [manifest guidance](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#module-manifest).
- [ ] Unit tests were added/updated and are all passing. See [testing guidelines](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#testing).
- [ ] Unit tests were added/updated and are all passing. See [testing guidelines](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#testing). This includes making sure that all pipeline input variations have been covered.
- [ ] Relevant usage examples have been added/updated in [USAGE.md](https://github.com/microsoft/PowerShellForGitHub/blob/master/USAGE.md).
- [ ] If desired, ensure your name is added to our [Contributors list](https://github.com/microsoft/PowerShellForGitHub/blob/master/CONTRIBUTING.md#contributors)
72 changes: 69 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Looking for information on how to use this module? Head on over to [README.md](
* [Adding New Configuration Properties](#adding-new-configuration-properties)
* [Code Comments](#code-comments)
* [Debugging Tips](#debugging-tips)
* [Pipeline Support](#pipeline-support)
* [Testing](#testing)
* [Installing Pester](#installing-pester)
* [Configuring Your Environment](#configuring-your-environment)
Expand Down Expand Up @@ -286,8 +287,71 @@ Set-GitHubConfiguration -LogRequestBody

----------

### Pipeline Support

This module has comprehensive support for the PowerShell pipeline. It is imperative that all
new functionality added to the module embraces this design.

* Most functions are declared as a `filter`. This is the equivalent of a `function` where the
body of the function is the `process` block, and the `begin/end` blocks are empty.

* In limited cases where one of the inputs is an array of something, and you specifically want that
to be processed as a single command (like adding a bunch of labels to a single issue at once),
you can implement it as a `function` where you use `begin/process` to gather all of the values
into a single internal array, and then do the actual command execution in the `end` block. A
good example of that which you can follow can be seen with `Add-GitHubIssueLabel`.

* Any function that requires the repo's `Uri` to be provided should be additionally aliased with
`[Alias('RepositoryUrl')]` and its `[Parameter()]` definition should include `ValueFromPipelineByPropertyName`.

* Do not use any generic term like `Name` in your parameters. That will end up causing unintended
pipeline issues down the line. For instance, if it's a label, call it `Label`, even though `Name`
would make sense, other objects in the pipeline (like a `GitHub.Respository` object) also have
a `name` property that would conflict.

* You should plan on adding additional properties to all objects being returned from an API call.
Any object that is specific to a repository should have a `RepositoryUrl` `NoteProperty` added
to it, enabling it to be piped-in to any other command that requires knowing which repository
you're talking about. Additionally, any other property that might be necessary to uniquely
identify that object in a different command should get added properties. For example, with Issues,
we add both an `IssueNumber` property and an `IssueId` property to it, as the Issue commands
need to interact with the `IssueNumber` while the Event commands interact with the `IssueId`.
We prefer to _only_ add additional properties that are believed to be needed as input to other
commands (as opposed to creating alias properties for all of the object's properties).

* For every major file, you will find an `Add-GitHub*AdditionalProperties` filter method at the end.
If you're writing a new file, you'll need to create this yourself (and model it after an existing
one). The goal of this is that you can simply pipe the output of your `Invoke-GHRestMethod`
directly into this method to update the result with the additional properties, and then return
that modified version to the user. The benefit of this approach is that you can then apply that
filter on child objects within the primary object. For instance, a `GitHub.Issue` has multiple
`GitHub.User` objects, `GitHub.Label` objects, a `GitHub.Milestone` object and more. Within
`Add-GitHubIssueAdditionalProperties`, it just needs to know to call the appropriate
`Add-GitHub*AdditionalProperties` method on the qualifying child properties, without needing to
know anything more about them.

* That method will also "type" information to each object. This is forward-looking work to ease
support for providing formatting of various object types in the future. That type should be
defined at the top of the current file at the script level (see other files for an example),
and you should be sure to both specify it in the `.OUTPUTS` section of the Comment Based Help (CBH)
for the command, as well as with `[OutputType({$script:GitHubUserTypeName})]` (for example).

* Going along with the `.OUTPUTS` is the `.INPUTS` section. Please maintain this section as well.
If you add any new type that will gain a `RepositoryUrl` property, then you'll need to update
virtually _all_ of the `.INPUTS` entries across all of the files where the function has a `Uri`
parameter. Please keep these type names alphabetical.

* To enable debugging issues involving pipeline support, there is an additional configuration
property that you might use: `Set-GitHubConfiguration -DisablePipelineSupport`. That will
prevent the module from adding _any_ additional properties to the objects.

----------

### Testing
[![Build status](https://dev.azure.com/ms/PowerShellForGitHub/_apis/build/status/PowerShellForGitHub-CI?branchName=master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)
[![Azure DevOps tests](https://img.shields.io/azure-devops/tests/ms/PowerShellForGitHub/109/master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/ms/PowerShellForGitHub/109/master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)


#### Installing Pester
This module supports testing using the [Pester UT framework](https://github.com/pester/Pester).
Expand Down Expand Up @@ -350,6 +414,8 @@ There are many more nuances to code-coverage, see

#### Automated Tests
[![Build status](https://dev.azure.com/ms/PowerShellForGitHub/_apis/build/status/PowerShellForGitHub-CI?branchName=master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)
[![Azure DevOps tests](https://img.shields.io/azure-devops/tests/ms/PowerShellForGitHub/109/master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/ms/PowerShellForGitHub/109/master)](https://dev.azure.com/ms/PowerShellForGitHub/_build/latest?definitionId=109&branchName=master)

These test are configured to automatically execute upon any update to the `master` branch
of `microsoft/PowerShellForGitHub`.
Expand All @@ -362,9 +428,9 @@ as well...it is stored, encrypted, within Azure DevOps. It is not accessible fo
the CI pipeline. To run the tests locally with your own account, see
[configuring-your-environment](#configuring-your-environment).

> NOTE: We're currently encountering issues with the tests successfully running within the pipeline.
> They do complete successfully locally, so please test your changes locally before submitting a
> pull request.
> Your change must successfully pass all tests before they will be merged. While we will run a CI
> build on your behalf for any submitted pull request, it's to your benefit to verify your changes
> locally first.
#### New Test Guidelines
Your tests should have NO dependencies on an account being set up in a specific way. They should
Expand Down
26 changes: 21 additions & 5 deletions GitHubAnalytics.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ function Group-GitHubIssue
The date property that should be inspected when determining which week grouping the issue
if part of.
.INPUTS
GitHub.Issue
.OUTPUTS
[PSCustomObject[]] Collection of issues and counts, by week, along with the total count of issues.
[PSCustomObject[]]
Collection of issues and counts, by week, along with the total count of issues.
.EXAMPLE
$issues = @()
Expand Down Expand Up @@ -90,8 +94,12 @@ function Group-GitHubIssue
foreach ($week in $weekDates)
{
$filteredIssues = @($Issue | Where-Object {
(($DateType -eq 'Created') -and ($_.created_at -ge $week) -and ($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Closed') -and ($_.closed_at -ge $week) -and ($_.closed_at -le $endOfWeek))
(($DateType -eq 'Created') -and
($_.created_at -ge $week) -and
($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Closed') -and
($_.closed_at -ge $week) -and
($_.closed_at -le $endOfWeek))
})

$endOfWeek = $week
Expand Down Expand Up @@ -144,6 +152,9 @@ function Group-GitHubPullRequest
The date property that should be inspected when determining which week grouping the
pull request if part of.
.INPUTS
GitHub.PullRequest
.OUTPUTS
[PSCustomObject[]] Collection of pull requests and counts, by week, along with the
total count of pull requests.
Expand Down Expand Up @@ -211,8 +222,12 @@ function Group-GitHubPullRequest
foreach ($week in $weekDates)
{
$filteredPullRequests = @($PullRequest | Where-Object {
(($DateType -eq 'Created') -and ($_.created_at -ge $week) -and ($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Merged') -and ($_.merged_at -ge $week) -and ($_.merged_at -le $endOfWeek))
(($DateType -eq 'Created') -and
($_.created_at -ge $week) -and
($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Merged') -and
($_.merged_at -ge $week) -and
($_.merged_at -le $endOfWeek))
})

$endOfWeek = $week
Expand Down Expand Up @@ -265,6 +280,7 @@ function Get-WeekDate
Get-WeekDate -Weeks 10
#>
[CmdletBinding()]
[OutputType([DateTime[]])]
param(
[ValidateRange(0, 10000)]
[int] $Weeks = 12
Expand Down

0 comments on commit 17f6122

Please sign in to comment.