-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0a15378
Showing
11 changed files
with
1,513 additions
and
0 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
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) | ||
} | ||
|
||
|
||
|
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,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) | ||
} | ||
|
||
} |
Oops, something went wrong.