Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM - Testing PR size #33

Closed
wants to merge 19 commits into from
47 changes: 47 additions & 0 deletions .github/actions/size_pull_request/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Cloud integration test splitter
description: Evaluate pull request size.

inputs:
path:
description: The path to the collection.
required: true

runs:
using: composite
steps:
- name: setup python
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install python required libraries
run: pip install -U requests
shell: bash

# - name: Testing comment on issue
# run: >-
# gh api --method POST
# -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
# /repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments
# -f "body=Testing comment via an action"
# shell: bash
# env:
# GH_TOKEN: ${{ github.token }}
- name: testing curl
run: >-
curl -X POST -L -H "Accept: application/vnd.github+json"
-H "Authorization: Bearer ${{ github.token }}"
-H "X-GitHub-Api-Version: 2022-11-28"
https://api.github.com/repos/ansible/terraform-provider-aap/issues/33/comments
-d '{"body": "Hello this is working from GH action"}'
shell: bash

- name: Evaluate pull request size
run: >-
python3 ${{ github.action_path }}/check_pr.py
--path ${{ inputs.path }}
--repository ${{ github.repository }}
--pr-number ${{ github.event.pull_request.number }}
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
65 changes: 65 additions & 0 deletions .github/actions/size_pull_request/check_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) 2024 Aubin Bikouo <@abikouo>
# GNU General Public License v3.0+
# (see https://www.gnu.org/licenses/gpl-3.0.txt)

from argparse import ArgumentParser
import subprocess
import re
import requests
import os


def WriteComment(repository: str, pr_number: int, comment: str) -> None:
url = f"https://api.github.com/repos/{repository}/issues/{pr_number}/comments"
result = requests.post(
url,
headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Authorization": "Bearer %s" % os.environ.get("GITHUB_TOKEN"),
},
json={"body": comment},
)
if result.status_code != 200:
raise RuntimeError(f"Post to URL {url} returned status code = {result.status_code}")


def RunDiff(path: str, repository: str, pr_number: int) -> None:
command = ["git diff --cached --stat $(git merge-base FETCH_HEAD origin)"]
proc = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=path
)
out, _ = proc.communicate()
WriteComment(
repository,
pr_number,
f"Output => {out.decode()}",
)
m = re.search(
"(\d*) files changed, (\d*) insertions\(\+\), (\d*) deletions\(\-\)",
out.decode(),
)
if m:
files = int(m.group(1))
insertions = int(m.group(2))
deletions = int(m.group(3))
WriteComment(
repository,
pr_number,
f"files = {files} - insertions = {insertions} - deletions = {deletions}",
)


if __name__ == "__main__":
"""Check PR size and push corresponding message and/or add label."""
parser = ArgumentParser()
parser.add_argument("--path", required=True, help="Path to the repository.")
parser.add_argument("--repository", required=True, help="Repository name org/name.")
parser.add_argument(
"--pr-number", type=int, required=True, help="The pull request number."
)

args = parser.parse_args()
RunDiff(args.path, args.repository, args.pr_number)
22 changes: 22 additions & 0 deletions .github/workflows/codereview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Code review
on:
pull_request:
types:
- opened
- reopened
- synchronize
branches:
- main
- stable-*

