Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rajinderSiddhu committed Mar 4, 2021
0 parents commit 0a15378
Show file tree
Hide file tree
Showing 11 changed files with 1,513 additions and 0 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/addPluginReleaseToRepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"strings"
)

func check(e error) {
if e != nil {
panic(e)
}
}

type PluginReleaseEvent struct {
Org string
Repo string
Released Plugin
}

type Plugin struct {
Id string `json:"id"`
Description string `json:"description"`
Provider string `json:"provider"`
Releases []Release `json:"releases"`
}

type Release struct {
Version string `json:"version"`
Date string `json:"date"`
Requires string `json:"requires"`
Sha512sum string `json:"sha512sum"`
State string `json:"state"`
Url string `json:"url"`
}

func main() {

pluginReleaseJson := []byte(os.Args[1])
var pluginReleaseEvent PluginReleaseEvent
pluginReleaseErr := json.Unmarshal(pluginReleaseJson, &pluginReleaseEvent)
check(pluginReleaseErr)

pluginsJson, pluginsJsonReadErr := ioutil.ReadFile("plugins.json")
check(pluginsJsonReadErr)
var plugins []Plugin
pluginsErr := json.Unmarshal(pluginsJson, &plugins)
check(pluginsErr)

updatedPlugins := addReleaseToPlugins(pluginReleaseEvent, plugins)

encodeBuffer := new(bytes.Buffer)
enc := json.NewEncoder(encodeBuffer)
enc.SetEscapeHTML(false)
encodeErr := enc.Encode(updatedPlugins)
check(encodeErr)
var indentBuffer bytes.Buffer
indentErr := json.Indent(&indentBuffer, encodeBuffer.Bytes(), " ", " ")
check(indentErr)

pluginsJsonWriteErr := ioutil.WriteFile("plugins.json", indentBuffer.Bytes(), 0644)
check(pluginsJsonWriteErr)
}


func addReleaseToPlugins(releaseEvent PluginReleaseEvent, existingPlugins []Plugin) []Plugin {
releasedPlugin := releaseEvent.Released
release := releasedPlugin.Releases[0]
release.Url = "https://github.com/" + releaseEvent.Org + "/" + releaseEvent.Repo + "/releases/download/" + release.Version + "/" + releaseEvent.Repo + "-" + release.Version + ".zip"
if strings.HasPrefix(release.Version,"v") {
// the plugins version is supplied with a v from the bundler, but fails when update manager compares versions
release.Version = release.Version[1:]
}
releasedPlugin.Releases = []Release {
release,
}

for ip, existingPlugin := range existingPlugins {
if existingPlugin.Id == releasedPlugin.Id {
for ir, existingRelease := range existingPlugin.Releases {
if existingRelease.Version == release.Version {
existingPlugin.Releases = append(existingPlugin.Releases[:ir], existingPlugin.Releases[ir+1:]...)
}
}
releasedPlugin.Releases = append(releasedPlugin.Releases, existingPlugin.Releases...)
existingPlugins[ip] = releasedPlugin
return existingPlugins
}
}

return append(existingPlugins, releasedPlugin)
}



253 changes: 253 additions & 0 deletions .github/workflows/addPluginReleaseToRepo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
package main

import (
"reflect"
"testing"
)

func TestAddingReleaseToEmptyRepo(t *testing.T) {

release := []Release {
{
"v1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"",
}}
pluginReleaseEvent := PluginReleaseEvent{"org1", "repo1",
Plugin {
"pluginId1",
"plugin description",
"provider1",
release,
}}

var existingPlugins []Plugin

result := addReleaseToPlugins(pluginReleaseEvent, existingPlugins)

expectedReleases := []Release {
{
"1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo1/releases/download/v1.2.0/repo1-v1.2.0.zip",
}}
expectedPlugins := []Plugin {
{
"pluginId1",
"plugin description",
"provider1",
expectedReleases,
},
}

if !reflect.DeepEqual(result, expectedPlugins) {
t.Errorf("Release was not added correctly: %s", result)
}

}

func TestAddingReleaseToRepoWithOtherPlugins(t *testing.T) {

release := []Release {
{
"v1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"",
}}
pluginReleaseEvent := PluginReleaseEvent{"org1", "repo1",
Plugin {
"pluginId1",
"plugin description",
"provider1",
release,
}}

existingReleases := []Release {
{
"1.0.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo0/releases/download/v1.0.0/repo0-v1.0.0.zip",
}}
existingPlugins := []Plugin {
{
"pluginId0",
"plugin description",
"provider1",
existingReleases,
}}

result := addReleaseToPlugins(pluginReleaseEvent, existingPlugins)

expectedReleases := []Release {
{
"1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo1/releases/download/v1.2.0/repo1-v1.2.0.zip",
}}
expectedPlugins := []Plugin {
existingPlugins[0],
{
"pluginId1",
"plugin description",
"provider1",
expectedReleases,
},
}
if !reflect.DeepEqual(result, expectedPlugins) {
t.Errorf("Release was not added correctly: %s", result)
}

}

func TestAddingReleaseToRepoWithExistingReleases(t *testing.T) {

release := []Release {
{
"v1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"",
}}
pluginReleaseEvent := PluginReleaseEvent{"org1", "repo1",
Plugin {
"pluginId1",
"new plugin description",
"provider1",
release,
}}

existingReleases := []Release {
{
"1.0.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo0/releases/download/v1.0.0/repo1-v1.0.0.zip",
}}
existingPlugins := []Plugin {
{
"pluginId1",
"plugin description",
"provider1",
existingReleases,
}}

result := addReleaseToPlugins(pluginReleaseEvent, existingPlugins)

expectedReleases := []Release {
{
"1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo1/releases/download/v1.2.0/repo1-v1.2.0.zip",
},
existingReleases[0],
}
expectedPlugins := []Plugin {
{
"pluginId1",
"new plugin description",
"provider1",
expectedReleases,
},
}

if !reflect.DeepEqual(result, expectedPlugins) {
t.Errorf("Release was not added correctly: %s", result)
}

}


func TestAddingReleaseToRepoWithTheSameRelease(t *testing.T) {

release := []Release {
{
"v1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"",
}}
pluginReleaseEvent := PluginReleaseEvent{"org1", "repo1",
Plugin {
"pluginId1",
"plugin description",
"provider1",
release,
}}

existingReleases := []Release {
{
"1.2.0",
"2020-01-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo0/releases/download/v1.0.0/repo1-v1.0.0.zip",
},
{
"1.0.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo0/releases/download/v1.0.0/repo1-v1.0.0.zip",
}}
existingPlugins := []Plugin {
{
"pluginId1",
"new plugin description",
"provider1",
existingReleases,
}}

result := addReleaseToPlugins(pluginReleaseEvent, existingPlugins)

expectedReleases := []Release {
{
"1.2.0",
"2020-02-24T20:46:40.585Z",
"orca>=0.0.0",
"asdf",
"RELEASE",
"https://github.com/org1/repo1/releases/download/v1.2.0/repo1-v1.2.0.zip",
},
existingReleases[1],
}
expectedPlugins := []Plugin {
{
"pluginId1",
"plugin description",
"provider1",
expectedReleases,
},
}


if !reflect.DeepEqual(result, expectedPlugins) {
t.Errorf("Release was not added correctly: %s", result)
}

}
Loading

0 comments on commit 0a15378

Please sign in to comment.