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

[tools/generator] add automation and release command for track2 #15271

Merged
merged 21 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
93ec460
[tools/generator] add automation and release command for track2
tadelesh Aug 12, 2021
db0bd9c
add copyright and do gofmt
tadelesh Aug 12, 2021
245a69f
Use semver to pump version, move git operation to repo package and fi…
tadelesh Aug 12, 2021
c82b9eb
Add specfic version to release cmd, seperate package for automation a…
tadelesh Aug 13, 2021
5d4edd2
add automation script and config for track2
tadelesh Aug 15, 2021
417cf29
fix: three dots path not work in linux in build script
tadelesh Aug 15, 2021
5a53141
fix: ln pwsh.exe to pwsh to run in linux
tadelesh Aug 15, 2021
0cd9e08
fix: ignore dep check for go-git
tadelesh Aug 15, 2021
92659f5
chore: label name change for track2
tadelesh Aug 16, 2021
3a27314
fix: wrong package path result in empty git commit
tadelesh Aug 16, 2021
57a5c39
fix: repo addr replacement in autorest.md
tadelesh Aug 16, 2021
f77a02c
chore: move script to eng folder and change name
tadelesh Aug 17, 2021
db9c1f0
fix: onboard generation need to replace NewClientMethod
tadelesh Aug 17, 2021
657ec8d
chore: change all naming from track2 to v2
tadelesh Aug 18, 2021
4eabb8f
fix: add new build constraint
tadelesh Aug 18, 2021
ac3b18e
fix: template compat with go 1.17 and change commit msg for release
tadelesh Aug 18, 2021
c3bae2d
chore: add v2 to readme begin regex to support future change of `trac…
tadelesh Aug 19, 2021
4afbde6
chore: remove track2 naming
tadelesh Aug 19, 2021
d6b470c
fix: duplicated and wrong expression
tadelesh Aug 19, 2021
726242c
fix: add package title param to release cmd
tadelesh Aug 20, 2021
0fd2226
fix: ci
tadelesh Aug 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# name = "github.com/x/y"
# version = "2.4.0"

ignored = ["github.com/ahmetb/go-linq/*", "github.com/google/go-github/*"]
ignored = ["github.com/ahmetb/go-linq/*", "github.com/google/go-github/*", "github.com/go-git/go-git/v5", "github.com/go-git/go-git/v5/*"]
tadelesh marked this conversation as resolved.
Show resolved Hide resolved

[prune]
unused-packages = true
Expand Down
19 changes: 19 additions & 0 deletions eng/scripts/automation_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set -x
set -e
export GOPATH="$(realpath ../../../..)"
if [ ! -d "$GOPATH/bin" ]
then
mkdir $GOPATH/bin
fi
PATH=$PATH:$GOPATH/bin
export GO111MODULE=on
cd tools/generator && go build && cp generator $GOPATH/bin && cd ../..
ln -s /usr/bin/pwsh $GOPATH/bin/pwsh.exe
cat > $2 << EOF
{
"envs": {
"PATH": "$PATH:$GOPATH",
"GOPATH": "$GOPATH"
}
}
EOF
2 changes: 1 addition & 1 deletion eng/scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $root = Resolve-Path ($PSScriptRoot + "/../..")
Set-Location $root
$sdks = @{};

foreach ($sdk in (Get-ModuleDirs 'sdk/...')) {
foreach ($sdk in (Get-ModuleDirs 'sdk/')) {
tadelesh marked this conversation as resolved.
Show resolved Hide resolved
$name = $sdk | split-path -leaf
$sdks[$name] = @{
'path' = $sdk;
Expand Down
28 changes: 28 additions & 0 deletions eng/swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
"generateOptions": {
"generateScript": {
"path": "generator automation-v2",
"stderr": {
"showInComment": "^\\[AUTOREST\\]",
"scriptError": "^\\[ERROR\\]",
"scriptWarning": "^\\[WARNING\\]"
}
}
},
"advancedOptions": {
"cloneDir": "src/github.com/Azure/azure-sdk-for-go",
"breakingChangeTracking": true
},
"initOptions": {
"initScript": {
"path": "sh ./eng/scripts/automation_init.sh",
"stderr": {
"scriptWarning": false
}
}
},
"packageOptions": {
"breakingChangeLabel": "CI-BreakingChange-Go-V2"
}
}
4 changes: 4 additions & 0 deletions tools/generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/automation"
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/issue"
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/template"
automation_v2 "github.com/Azure/azure-sdk-for-go/tools/generator/cmd/v2/automation"
release_v2 "github.com/Azure/azure-sdk-for-go/tools/generator/cmd/v2/release"
"github.com/spf13/cobra"
)

Expand All @@ -29,6 +31,8 @@ func Command() *cobra.Command {

rootCmd.AddCommand(
automation.Command(),
automation_v2.Command(),
release_v2.Command(),
issue.Command(),
template.Command(),
)
Expand Down
150 changes: 150 additions & 0 deletions tools/generator/cmd/v2/automation/automationCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package automation

import (
"fmt"
"log"
"os"
"strings"

"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/automation/pipeline"
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/v2/common"
"github.com/Azure/azure-sdk-for-go/tools/internal/utils"
"github.com/spf13/cobra"
)

// Command returns the automation v2 command. Note that this command is designed to run in the root directory of
// azure-sdk-for-go. It does not work if you are running this tool in somewhere else
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "automation-v2 <generate input filepath> <generate output filepath>",
Args: cobra.ExactArgs(2),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetFlags(0) // remove the time stamp prefix
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := execute(args[0], args[1]); err != nil {
logError(err)
return err
}
return nil
},
SilenceUsage: true, // this command is used for a pipeline, the usage should never show
}

return cmd
}

func execute(inputPath, outputPath string) error {
log.Printf("Reading generate input file from '%s'...", inputPath)
input, err := pipeline.ReadInput(inputPath)
if err != nil {
return fmt.Errorf("cannot read generate input: %+v", err)
}
log.Printf("Generating using the following GenerateInput...\n%s", input.String())
cwd, err := os.Getwd()
if err != nil {
return err
}
log.Printf("Using current directory as SDK root: %s", cwd)

ctx := automationContext{
sdkRoot: utils.NormalizePath(cwd),
specRoot: input.SpecFolder,
commitHash: input.HeadSha,
}
output, err := ctx.generate(input)
if err != nil {
return err
}
log.Printf("Output generated: \n%s", output.String())
log.Printf("Writing output to file '%s'...", outputPath)
if err := pipeline.WriteOutput(outputPath, output); err != nil {
return fmt.Errorf("cannot write generate output: %+v", err)
}
return nil
}

type automationContext struct {
sdkRoot string
specRoot string
commitHash string
}

// TODO -- support dry run
func (ctx *automationContext) generate(input *pipeline.GenerateInput) (*pipeline.GenerateOutput, error) {
if input.DryRun {
return nil, fmt.Errorf("dry run not supported yet")
}

// iterate over all the readme
results := make([]pipeline.PackageResult, 0)
errorBuilder := generateErrorBuilder{}
for _, readme := range input.RelatedReadmeMdFiles {
log.Printf("Start to process readme file: %s", readme)
generateCtx := common.GenerateContext{
SdkPath: ctx.sdkRoot,
SpecPath: ctx.specRoot,
CommitHash: ctx.commitHash,
}

namespaceResults, errors := generateCtx.GenerateForAutomation(readme, input.RepoHTTPSURL)
if len(errors) != 0 {
errorBuilder.add(errors...)
continue
}

for _, namespaceResult := range namespaceResults {
content := namespaceResult.ChangelogMd
breaking := namespaceResult.Changelog.HasBreakingChanges()
breakingChangeItems := namespaceResult.Changelog.GetBreakingChangeItems()

results = append(results, pipeline.PackageResult{
Version: namespaceResult.Version,
PackageName: namespaceResult.PackageName,
Path: []string{fmt.Sprintf("sdk/%s/%s", namespaceResult.RpName, namespaceResult.PackageName)},
ReadmeMd: []string{readme},
Changelog: &pipeline.Changelog{
Content: &content,
HasBreakingChange: &breaking,
BreakingChangeItems: &breakingChangeItems,
},
})
}
log.Printf("Finish to process readme file: %s", readme)
}

return &pipeline.GenerateOutput{
Packages: results,
}, errorBuilder.build()
}

type generateErrorBuilder struct {
errors []error
}

func (b *generateErrorBuilder) add(err ...error) {
b.errors = append(b.errors, err...)
}

func (b *generateErrorBuilder) build() error {
if len(b.errors) == 0 {
return nil
}
var messages []string
for _, err := range b.errors {
messages = append(messages, err.Error())
}
return fmt.Errorf("total %d error(s): \n%s", len(b.errors), strings.Join(messages, "\n"))
}

func logError(err error) {
for _, line := range strings.Split(err.Error(), "\n") {
if l := strings.TrimSpace(line); l != "" {
log.Printf("[ERROR] %s", l)
}
}
}
22 changes: 22 additions & 0 deletions tools/generator/cmd/v2/common/cmdProcessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package common

import (
"fmt"
"log"
"os/exec"
)

// execute `go generate` command and fetch result
func ExecuteGoGenerate(path string) error {
cmd := exec.Command("go", "generate")
cmd.Dir = path
output, err := cmd.CombinedOutput()
log.Printf("Result of `go generate` execution: \n%s", string(output))
if err != nil {
return fmt.Errorf("failed to execute go generate '%s': %+v", string(output), err)
}
return nil
}
Loading