Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
cdupuis committed Jul 8, 2020
1 parent 831507c commit 43cf54b
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 77 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.idea/
*.iml
.vscode/
**/*~
**/.#*
.npm*
.travis.yml
.atomist/
.nyc_output/
assets/kubectl/
node_modules/
# build/
coverage/
doc/
log/
scripts/
src/
test/
CO*.md
**/*.log
**/*.txt

build/test/
build/typedoc/
img/
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM ubuntu:focal

# tools
RUN apt-get update && apt-get install -y \
curl \
wget \
gnupg \
build-essential \
&& rm -rf /var/lib/apt/lists/*

# nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc" \
&& echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$HOME/.bashrc"

# nodejs and tools
RUN bash -c "source $HOME/.nvm/nvm.sh \
&& nvm install 10 \
&& nvm install 12 \
&& nvm install 14 \
&& nvm use --lts"

WORKDIR "/skill"

COPY package.json package-lock.json ./

RUN bash -c "source $HOME/.nvm/nvm.sh \
&& npm ci --no-optional \
&& npm cache clean --force"

COPY . ./

WORKDIR "/atm/home"

ENTRYPOINT ["node", "--no-deprecation", "--trace-warnings", "--expose_gc", "--optimize_for_size", "--always_compact", "--max_old_space_size=512", "/skill/node_modules/.bin/atm-skill"]
CMD ["run"]
10 changes: 9 additions & 1 deletion docs/images/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions graphql/query/repos.graphql

This file was deleted.

27 changes: 27 additions & 0 deletions graphql/subscription/buildOnPush.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
subscription buildOnPush {
Push {
repo {
url
owner
name
org {
provider {
apiUrl
gitUrl
}
}
channels {
name
team {
id
}
}
}
branch
after {
timestamp
sha
url
}
}
}
22 changes: 0 additions & 22 deletions lib/commands/helloWorld.ts

This file was deleted.

4 changes: 3 additions & 1 deletion lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
*/

export interface Configuration {
world: string;
version: string;
scripts: string[];
docker_cache: string[];
}
173 changes: 173 additions & 0 deletions lib/events/buildOnPush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
EventContext,
EventHandler,
Step,
secret,
repository,
project,
runSteps,
StepListener,
HandlerStatus,
github,
} from "@atomist/skill";
import { Configuration } from "../configuration";
import { BuildOnPushSubscription } from "../typings/types";
import * as fs from "fs-extra";
import * as df from "dateformat";

interface NpmParameters {
project: project.Project;
version: string;
check: github.Check;
}

type NpmStep = Step<EventContext<BuildOnPushSubscription, Configuration>, NpmParameters>;

const LoadProjectStep: NpmStep = {
name: "load",
run: async (ctx, params) => {
const push = ctx.data.Push[0];
const repo = push.repo;

const credential = await ctx.credential.resolve(
secret.gitHubAppToken({ owner: repo.owner, repo: repo.name, apiUrl: repo.org.provider.apiUrl }),
);

const project: project.Project = await ctx.project.load(
repository.gitHub({
owner: repo.owner,
repo: repo.name,
credential,
}),
process.cwd(),
);
params.project = project;

return {
visibility: "hidden",
code: 0,
};
},
};

const ValidateStep: NpmStep = {
name: "validate",
run: async (ctx, params) => {
if (!(await fs.pathExists(params.project.path("package.json")))) {
return {
visibility: "hidden",
code: 1,
reason: `Ignoring push to non-NPM project`,
};
}
return {
visibility: "hidden",
code: 0,
};
},
};

const SetupNodeStep: NpmStep = {
name: "setup node",
run: async (ctx, params) => {
const cfg = ctx.configuration?.[0]?.parameters;
// Set up node version
const result = await params.project.spawn("nvm", ["install", cfg.version]);
return {
code: result.status,
};
},
};

const NodeVersionStep: NpmStep = {
name: "version",
run: async (ctx, params) => {
const pj = await fs.readJson(params.project.path("package.json"));
const branch = ctx.data.Push[0].branch.split("/").join(".");
const branchSuffix = `${branch}.`;

let pjVersion = pj.version;
if (!pjVersion || pjVersion.length === 0) {
pjVersion = "0.0.1";
}

const version = `${pjVersion}-${gitBranchToNpmVersion(branchSuffix)}${formatDate()}`;
params.version = version;
const result = await params.project.spawn("npm", ["version", "--no-git-tag-version", version]);
return {
code: result.status,
};
},
};

function gitBranchToNpmVersion(branchName: string): string {
return branchName
.replace(/\//g, "-")
.replace(/_/g, "-")
.replace(/@/g, "");
}

function formatDate(date = new Date(), format = "yyyymmddHHMMss", utc = true) {
return df(date, format, utc);
}

const NpmInstallStep: NpmStep = {
name: "npm install",
run: async (ctx, params) => {
const opts = { env: { ...process.env, NODE_ENV: "development" } };
let result;
if (await fs.pathExists(params.project.path("package-lock.json"))) {
result = await params.project.spawn("npm", ["ci"], opts);
} else {
result = await params.project.spawn("npm", ["install"], opts);
}

return {
code: result.status,
};
},
};

const NodeScriptsStep: NpmStep = {
name: "npm run",
run: async (ctx, params) => {
const cfg = ctx.configuration?.[0]?.parameters;
const scripts = cfg.scripts;
// Run scripts
for (const script of scripts) {
const result = await params.project.spawn("npm", ["run", "--if-present", script]);
if (result.status !== 0) {
return {
code: result.status,
};
}
}
return {
code: 0,
};
},
};

export const handler: EventHandler<BuildOnPushSubscription, Configuration> = async ctx => {
return runSteps({
context: ctx,
steps: [LoadProjectStep, ValidateStep, SetupNodeStep, NodeVersionStep, NpmInstallStep, NodeScriptsStep],
listeners: [checkListener],
});
};
22 changes: 0 additions & 22 deletions lib/events/helloWorld.ts

This file was deleted.

10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"main": "node_modules/@atomist/skill/lib/function.js",
"dependencies": {
"@atomist/skill": "0.1.0-master.20200702145252"
"@atomist/skill": "0.1.0-master.20200702145252",
"@types/dateformat": "^3.0.1",
"dateformat": "^3.0.3"
},
"devDependencies": {
"@google-cloud/functions-framework": "^1.6.0",
Expand Down Expand Up @@ -48,7 +50,7 @@
"start": "functions-framework --target=entryPoint --signature-type=event",
"test": "mocha --require espower-typescript/guess \"test/**/*.test.ts\" --reporter min",
"test:one": "mocha --require espower-typescript/guess \"test/**/${TEST:-*.test.ts}\"",
"skill": "run-s compile test skill:generate skill:bundle skill:package",
"skill": "run-s compile test skill:generate",
"skill:generate": "atm-skill generate",
"skill:clean": "atm-skill clean",
"skill:bundle": "atm-skill bundle --minify --source-map",
Expand Down
4 changes: 3 additions & 1 deletion skill.package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#

---
apiVersion: 1
package:
type: atomist/node-skill
type: atomist/container-skill
command: npm ci && npm run skill
Loading

0 comments on commit 43cf54b

Please sign in to comment.