-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds endpoint to list installations, support org_name lookup with mor…
…e than 30 installations (#141) * Add plugin version to Vault backend Signed-off-by: Martin Baillie <[email protected]> * Bump codecov/codecov-action from 4.1.0 to 4.5.0 Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.1.0 to 4.5.0. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](codecov/codecov-action@v4.1.0...v4.5.0) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Fix formatting Signed-off-by: Martin Baillie <[email protected]> * Bump codecov/codecov-action from 4.5.0 to 5.1.2 Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.5.0 to 5.1.2. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](codecov/codecov-action@v4.5.0...v5.1.2) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * installations endpoint * test * tests pass * add pagination * Improve GitHub Installations API documentation Signed-off-by: Martin Baillie <[email protected]> * Pre-allocate map Signed-off-by: Martin Baillie <[email protected]> * Add pagination tests for installation path handling Signed-off-by: Martin Baillie <[email protected]> --------- Signed-off-by: Martin Baillie <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Martin Baillie <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c493250
commit 75580a4
Showing
5 changed files
with
410 additions
and
23 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/hashicorp/vault/sdk/framework" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// pathPatternInstallation is the string used to define the base path of the | ||
// installations endpoint. | ||
const pathPatternInstallations = "installations" | ||
|
||
const ( | ||
pathInstallationsHelpSyn = ` | ||
List GitHub App installations associated with this plugin's configuration. | ||
` | ||
pathInstallationsHelpDesc = ` | ||
This endpoint returns a mapping of GitHub organization names to their | ||
corresponding installation IDs for the App associated with this plugin's | ||
configuration. It automatically handles GitHub API pagination, combining results | ||
from all pages into a single response. This ensures complete results even for | ||
GitHub Apps installed on many organizations. | ||
` | ||
) | ||
|
||
func (b *backend) pathInstallations() *framework.Path { | ||
return &framework.Path{ | ||
Pattern: pathPatternInstallations, | ||
Fields: map[string]*framework.FieldSchema{}, | ||
ExistenceCheck: b.pathInstallationsExistenceCheck, | ||
Operations: map[logical.Operation]framework.OperationHandler{ | ||
// As per the issue request in https://git.io/JUhRk, allow Vault | ||
// Reads (i.e. HTTP GET) to also write the GitHub installations. | ||
logical.ReadOperation: &framework.PathOperation{ | ||
Callback: withFieldValidator(b.pathInstallationsWrite), | ||
}, | ||
}, | ||
HelpSynopsis: pathInstallationsHelpSyn, | ||
HelpDescription: pathInstallationsHelpDesc, | ||
} | ||
} | ||
|
||
// pathInstallationsWrite corresponds to READ, CREATE and UPDATE on /github/installations. | ||
func (b *backend) pathInstallationsWrite( | ||
ctx context.Context, | ||
req *logical.Request, | ||
_ *framework.FieldData, | ||
) (res *logical.Response, err error) { | ||
client, done, err := b.Client(ctx, req.Storage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer done() | ||
|
||
// Instrument and log the installations API call, recording status, duration and | ||
// whether any constraints (permissions, repository IDs) were requested. | ||
defer func(begin time.Time) { | ||
duration := time.Since(begin) | ||
b.Logger().Debug("attempted to fetch installations", | ||
"took", duration.String(), | ||
"err", err, | ||
) | ||
installationsDuration.With(prometheus.Labels{ | ||
"success": strconv.FormatBool(err == nil), | ||
}).Observe(duration.Seconds()) | ||
}(time.Now()) | ||
|
||
// Perform the installations request. | ||
return client.ListInstallations(ctx) | ||
} | ||
|
||
// pathInstallationsExistenceCheck always returns false to force the Create path. This | ||
// plugin predates the framework's 'ExistenceCheck' features and we wish to | ||
// avoid changing any contracts with the user at this stage. Installations are created | ||
// regardless of whether the request is a CREATE, UPDATE or even READ (per a | ||
// user's request (https://git.io/JUhRk). | ||
func (b *backend) pathInstallationsExistenceCheck( | ||
context.Context, *logical.Request, *framework.FieldData, | ||
) (bool, error) { | ||
return false, nil | ||
} |
Oops, something went wrong.