UNPKG

36.9 kBMarkdownView Raw
1# Software Delivery Machine
2
3[![atomist sdm goals](http://badge.atomist.com/T29E48P34/atomist/sdm-core/e93405f2-a313-4da8-92fd-833de3b90cde)](https://app.atomist.com/workspace/T29E48P34) [![npm version](https://badge.fury.io/js/%40atomist%2Fsdm-core.svg)](https://badge.fury.io/js/%40atomist%2Fsdm-core)
4
5Atomist framework enabling you to control your delivery and development process in code. Think of it as Spring Boot for software delivery.
6
7## What is a Software Delivery Machine?
8A **software delivery machine** (SDM) is a development process in a box. It automates all steps in the flow from commit to production (potentially via staging environments), and many other actions, using the consistent model provided by the Atomist *API for software*.
9
10> Many teams have a blueprint in their mind for how they'd like to deliver software and ease their day to day work, but find it hard to realize. A Software Delivery Machine makes it possible.
11
12The concept is explained in detail in Rod Johnson's blog [Why you need a Software Delivery Machine](https://the-composition.com/why-you-need-a-software-delivery-machine-85e8399cdfc0). This [video](https://vimeo.com/260496136) shows it in action.
13
14> Atomist is about developing your development experience by using your coding skills. Change the code, restart, and see your new automations and changed behavior across all your projects, within seconds.
15
16## Get Started
17This repository contains an SDM framework built on lower level Atomist capabilities.
18
19SDMs based on this framework process events from the Atomist SaaS event hub. The architecture is as follows, with events coming in from the systems that matter in your development process:
20
21<img src="https://atomist.com/img/Atomist-Team-Development.jpg"/>
22
23You'll need to be a member of an Atomist workspace to run an SDM.
24Create your own by [enrolling](https://github.com/atomist/welcome/blob/master/enroll.md) at [atomist.com](https://atomist.com).
25Things work best if you install an org webhook, so that Atomist receives events for all your GitHub repos.
26
27Once the Atomist bot is in your Slack team, type `@atomist create sdm` to have Atomist create a personalized SDM instance using this project. You can also clone the `sample-sdm` project.
28
29Once your SDM is running, type `@atomist show skills` in any channel to see a list of all available Atomist commands.
30
31## Run Locally
32
33SDM projects are Atomist automation clients, written in [TypeScript](https://www.typescriptlang.org) or JavaScript. See [run an automation client](https://github.com/atomist/welcome/blob/master/runClient.md) for instructions on how to set up your environment and run it under Node.js.
34
35See [set up](./docs/Setup.md) for additional prerequisites depending on the projects you're building.
36
37See the [sample-sdm project](https://github.com/atomist/sample-sdm) project for instructions on how to run an SDM instance, and description of the out of the box functionality.
38
39## Core Concepts
40Atomist is a flexible platform, enabling you to build your own automations or use those provided by Atomist or third parties. Because you're using a real programming language (not YAML or Bash), and you have access to a real ecosystem (Node), you can create a richer delivery experience than you've even imagined.
41
42This project demonstrates Atomist as the *API for software*, exposing:
43
44- *What we know*: The Atomist cortex, accessible through GraphQL queries and subscription joins
45- *What just happened*: An event, triggered by a GraphQL subscription, which is contextualized with the existing knowledge
46- *What you're working on*: A library that enables you to comprehend and manipulate the source code you're working on.
47
48This project builds on other Atomist core functionality available from global automations, such as: Atomist **lifecycle**, showing commit, pull request and other activity through actionable messages.
49
50Atomist is not tied to GitHub, but this repository focuses on using Atomist with GitHub.com or
51GitHub Enterprise.
52
53
54### Events
55The heart of Atomist is its event handling. As your code flows from commit
56through to deployment and beyond, Atomist receives events, correlates the incoming data
57with its previous knowledge, and invokes your event handlers with rich context. This enables your automations to perform tasks such as:
58
59- Scanning code for security or quality issues on every push
60- Driving deployments and promotion between environments
61- Performing custom actions on deployment, such as kicking off integration test suites.
62
63The Atomist correlated event model also enables Atomist to provide you with visibility throughout the commit to deployment flow, in Slack or through the Atomist web dashboard.
64
65#### Under the Hood: How it Works
66Event handlers subscribe to events using [GraphQL](http://graphql.org) subscriptions against the Atomist cortex. The following GraphQL subscribes to completed builds, returning related data such as the last commit and any linked Slack channels:
67
68```graphql
69subscription OnBuildComplete {
70 Build {
71 buildId
72 buildUrl
73 compareUrl
74 name
75 status
76 commit {
77 sha
78 message
79 repo {
80 name
81 owner
82 gitHubId
83 allowRebaseMerge
84 channels {
85 name
86 id
87 }
88 }
89 statuses {
90 context
91 description
92 state
93 targetUrl
94 }
95 }
96 }
97}
98```
99When using TypeScript (our recommended language), an event handler can subscribe to such events with the benefit of strong typing. For example, this Atomist event handler can respond to the above GraphQL subscription:
100
101```typescript
102@EventHandler("Set status on build complete",
103 GraphQL.subscriptionFromFile("graphql/subscription/OnBuildComplete.graphql"))
104export class SetStatusOnBuildComplete implements HandleEvent<OnBuildComplete.Subscription> {
105
106 public async handle(event: EventFired<OnBuildComplete.Subscription>,
107 ctx: HandlerContext,
108 params: this): Promise<HandlerResult> {
109```
110
111This underlying GraphQL/event handler infrastructure is generic and powerful. However, many things are better done at a higher level. This project provides a framework above this infrastructure that makes typical tasks far easier, while not preventing you from breaking out into lower level functionality.
112
113> This repository
114> includes event handlers that subscribe to the most important events in a typical
115> delivery flow. This enables dynamic and sophisticated delivery processes that are consistent across
116> multiple projects.
117
118### Goals and Listeners
119
120The most important higher level SDM functionality relates to what happens on a push to a repository. An SDM allows you to process a push in any way you choose, but typically you want it to initiate a delivery flow.
121
122#### Goals
123
124An SDM allows you to set **goals** on push. Goals correspond to the actions that make up a delivery flow, such as build and deployment. Goals are not necessarily sequential--some may be executed in parallel--but certain goals, such as deployment, have preconditions (goals that must have previously completed successfully).
125
126Goals are set using **rules**, which are typically expressed in a simple internal DSL. For example, the following rules use `PushTest` predicates such as `ToDefaultBranch` and `IsMaven` to determine what goals to set for incoming pushes:
127
128```typescript
129whenPushSatisfies(ToDefaultBranch, IsMaven, HasSpringBootApplicationClass, HasCloudFoundryManifest,
130 ToPublicRepo, not(NamedSeedRepo), not(FromAtomist), IsDeployEnabled)
131 .setGoals(HttpServiceGoals),
132whenPushSatisfies(IsMaven, HasSpringBootApplicationClass, not(FromAtomist))
133 .itMeans("Spring Boot service local deploy")
134 .setGoals(LocalDeploymentGoals),
135```
136
137Push test predicates are easy to write using the Atomist API. For example:
138
139```typescript
140export const IsMaven: PredicatePushTest = predicatePushTest(
141 "Is Maven",
142 async p => !!(await p.getFile("pom.xml")));
143```
144
145Goals are defined as follows:
146
147```typescript
148export const HttpServiceGoals = new Goals(
149 "HTTP Service",
150 FingerprintGoal,
151 AutofixGoal,
152 ReviewGoal,
153 PushReactionGoal,
154 BuildGoal,
155 ArtifactGoal,
156 StagingDeploymentGoal,
157 StagingEndpointGoal,
158 StagingVerifiedGoal,
159 ProductionDeploymentGoal,
160 ProductionEndpointGoal);
161```
162
163It is possible to define new goals with accompanying implementations, making this approach highly extensible.
164
165#### Listeners
166We'll return to push tests shortly, but first let's consider the SDM listener concept.
167
168While the goals set drive the delivery process, domain specific **listeners** help in goal implementation and allow observation of the process as it unfolds. Listener **registrations** allow selective listener firing, on only particular pushes. A registration includes a name (for diagnostics) and a `PushTest`, narrowing on particular pushes.
169
170For example, the following listener registration causes an automatic fix to be made on every push to a Node project, adding a license file if none is found:
171
172```typescript
173 sdm.addAutofixes({
174 name: "fix me",
175 pushTest: IsNode,
176 action: async cri => {
177 const license = await axios.get("https://www.apache.org/licenses/LICENSE-2.0.txt");
178 return cri.project.addFile("LICENSE", license.data);
179 },
180 })
181
182```
183
184The following listener observes a build, notifying any linked Slack channels of its status:
185
186```typescript
187sdm.addBuildListeners(async br =>
188 br.addressChannels(`Build of ${br.id.repo} has status ${br.build.status}`));
189```
190
191> SDM listeners are a layer above GraphQL subscriptions and event handlers that simplify common scenarios, and enable most functionality to be naturally expressed in terms of the problem domain. Listener implementations are also easily testable.
192
193##### Common Listener Context
194As with all good frameworks, we've tried to make the API consistent. All listener invocations include at least the following generally useful information:
195
196```typescript
197export interface SdmContext {
198
199 /**
200 * Context of the Atomist EventHandler invocation. Use to run GraphQL
201 * queries, use the messageClient directly and find
202 * the team and correlation id
203 */
204 context: HandlerContext;
205
206 /**
207 * If available, provides a way to address the channel(s) related to this event.
208 * This is usually, but not always, the channels linked to a
209 * In some cases, such as repo creation or a push to a repo where there is no linked channel,
210 * addressChannels will go to dev/null without error.
211 */
212 addressChannels: AddressChannels;
213
214 /**
215 * Credentials for use with source control hosts such as GitHub
216 */
217 credentials: ProjectOperationCredentials;
218
219}
220```
221Most events concern a specific repository, and hence most listener invocations extend `RepoContext`:
222
223```typescript
224export interface RepoContext extends SdmContext {
225
226 /**
227 * The repo this relates to
228 */
229 id: RemoteRepoRef;
230
231}
232```
233
234Many repo-specific listeners are given access to the repository source, via the `Project` abstraction:
235
236```typescript
237export interface ProjectListenerInvocation extends RepoListenerInvocation {
238
239 /**
240 * The project to which this event relates. It will have been cloned
241 * prior to this invocation. Modifications made during listener invocation will
242 * not be committed back to the project (although they are acceptable if necessary, for
243 * example to run particular commands against the project).
244 * As well as working with
245 * project files using the Project superinterface, we can use git-related
246 * functionality fro the GitProject subinterface: For example to check
247 * for previous shas.
248 * We can also easily run shell commands against the project using its baseDir.
249 */
250 project: GitProject;
251
252}
253
254```
255The `Project` interface is defined in [@atomist/automation-client](https://github.com/atomist/automation-client-ts). It provides an abstraction to the present repository, with Atomist taking care of Git cloning and (if necessary) writing back any changes via a push. It is abstracted from the file system, making it easy to unit listeners accessing repository contents, using the `InMemoryProject` and `InMemoryFile` classes.
256
257> The Project API and sophisticated parsing functionality available on top of it is a core Atomist capability. Many events can only be understood in the context of the impacted code, and many actions are achieved by modifying code.
258
259Push listeners also have access to the details of the relevant push:
260
261```typescript
262export interface PushListenerInvocation extends ProjectListenerInvocation {
263
264 /**
265 * Information about the push, including repo and commit
266 */
267 readonly push: OnPushToAnyBranch.Push;
268
269}
270```
271
272##### Available Listener Interfaces
273The following listener interfaces are available:
274
275- `ArtifactListener`: Invoked when a new binary has been created
276- `BuildListener`: Invoked when a build is complete.
277- `ChannelLinkListenerInvocation`: Invoked when a channel is linked to a repo
278- `ClosedIssueListener`: Invoked when an issue is closed
279- `PushReactionListener`: Invoked in response to a code change
280- `DeploymentListener`: Invoked when a deployment has succeeded
281- `FingerprintDifferenceListener`: Invoked when a fingerprint has changed
282- `GoalsSetListener`: Invoked when goals are set on a push
283- `Listener`: Superinterface for all listeners
284- `NewIssueListener`: Invoked when an issue has been created
285- `ProjectListener`: Superinterface for all listeners that relate to a project and make the cloned project available
286- `PullRequestListener`: Invoked when a pull request is raised
287- `PushListener`: Superinterface for listeners to push events
288- `RepoCreationListener`: Invoked when a repository has been created
289- `SupersededListener`: Invoked when a commit has been superseded by a subsequent commit
290- `TagListener`: Invoked when a repo is created
291- `UpdatedIssueListener`: Invoked when an issue has been updated
292- `UserJoiningChannelListener`: Invoked when a user joins a channel
293- `VerifiedDeploymentListener`: Invoked when an endpoint has been verified
294
295
296#### Push Mappings
297Let's now return to push mappings and goal setting. The `PushMapping` interface is used to decide how to handle pushes. Normally it is used via the DSL we've seen.
298
299```typescript
300export interface PushMapping<V> {
301
302 /**
303 * Name of the PushMapping. Must be unique
304 */
305 readonly name: string;
306
307 /**
308 * Compute a value for the given push. Return undefined
309 * if we don't find a mapped value.
310 * Return DoNotSetAnyGoals (null) to shortcut evaluation of the present set of rules,
311 * terminating evaluation and guarantee the return of undefined if we've reached this point.
312 * Only do so if you are sure
313 * that this evaluation must be short circuited if it has reached this point.
314 * If a previous rule has matched, it will still be used.
315 * The value may be static
316 * or computed on demand, depending on the implementation.
317 * @param {PushListenerInvocation} p
318 * @return {Promise<V | undefined | NeverMatch>}
319 */
320 valueForPush(p: PushListenerInvocation): Promise<V | undefined | NeverMatch>;
321}
322```
323`PushMapping` is a central interface used in many places.
324
325A `GoalSetter` is a `PushMapping` that returns `Goals`.
326
327A `PushTest` is simply a `PushMapping` that returns `boolean`.
328
329## Code Examples
330Let's look at some examples of listeners.
331
332### Issue Creation
333When a new issue is created, you may want to notify people or perform an action.
334#### Listener interfaces
3351. `NewIssueListener`: [NewIssueListener](src/api/listener/NewIssueListener.ts)
336
337#### Examples
338The following example notifies any user who raises an issue with insufficient detail in the body, via a
339direct message in Slack, and provides them with a helpful
340link to the issue. Note that we make use of the
341person available via the `openedBy` field:
342
343```typescript
344export async function requestDescription(inv: NewIssueInvocation) {
345 if (!inv.issue.body || inv.issue.body.length < 10) {
346 await inv.context.messageClient.addressUsers(
347 `Please add a description for new issue ${inv.issue.number}: _${inv.issue.title}_: ${inv.id.url}/issues/${inv.issue.number}`,
348 inv.issue.openedBy.person.chatId.screenName);
349 }
350}
351```
352This is registed with a `SoftwareDeliveryMachine` instance as follows:
353
354```typescript
355sdm.addNewIssueListeners(requestDescription)
356```
357
358Using the `credentials` on the `NewIssueInvocation`, you can easily use the GitHub API to modify the issue, for example correcting spelling errors.
359
360### Repo Creation
361We frequently want to respond to the creation of a new repository: For example, we may want to notify people, provision infrastructure, or tag it with GitHub topics based on its contents.
362
363#### Listener interfaces
364There are two scenarios to consider:
365
3661. The creation of a new repository. `RepoCreationListener`: [RepoCreationListener](src/api/listener/RepoCreationListener.ts)
3672. The first push to a repository, which uses the more generic [ProjectListener](src/api/listener/PushListener.ts)
368
369The second scenario is usually more important, as it is possible to create a repository without any source code or a master branch, which isn't enough to work with for common actions.
370
371#### Examples
372The following example publishes a message to the `#general` channel in Slack when a new repo has been created:
373
374```typescript
375export const PublishNewRepo: SdmListener = (i: ListenerInvocation) => {
376 return i.context.messageClient.addressChannels(
377 `A new repo was created: \`${i.id.owner}:${i.id.repo}\``, "general");
378};
379
380```
381
382Tagging a repo with topics based on its content is a useful action. `tagRepo` is a convenient function to construct a `ProjectListener` for this. It tags as an argument a `Tagger`, which looks at the project content and returns a `Tags` object. The following example from `atomist.config.ts` tags Spring Boot repos, using a `Tagger` from the `spring-automation` project, in addition to suggesting the addition of a Cloud Foundry manifest, and publishing the repo using the listener previously shown:
383
384```typescript
385sdm.addFirstPushListener(tagRepo(springBootTagger))
386 .addFirstPushListener(suggestAddingCloudFoundryManifest),
387 .addFirstPushListener(PublishNewRepo)
388```
389
390##### ReviewerRegistration
391`ProjectReviewer` is a type defined in `automation-client-ts`. It allows a structured review to be returned. The review comments can localize the file path, line and column if such information is available, and also optionally include a link to a "fix" command to autofix the problem.
392
393The following is a simple project reviewer spots projects without a README, using the `Project` API:
394
395```typescript
396const hasNoReadMe: ReviewerRegistration = {
397 name: "hasNoReadme",
398 action: async cri => ({
399 repoId: cri.project.id,
400 comments: !!(await cri.project.getFile("README.me")) ?
401 [] :
402 [new DefaultReviewComment("info", "readme",
403 "Project has no README",
404 {
405 path: "README.md",
406 lineFrom1: 1,
407 offset: -1,
408 })],
409 }),
410};
411```
412A slightly more complex example uses the `saveFromFiles` utility method to look for and object to YAML files in Maven projects:
413
414```typescript
415const rodHatesYaml: ReviewerRegistration = {
416 name: "rodHatesYaml",
417 pushTest: hasFile("pom.xml"),
418 action: async cri => ({
419 repoId: cri.project.id,
420 comments:
421 await saveFromFiles(cri.project, "**/*.yml", f =>
422 new DefaultReviewComment("info", "yml-reviewer",
423 `Found YML in \`${f.path}\`: Rod regards the format as an insult to computer science`,
424 {
425 path: f.path,
426 lineFrom1: 1,
427 offset: -1,
428 })),
429 }),
430};
431
432```
433
434These reviewers can be added in an SDM definition as follows:
435
436```typescript
437sdm.addProjectReviewers(hasNoReadme, rodHatesYaml);
438```
439##### AutofixRegistration
440
441An `AutofixRegistration` can automatically execute code fixes. An example, which adds a license to file if one isn't found:
442
443```typescript
444export const AddLicenseFile: AutofixRegistration = editorAutofixRegistration({
445 name: "License Fix",
446 pushTest: not(hasFile(LicenseFilename)),
447 editor: async p => {
448 const license = await axios.get("https://www.apache.org/licenses/LICENSE-2.0.txt");
449 return p.addFile("LICENSE", license.data);
450 },
451});
452```
453Note the use of the `addFile` method on `Project`. Atomist takes care of committing the change to the
454branch of the push.
455
456Registration with an SDM is simple:
457
458```typescript
459sdm.addAutofixes(
460 AddAtomistJavaHeader,
461 AddAtomistTypeScriptHeader,
462 AddLicenseFile,
463);
464```
465
466##### CodeActionRegistration interface
467This registration allows you to react to the code, with information about the changes in the given push:
468
469For example, the following function lists changed files to any linked Slack channels for the repo:
470
471```typescript
472export const listChangedFiles: PushReactionRegistration = {
473 action(i: PushImpactListenerInvocation) {
474 return i.addressChannels(`Files changed:\n${i.filesChanged.map(n => "- `" + n + "`").join("\n")}`);
475 },
476 name: "List files changed",
477};
478```
479
480If you don't have a custom name or PushTest, you can use the following shorthand:
481
482
483```typescript
484export const listChangedFiles = i => i.addressChannels(`Files changed:\n${i.filesChanged.map(n => "- `" + n + "`").join("\n")}`);
485
486```
487
488Add in an SDM definition as follows:
489
490```typescript
491sdm.addPushReactions(listChangedFiles)
492```
493> If your reaction is essentially a review--for example, it's associated with a known problem in a particular file location--use a `ReviewerRegistration` rather than a `PushReactionRegistration`.
494>
495> Important note: You must have set a `PushReactionGoal` for push reactions to be invoked
496
497#### Fingerprints
498A special kind of push listener relates to **fingerprints**.
499
500Fingerprints are data computed against a push. Think of them as snapshots. Typically they reflect the state of the repository's source code after the push; they can also take into account other characteristics of the commit. Fingerprinting is valuable because:
501
5021. *It enables us to assess the impact of a particular commit, through providing a semantic diff*. For example, did the commit change dependencies? Did it change some particularly sensitive files that necessitate closer than usual review?
5032. *It enables us to understand the evolution of a code base over time.* Atomist persists fingerprints, so we can trace over time anything we fingerprint, and report against it. For example, what is happening to code quality metrics over time?
504
505Atomist ships some out of the box fingerprints, such as Maven and `npm` dependency fingerprints. But it's easy to write your own. Fingerprint registrations are like other listener registrations, specifying a name and `PushTest`. The following example is the complete code for fingerprinting dependencies specified in a `package-lock.json` file:
506
507```typescript
508export class PackageLockFingerprinter implements FingerprinterRegistration {
509
510 public readonly name = "PackageLockFingerprinter";
511
512 public readonly pushTest: PushTest = IsNode;
513
514 public async action(cri: PushImpactListenerInvocation): Promise<FingerprinterResult> {
515 const lockFile = await cri.project.getFile("package-lock.json");
516 if (!lockFile) {
517 return [];
518 }
519 try {
520 const content = await lockFile.getContent();
521 const json = JSON.parse(content);
522 const deps = json.dependencies;
523 const dstr = JSON.stringify(deps);
524 return {
525 name: "dependencies",
526 abbreviation: "deps",
527 version: "0.1",
528 sha: computeShaOf(dstr),
529 data: json,
530 };
531 } catch (err) {
532 logger.warn("Unable to compute package-lock.json fingerprint: %s", err.message);
533 return [];
534 }
535 }
536}
537```
538
539Fingerprinters can be added to an SDM as follows:
540
541```typescript
542sdm.addFingerprinterRegistrations(new PackageLockFingerprinter());
543```
544
545Fingerprinting will only occur if a `FingerprintGoal` is selected when goals are set.
546
547## Generators
548Another important concern is project creation. Consistent project creation is important to governance and provides a way of sharing knowledge across a team.
549
550Atomist's [unique take on project generation](https://the-composition.com/no-more-copy-paste-bf6c7f96e445) starts from a **seed project**--a kind of golden master, that is version controlled using your regular repository hosting solution. A seed project doesn't need to include template content: It's a regular project in whatever stack, and Atomist transforms it to be a unique, custom project based on the parameters supplied at the time of project creation. This allows freedom to evolve the seed project with regular development tools.
551
552Generators can be registered with an SDM as follows:
553
554```typescript
555sdm.addGenerators(() => springBootGenerator({
556 ...CommonJavaGeneratorConfig,
557 seedRepo: "spring-rest-seed",
558 intent: "create spring",
559}))
560```
561
562The `springBootGenerator` function used here is provided in `sample-sdm`, but it's easy enough to write your own transformation using the `Project` API. Here's most of the code in our real Node generator:
563
564```typescript
565export function nodeGenerator(config: GeneratorConfig,
566 details: Partial<GeneratorCommandDetails<NodeProjectCreationParameters>> = {}): HandleCommand {
567 return generatorHandler<NodeProjectCreationParameters>(
568 transformSeed,
569 () => new NodeProjectCreationParameters(config),
570 `nodeGenerator-${config.seedRepo}`,
571 {
572 tags: ["node", "typescript", "generator"],
573 ...details,
574 intent: config.intent,
575 });
576}
577
578function transformSeed(params: NodeProjectCreationParameters, ctx: HandlerContext) {
579 return chainEditors(
580 updatePackageJsonIdentification(params.appName, params.target.description,
581 params.version,
582 params.screenName,
583 params.target),
584 updateReadmeTitle(params.appName, params.target.description),
585 );
586}
587```
588
589You can invoke such a generator from Slack, like this:
590
591<img src="https://github.com/atomist/github-sdm/blob/master/docs/create_sample1.png?raw=true"/>
592
593Note how the repo was automatically tagged with GitHub topics after creation. This was the work of a listener, specified as follows:
594
595```typescript
596sdm.addFirstPushListener(
597 tagRepo(springBootTagger),
598);
599```
600
601With Atomist ChatOps supports, you can follow along in a linked channel like this:
602
603<img src="https://github.com/atomist/github-sdm/blob/master/docs/sample1_channel.png?raw=true"/>
604
605Note the suggestion to add a Cloud Foundry manifest. This is the work of another listener, which reacts to finding new code in a repo. Listeners and commands such as generators work hand in hand for Atomist.
606
607## Editors
608Another core concept is a project **editor**. An editor is a command that transforms project content. Atomist infrastructure can help persist such transformations through branch commits or pull requests, with clean diffs.
609
610### A Simple Editor
611As you'd expect, editors also use th `Project` API.
612
613Here's an example of a simple editor that takes as a parameter the path of a file to remove from a repository.
614
615```typescript
616@Parameters()
617export class RemoveFileParams {
618
619 @Parameter()
620 public path: string;
621}
622
623export const removeFileEditor: HandleCommand = editorCommand<RemoveFileParams>(
624 () => removeFile,
625 "remove file",
626 RemoveFileParams,
627 {
628 editMode: params => commitToMaster(`You asked me to remove file ${params.path}!`),
629 });
630
631async function removeFile(p: Project, ctx: HandlerContext, params: RemoveFileParams) {
632 return p.deleteFile(params.path);
633}
634```
635
636Editors can be registered with an SDM as follows:
637
638```typescript
639sdm.addEditors(
640 () => removeFileEditor,
641);
642```
643
644### Dry Run Editors
645More elaborate editors use helper APIs on top of the `Project` API such as Atomist's [microgrammar](https://github.com/atomist/microgrammar) API and [ANTLR](https://github.com/atomist/antlr-ts) integration.
646
647There's also an important capability called "dry run editing": Performing an edit on a branch, and then either raising either a PR or an issue, depending on build success or failure. This allows us to safely apply edits across many repositories. There's a simple wrapper function to enable this:
648
649```typescript
650export const tryToUpgradeSpringBootVersion: HandleCommand = dryRunEditor<UpgradeSpringBootParameters>(
651 params => setSpringBootVersionEditor(params.desiredBootVersion),
652 UpgradeSpringBootParameters,
653 "boot-upgrade", {
654 description: `Upgrade Spring Boot version`,
655 intent: "try to upgrade Spring Boot",
656 },
657);
658```
659This editor will upgrade the Spring Boot version in one or more projects, then wait to see if the builds succeed. Output will look like this (in the case of success):
660
661<img src="https://github.com/atomist/github-sdm/blob/master/docs/dry_run_upgrade.png?raw=true"/>
662
663> Dry run editing is another example of how commands and events can work hand in hand with Atomist to provide a uniquely powerful solution.
664
665
666## Arbitrary Commands
667Both generators and editors are special cases of Atomist **command handlers**, which can be invoked via Slack or HTTP. You can write commands to ensure that anything that needs to be repeated gets done the right way each time, and that the solution isn't hidden on someone's machine.
668
669## Pulling it All Together: The `SoftwareDeliveryMachine` class
670
671Your ideal delivery blueprint spans delivery flow, generators, editors and other commands. All we need is something to pull it together.
672
673Your event listeners need to be invoked by Atomist handlers. The `SoftwareDeliveryMachine` takes care of this, ensuring that the correct handlers are emitted for use in `atomist.config.ts`, without you needing to worry about the event handler registrations on underlying GraphQL.
674
675The `SoftwareDeliveryMachine` class offers a fluent builder approach to adding command handlers, generators and editors.
676
677### Example
678For example:
679
680```typescript
681 const sdm = createSoftwareDeliveryMachine(
682 {
683 builder: K8sBuildOnSuccessStatus,
684 deployers: [
685 K8sStagingDeployOnSuccessStatus,
686 K8sProductionDeployOnSuccessStatus,
687 ],
688 artifactStore,
689 },
690 whenPushSatisfies(PushToDefaultBranch, IsMaven, IsSpringBoot, HasK8Spec, PushToPublicRepo)
691 .setGoals(HttpServiceGoals),
692 whenPushSatisfies(not(PushFromAtomist), IsMaven, IsSpringBoot)
693 .setGoals(LocalDeploymentGoals),
694 whenPushSatisfies(IsMaven, MaterialChangeToJavaRepo)
695 .setGoals(LibraryGoals),
696 whenPushSatisfies(IsNode).setGoals(NpmGoals),
697 );
698 sdm.addFirstPushListener(suggestAddingK8sSpec)
699 .addSupportingCommands(() => addK8sSpec)
700 .addSupportingEvents(() => NoticeK8sTestDeployCompletion,
701 () => NoticeK8sProdDeployCompletion)
702 .addEndpointVerificationListeners(
703 lookFor200OnEndpointRootGet({
704 retries: 15,
705 maxTimeout: 5000,
706 minTimeout: 3000,
707 }),
708 );
709 sdm.addNewIssueListeners(requestDescription)
710 .addEditors(() => tryToUpgradeSpringBootVersion)
711 .addGenerators(() => springBootGenerator({
712 seedOwner: "spring-team",
713 seedRepo: "spring-rest-seed",
714 groupId: "myco",
715 }))
716 .addFirstPushListener(tagRepo(springBootTagger))
717 .addProjectReviewers(logReview)
718 .addPushReactions(listChangedFiles)
719 .addFingerprinters(mavenFingerprinter)
720 .addDeploymentListeners(PostToDeploymentsChannel)
721 .addEndpointVerificationListeners(LookFor200OnEndpointRootGet)
722 .addVerifiedDeploymentListeners(presentPromotionButton)
723 .addSupersededListeners(
724 inv => {
725 logger.info("Will undeploy application %j", inv.id);
726 return LocalMavenDeployer.deployer.undeploy(inv.id);
727 })
728 .addSupportingCommands(
729 () => addCloudFoundryManifest,
730 DescribeStagingAndProd,
731 () => disposeProjectHandler,
732 )
733 .addSupportingEvents(OnDryRunBuildComplete);
734```
735The `SoftwareDeliveryMachine` instance will create the necessary Atomist event handlers to export.
736
737In `atomist.config.ts` you can bring them in simply as follows:
738
739```typescript
740commands: assembled.commandHandlers,
741events: assembled.eventHandlers,
742```
743
744## Structure of This Project
745
746- `src/api` is the public user-facing API, including the software delivery machine concept that ties everything together. *This may be extracted into its own Node module in future.*
747- `src/spi` contains interfaces to be extended in integrations with infrastructure,
748such as artifact storage, logging, build and deployment.
749- `src/graphql` contains GraphQL queries. You can add fields to existing queries and subscriptions, and add your own.
750- `src/typings` is where types generated from GraphQL wind up. Refresh these with `npm run gql:gen`
751if you update any GraphQL files in `src/graphql`.
752- `src/util` contains miscellaneous utilities.
753- `src/internal` contains lower level code such as event handlers necessary to support the user API. This is not intended for user use.
754- `src/pack` contains "extension packs." These will ultimately be extracted into their own Node modules.
755- The other directories unders `src` contain useful functionality that may eventually be moved out of this project.
756
757The types from `src/api` can be imported into downstream projects from the `index.ts` barrel.
758
759## Plugging in Third Party Tools
760
761This repo shows the use of Atomist to perform many steps itself. However, each of the goals used by Atomist here is pluggable.
762
763It's also easy to integrate third party tools like Checkstyle.
764
765### Integrating CI tools
766One of the tools you are most likely to integrate is CI. For example, you can integrate Jenkins, Travis or Circle CI with Atomist so that these tools are responsible for build. This has potential advantages in terms of scheduling and repeatability of environments.
767
768Integrating a CI tool with Atomist is simple. Simply invoke Atomist hooks to send events around build and artifact creation.
769
770If integrating CI tools, we recommend the following:
771
772- CI tools are great for building and generating artifacts. They are often abused as a PaaS for `bash`. If you find your CI usage has you programming in `bash` or YML, consider whether invoking such operations from Atomist event handlers might be a better model.
773- Use Atomist generators to create your CI files, and Atomist editors to keep them in synch, minimizing inconsistency.
774
775### Integrating APM tools
776tbd
777
778### Integrating with Static Analysis Tools
779Any tool that runs on code, such as Checkstyle, can easily be integrated.
780
781If the tool doesn't have a Node API (which Checkstyle doesn't as it's written in Java), you can invoke it via Node `spawn`, as Node excels at working with child processes.
782
783## Advanced Push Rules
784
785### Computed Values
786You can use computed `boolean` values or the results of synchronous or asynchronous functions returning `boolean` in the DSL, making it possible to bring in any state you wish. For example:
787
788```typescript
789whenPushSatisfies(IsMaven, HasSpringBootApplicationClass,
790 deploymentsToday < 25)
791 .itMeans("Not tired of deploying Spring apps yet")
792 .setGoals(LocalDeploymentGoals),
793```
794
795### Decision Trees
796You can write decision trees in push rules or other push mappings. These can be nested to arbitrary depth, and can use computed state. For example:
797
798```typescript
799let count = 0;
800const pm: PushMapping<Goals> = given<Goals>(IsNode)
801 // Compute a value we'll use later
802 .init(() => count = 0)
803 .itMeans("node")
804 .then(
805 given<Goals>(IsExpress).itMeans("express")
806 .compute(() => count++)
807 // Go into tree branch rule set
808 .then(
809 whenPushSatisfies(count > 0).itMeans("nope").setGoals(NoGoals),
810 whenPushSatisfies(TruePushTest).itMeans("yes").setGoals(HttpServiceGoals),
811 ),
812 );
813```
814
815### "Contribution" Style
816tbd
817
818## Roadmap
819
820This project is under active development, and still in flux. Some goals:
821
822- Splitting out `sdm-api` project with the contents of the `src/api` directory
823- Extracting the extension packs under `src/pack` into their own Node modules.