jobs:
pull-request-size:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: abikouo/terraform-provider-aap/.github/actions/size_pull_request@size_pr_aap_version
with:
path: "."
3 changes: 3 additions & 0 deletions changelogs/fragments/20240619-aap-versioning.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Support dynamic value for AAP endpoints since the value depends on the AAP version (https://github.com/ansible/terraform-provider-aap/pull/30).
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.1.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.19.1
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/stretchr/testify v1.8.4
)
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
16 changes: 0 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,18 @@ github.com/hashicorp/go-plugin v1.5.2/go.mod h1:w1sAEES3g3PuV/RzUrgow20W2uErMly8
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hc-install v0.6.4 h1:QLqlM56/+SIIGvGcfFiwMY3z5WGXT066suo/v9Km8e0=
github.com/hashicorp/hc-install v0.6.4/go.mod h1:05LWLy8TD842OtgcfBbOT0WMoInBMUSHjmDx10zuBIA=
github.com/hashicorp/hc-install v0.7.0 h1:Uu9edVqjKQxxuD28mR5TikkKDd/p55S8vzPC1659aBk=
github.com/hashicorp/hc-install v0.7.0/go.mod h1:ELmmzZlGnEcqoUMKUuykHaPCIR1sYLYX+KSggWSKZuA=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8JyYF3vpnuEo=
github.com/hashicorp/terraform-exec v0.20.0/go.mod h1:ckKGkJWbsNqFKV1itgMnE0hY9IYf1HoiekpuN0eWoDw=
github.com/hashicorp/terraform-exec v0.21.0 h1:uNkLAe95ey5Uux6KJdua6+cv8asgILFVWkd/RG0D2XQ=
github.com/hashicorp/terraform-exec v0.21.0/go.mod h1:1PPeMYou+KDUSSeRE9szMZ/oHf4fYUmB923Wzbq1ICg=
github.com/hashicorp/terraform-json v0.21.0 h1:9NQxbLNqPbEMze+S6+YluEdXgJmhQykRyRNd+zTI05U=
github.com/hashicorp/terraform-json v0.21.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7orfb5Ltvec=
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/terraform-plugin-docs v0.19.2 h1:YjdKa1vuqt9EnPYkkrv9HnGZz175HhSJ7Vsn8yZeWus=
github.com/hashicorp/terraform-plugin-docs v0.19.2/go.mod h1:gad2aP6uObFKhgNE8DR9nsEuEQnibp7il0jZYYOunWY=
github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSeyEljqjH3G39w28JK4c=
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5OckzhoiO3ig6SGxwelD2sI=
Expand Down Expand Up @@ -228,8 +218,6 @@ golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOM
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 h1:EDuYyU/MkFXllv9QF9819VlI9a4tzGuCbhG0ExK9o1U=
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -238,8 +226,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -270,8 +256,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
83 changes: 77 additions & 6 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package provider
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand All @@ -18,18 +20,72 @@ type ProviderHTTPClient interface {
Get(path string) ([]byte, diag.Diagnostics)
Update(path string, data io.Reader) ([]byte, diag.Diagnostics)
Delete(path string) ([]byte, diag.Diagnostics)
setApiEndpoint() diag.Diagnostics
getApiEndpoint() string
}

func GetKeyFromJson[T interface{}](jsonData []byte, keyName string, value *T) error {
// Unmarshal the JSON data
var result map[string]interface{}
err := json.Unmarshal(jsonData, &result)
if err != nil {
return err
}
// Extract key from Json structure
rawValue, ok := result[keyName]
if !ok {
return fmt.Errorf("missing attribute '%s' from JSON response", keyName)
}
*value = rawValue.(T)
return nil
}

func readApiEndpoint(client ProviderHTTPClient) (string, diag.Diagnostics) {
body, diags := client.Get("/api/")
if diags.HasError() {
return "", diags
}
var apis map[string]interface{}
err := GetKeyFromJson[map[string]interface{}](body, "apis", &apis)
if err == nil {
// AAP 2.5 returns {"apis": { "controller": "/api/controller/", (...) } (...)}
// We need to fetch '/api/controller/' to have the current version
controller, ok := apis["controller"]
if !ok {
diags.AddError(
"Unable to Retrieve controller endpoint from Gateway response",
fmt.Sprintf("Unexpected error: %s", err.Error()),
)
return "", diags
}
body, diags = client.Get(controller.(string))
if diags.HasError() {
return "", diags
}
}

var endpoint string
err = GetKeyFromJson[string](body, "current_version", &endpoint)
if err != nil {
diags.AddError(
"Error while setting API Endpoint",
fmt.Sprintf("Unexpected error: %s", err.Error()),
)
}
return endpoint, diags
}

// Client -
type AAPClient struct {
HostURL string
Username *string
Password *string
httpClient *http.Client
HostURL string
Username *string
Password *string
httpClient *http.Client
ApiEndpoint string
}

// NewClient - create new AAPClient instance
func NewClient(host string, username *string, password *string, insecureSkipVerify bool, timeout int64) (*AAPClient, error) {
func NewClient(host string, username *string, password *string, insecureSkipVerify bool, timeout int64) (*AAPClient, diag.Diagnostics) {
hostURL, _ := url.JoinPath(host, "/")
client := AAPClient{
HostURL: hostURL,
Expand All @@ -42,7 +98,22 @@ func NewClient(host string, username *string, password *string, insecureSkipVeri
}
client.httpClient = &http.Client{Transport: tr, Timeout: time.Duration(timeout) * time.Second}

return &client, nil
// Set AAP API endpoint
diags := client.setApiEndpoint()
return &client, diags
}

func (c *AAPClient) setApiEndpoint() diag.Diagnostics {
endpoint, diags := readApiEndpoint(c)
if diags.HasError() {
return diags
}
c.ApiEndpoint = endpoint
return diags
}

func (c *AAPClient) getApiEndpoint() string {
return c.ApiEndpoint
}

func (c *AAPClient) computeURLPath(path string) string {
Expand Down
Loading
Loading