screwdriver-api 8.0.104 → 8.0.106

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-api",
3
- "version": "8.0.104",
3
+ "version": "8.0.106",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -715,6 +715,53 @@ async function batchStopJobs({ pipelines, prNum, action, name }) {
715
715
  await Promise.all(flatPRJobs.map(j => stopJob({ job: j, prNum, action })));
716
716
  }
717
717
 
718
+ /**
719
+ * Archive PR jobs when PR closed
720
+ * @method archivePRJobs
721
+ * @param {Object} scmConfig Response toolkit
722
+ * @param {Hapi.request} request Request from user
723
+ * @param {String} prNum Pull request number
724
+ * @param {PipelineFactory}pipelineFactory PipelineFactory object
725
+ * @param {String} hookId Unique ID for this scm event
726
+ * @return {Promise}
727
+ */
728
+ async function archivePRJobs(scmConfig, request, prNum, pipelineFactory, hookId) {
729
+ const { scmUri } = scmConfig;
730
+ const splitUri = scmUri.split(':');
731
+ const scmRepoId = `${splitUri[0]}:${splitUri[1]}`;
732
+ const listConfig = {
733
+ search: { field: 'scmUri', keyword: `${scmRepoId}:%` },
734
+ params: {
735
+ state: 'ACTIVE'
736
+ }
737
+ };
738
+ const pipelines = await pipelineFactory.list(listConfig);
739
+ const prJobName = `PR-${prNum}`;
740
+ const updatePRJobs = job =>
741
+ stopJob({ job, prNum, action: 'Closed' })
742
+ .then(() => request.log(['webhook', hookId, job.id], `${job.name} stopped`))
743
+ .then(() => {
744
+ job.archived = true;
745
+
746
+ return job.update();
747
+ })
748
+ .then(() => request.log(['webhook', hookId, job.id], `${job.name} disabled and archived`));
749
+
750
+ await Promise.all(
751
+ pipelines.map(p =>
752
+ p.getJobs({ type: 'pr' }).then(jobs => {
753
+ const prJobs = jobs.filter(j => j.name.includes(prJobName));
754
+
755
+ return Promise.all(prJobs.map(j => updatePRJobs(j)));
756
+ })
757
+ )
758
+ ).catch(err => {
759
+ logger.error(`Failed to pullRequestClosed: [${hookId}]: ${err}`);
760
+
761
+ throw err;
762
+ });
763
+ }
764
+
718
765
  /**
719
766
  * Create a new job and start the build for an opened pull-request
720
767
  * @async pullRequestOpened
@@ -862,30 +909,10 @@ async function createPrClosedEvent(options, request) {
862
909
  * @param {Hapi.reply} reply Reply to user
863
910
  */
864
911
  async function pullRequestClosed(options, request, h) {
865
- const { pipelines, hookId, name, prNum, action } = options;
866
- const updatePRJobs = job =>
867
- stopJob({ job, prNum, action })
868
- .then(() => request.log(['webhook', hookId, job.id], `${job.name} stopped`))
869
- .then(() => {
870
- job.archived = true;
871
-
872
- return job.update();
873
- })
874
- .then(() => request.log(['webhook', hookId, job.id], `${job.name} disabled and archived`));
912
+ const { pipelines, hookId } = options;
913
+ const prClosedJobs = [];
875
914
 
876
915
  try {
877
- await Promise.all(
878
- pipelines.map(p =>
879
- p.getJobs({ type: 'pr' }).then(jobs => {
880
- const prJobs = jobs.filter(j => j.name.includes(name));
881
-
882
- return Promise.all(prJobs.map(j => updatePRJobs(j)));
883
- })
884
- )
885
- );
886
-
887
- const prClosedJobs = [];
888
-
889
916
  for (const p of pipelines) {
890
917
  const jobs = await p.getJobs({ type: 'pipeline' });
891
918
  const filteredJobs = jobs.filter(
@@ -1056,6 +1083,9 @@ function pullRequestEvent(pluginOptions, request, h, parsed, token) {
1056
1083
  return triggeredPipelines(pipelineFactory, scmConfig, branch, type, action, changedFiles, releaseName, ref);
1057
1084
  })
1058
1085
  .then(async pipelines => {
1086
+ if (action === 'closed') {
1087
+ await archivePRJobs(scmConfig, request, prNum, pipelineFactory, hookId);
1088
+ }
1059
1089
  if (!pipelines || pipelines.length === 0) {
1060
1090
  const message = `Skipping since Pipeline triggered by PRs against ${fullCheckoutUrl} does not exist`;
1061
1091